Skip to content

Commit 2d7f885

Browse files
committed
Improved attachment download compatibility
Added more headers to make it appear more like a browser request, and try to use TLS1.3, if we get a 403 (which is useful for digikey). Commit cherry picked from @Treeed
1 parent f5c17bc commit 2d7f885

File tree

1 file changed

+21
-2
lines changed

1 file changed

+21
-2
lines changed

src/Services/Attachments/AttachmentSubmitHandler.php

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -345,9 +345,28 @@ protected function downloadURL(Attachment $attachment, bool $secureAttachment):
345345
$tmp_path = $attachment_folder.DIRECTORY_SEPARATOR.$this->generateAttachmentFilename($attachment, 'tmp');
346346

347347
try {
348-
$response = $this->httpClient->request('GET', $url, [
348+
$opts = [
349349
'buffer' => false,
350-
]);
350+
//Use user-agent and other headers to make the server think we are a browser
351+
'headers' => [
352+
"sec-ch-ua" => "\"Not(A:Brand\";v=\"99\", \"Google Chrome\";v=\"133\", \"Chromium\";v=\"133\"",
353+
"sec-ch-ua-mobile" => "?0",
354+
"sec-ch-ua-platform" => "\"Windows\"",
355+
"user-agent" => "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/133.0.0.0 Safari/537.36",
356+
"sec-fetch-site" => "none",
357+
"sec-fetch-mode" => "navigate",
358+
],
359+
360+
];
361+
$response = $this->httpClient->request('GET', $url, $opts);
362+
//Digikey wants TLSv1.3, so try again with that if we get a 403
363+
if ($response->getStatusCode() === 403) {
364+
$opts['crypto_method'] = STREAM_CRYPTO_METHOD_TLSv1_3_CLIENT;
365+
$response = $this->httpClient->request('GET', $url, $opts);
366+
}
367+
# if you have these changes and downloads still fail, check if it's due to an unknown certificate. Curl by
368+
# default uses the systems ca store and that doesn't contain all the intermediate certificates needed to
369+
# verify the leafs
351370

352371
if (200 !== $response->getStatusCode()) {
353372
throw new AttachmentDownloadException('Status code: '.$response->getStatusCode());

0 commit comments

Comments
 (0)