Skip to content

Commit

Permalink
Replaced WinHttpRequest and the HttpRequest lib with cURL (#391)
Browse files Browse the repository at this point in the history
* replaced WinHttp/HttpRequest with curl

* fixed file download error handling

* replaced some connection failure error handling

* updated notes; removed unused libraries

* added curl downloadlink

* corrected spelling error
  • Loading branch information
Eruyome authored Jul 19, 2017
1 parent a37197e commit 38aed34
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 1,442 deletions.
Binary file modified Fallback.exe
Binary file not shown.
1,017 changes: 0 additions & 1,017 deletions lib/HTTPRequest.ahk

This file was deleted.

103 changes: 82 additions & 21 deletions lib/PoEScripts_Download.ahk
Original file line number Diff line number Diff line change
@@ -1,46 +1,77 @@
PoEScripts_Download(url, ioData, ioHdr, options, useFallback = true, critical = false, binaryDL = false, errorMsg = "") {
/*
url = download url
postData = postData
reqHeaders= multiple request headers separated by newline
options = multiple options separated by newline
(take a look at WinHttpRequest.ahk)
ioData = uri encoded postData
ioHdr = array of request headers
options = multiple options separated by newline (currently only "SaveAs:")
useFallback = Use UrlDownloadToFile if WinHttp fails, not possible for POST requests or when cookies are required
useFallback = Use UrlDownloadToFile if curl fails, not possible for POST requests or when cookies are required
critical = exit macro if download fails
binaryDL = file download (zip for example)
errorMsg = optional error message, will be added to default message
*/


; https://curl.haxx.se/download.html -> https://bintray.com/vszakats/generic/curl/
curl := A_ScriptDir "\lib\curl.exe"
headers := ""
For key, val in ioHdr {
headers .= "-H """ val """ "
}

If (StrLen(options)) {
RegExMatch(options, "i)SaveAs:[ \t]*\K[^\r\n]+", SavePath)
commandData .= " " options " "
commandHdr .= ""
}

e := {}
Try {
;WinHttpRequest(url, ioData, ioHdr, options, Out_Headers_Obj)
HTTPRequest(url, ioData, ioHdr, options)
html := ioData
Try {
commandData := curl
commandHdr := curl
If (binaryDL) {
commandData .= " -LJkv " ; save as file
If (SavePath) {
commandData .= "-o """ SavePath """ " ; set target destination and name
}
} Else {
commandData .= " -Ls --compressed " ; follow redirects
commandHdr .= " -ILs " ; follow redirects
}
If (StrLen(headers)) {
commandData .= headers
commandHdr .= headers
}
If (StrLen(ioData)) {
commandData .= "--data """ ioData """ "
}

; get data
html := StdOutStream(commandData """" url """")
If (not binaryDL) {
; get headers in seperate request
ioHdr := StdOutStream(commandHdr """" url payload """") ; add payload to url since you can't use the -I argument with POST requests
}
} Catch e {

}

If (!binaryDL) {
; Use fallback download if WinHttpRequest fails
If ((StrLen(html) < 1 or not html or e.what) and useFallback) {
; Use fallback download if curl fails
If ((not RegExMatch(ioHdr, "i)HTTP\/1.1 200 OK") or e.what) and useFallback) {
DownloadFallback(url, html, e, critical, errorMsg)
} Else If ((StrLen(html) < 1 or not html) and e.what) {
} Else If (not RegExMatch(ioHdr, "i)HTTP\/1.1 200 OK" and e.what)) {
ThrowError(e)
} Else If ((StrLen(html) < 1 or not html)) {

}
}
; handle binary file downloads
Else If (InStr(Options, "SaveAs:") and not e.what) {
; check returned request headers (including custom error messages)
If (RegExMatch(ioHdr, "(CreateFile).*?( failed)|(WriteFile).*?( failed)", match)) {
MsgBox, 16,, % "Error downloading file. " match1 match2 match3 match4 "!"
Else If (not e.what) {
; check returned request headers
ioHdr := ParseReturnedHeaders(html)
If (not RegExMatch(ioHdr, "i)HTTP\/1.1 200 OK")) {
MsgBox, 16,, % "Error downloading file to " SavePath
Return "Error: Wrong Status"
}

RegExMatch(Options, "i)SaveAs:[ \t]*\K[^\r\n]+", SavePath)

; compare file sizes
FileGetSize, sizeOnDisk, %SavePath%
RegExMatch(ioHdr, "i)Content-Length:\s(\d+)", size)
Expand All @@ -55,6 +86,36 @@
Return html
}

ParseReturnedHeaders(output) {
headerGroups := []
headerGroup := ""

Pos := 0
While Pos := RegExMatch(output, "is)\[5 bytes data.*?({|$)", match, Pos + (StrLen(match) ? StrLen(match) : 1)) {
headerGroups.push(match)
}

i := headerGroups.Length()
Loop, % i {
If (RegExMatch(headerGroups[i], "is)Content-Length")) {
headerGroup := headerGroups[i]
break
}
i--
}

out := ""
headerGroup := RegExReplace(headerGroup, "im)^<|\[5 bytes data\]|^{")
Loop, parse, headerGroup, `n, `r
{
If (StrLen(Trim(A_LoopField))) {
out .= Trim(A_LoopField)
}
}

Return out
}

; only works if no post data required/not downloading for example .zip files
DownloadFallback(url, ByRef html, e, critical, errorMsg) {
ErrorLevel := 0
Expand Down
31 changes: 7 additions & 24 deletions lib/PoEScripts_Update.ahk
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#Include, %A_ScriptDir%\lib\JSON.ahk
#Include, %A_ScriptDir%\lib\zip.ahk
#Include, %A_ScriptDir%\lib\WinHttpRequest.ahk

PoEScripts_Update(user, repo, ReleaseVersion, ShowUpdateNotification, userDirectory, isDevVersion, skipSelection, skipBackup, SplashScreenTitle = "") {
status := GetLatestRelease(user, repo, ReleaseVersion, ShowUpdateNotification, userDirectory, isDevVersion, skipSelection, skipBackup, SplashScreenTitle)
Expand All @@ -15,18 +14,12 @@ GetLatestRelease(user, repo, ReleaseVersion, ShowUpdateNotification, userDirecto
url := "https://api.github.com/repos/" . user . "/" . repo . "/releases"
downloadUrl := "https://github.com/" . user . "/" . repo . "/releases"
html := ""

postData := ""
reqHeaders =
(LTrim
Content-type: application/html
)
options =
(LTrim
Charset: UTF-8
Codepage: 65001
Method: GET
)
options := ""

reqHeaders := []
reqHeaders.push("Content-Type: text/html; charset=UTF-8")

Try {
errorMsg := "Update check failed. Please check manually on the Github page for updates.`nThe script will now continue."
Expand Down Expand Up @@ -493,18 +486,8 @@ DownloadRelease(url, project, ByRef savePath) {
}

postData := ""
reqHeaders =
(LTrim
Content-type: application/octet-stream
User-Agent: %project%
)
options =
(LTrim
Charset: UTF-8
Codepage: 65001
SaveAs: %savePath%
Method: GET
)
reqHeaders := []
options := "SaveAs: " savePath
response := PoEScripts_Download(url, ioData := postData, ioHdr := reqHeaders, options, true, true, true)
SplashTextOff

Expand Down
Loading

0 comments on commit 38aed34

Please sign in to comment.