Skip to content

Commit c46cfd7

Browse files
committed
Added: _WinHttpSimpleFormFill_SetCallback function.
1 parent 46e7a3c commit c46cfd7

File tree

2 files changed

+99
-2
lines changed

2 files changed

+99
-2
lines changed

WinHttp.au3

Lines changed: 39 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ DllOpen("winhttp.dll") ; making sure reference count never reaches 0
6464
;_WinHttpSetTimeouts
6565
;_WinHttpSimpleBinaryConcat
6666
;_WinHttpSimpleFormFill
67+
;_WinHttpSimpleFormFill_SetCallback
6768
;_WinHttpSimpleReadData
6869
;_WinHttpSimpleReadDataAsync
6970
;_WinHttpSimpleRequest
@@ -1108,7 +1109,7 @@ EndFunc
11081109
; +If you want to upload using alternative way (to avoid certain PHP bug that could exist on server side) then prefix the file string with [["PHP#50338:"]] string.
11091110
; +For example: [[..."name:files[]", "PHP#50338:" & $sFile1 & "|" & $sFile2 ...]]
11101111
; +Muliple files are always separated with vertical line ASCII character when filling the form.
1111-
; Related .......: _WinHttpConnect
1112+
; Related .......: _WinHttpConnect, _WinHttpSimpleFormFill_SetCallback
11121113
;============================================================================================
11131114
Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId = Default, $sFldId1 = Default, $sDta1 = Default, $sFldId2 = Default, $sDta2 = Default, $sFldId3 = Default, $sDta3 = Default, $sFldId4 = Default, $sDta4 = Default, $sFldId5 = Default, $sDta5 = Default, $sFldId6 = Default, $sDta6 = Default, $sFldId7 = Default, $sDta7 = Default, $sFldId8 = Default, $sDta8 = Default, $sFldId9 = Default, $sDta9 = Default, $sFldId10 = Default, $sDta10 = Default, _
11141115
$sFldId11 = Default, $sDta11 = Default, $sFldId12 = Default, $sDta12 = Default, $sFldId13 = Default, $sDta13 = Default, $sFldId14 = Default, $sDta14 = Default, $sFldId15 = Default, $sDta15 = Default, $sFldId16 = Default, $sDta16 = Default, $sFldId17 = Default, $sDta17 = Default, $sFldId18 = Default, $sDta18 = Default, $sFldId19 = Default, $sDta19 = Default, $sFldId20 = Default, $sDta20 = Default, _
@@ -1554,6 +1555,31 @@ Func _WinHttpSimpleFormFill(ByRef $hInternet, $sActionPage = Default, $sFormId =
15541555
Return SetError(3, 0, "")
15551556
EndFunc
15561557

1558+
; #FUNCTION# ====================================================================================================================
1559+
; Name...........: _WinHttpSimpleFormFill_SetCallback
1560+
; Description ...: Sets user defined function as callback function for _WinHttpSimpleFormFill
1561+
; Syntax.........: _WinHttpSimpleFormFill_SetCallback($vCallback [, $iNumChunks = 100 ])
1562+
; Parameters ....: $vCallback - UDF's name
1563+
; $iNumChunks - [optional] number of chunks to send during form submitting. Default is 100.
1564+
; Return values .: Undefined.
1565+
; Author ........: trancexx
1566+
; Remarks .......: Unregistering is done by passing [[0]] as first argument.
1567+
; Related .......: _WinHttpSimpleFormFill
1568+
; ===============================================================================================================================
1569+
Func _WinHttpSimpleFormFill_SetCallback($vCallback = Default, $iNumChunks = 100)
1570+
If $iNumChunks <= 0 Then $iNumChunks = 100
1571+
Local Static $vFunc = Default, $iParts = $iNumChunks
1572+
If $vCallback <> Default Then
1573+
$vFunc = $vCallback
1574+
$iParts = Ceiling($iNumChunks)
1575+
ElseIf $vCallback = 0 Then
1576+
$vFunc = Default
1577+
$iParts = 1
1578+
EndIf
1579+
Local $aOut[2] = [$vFunc, $iParts]
1580+
Return $aOut
1581+
EndFunc
1582+
15571583
; #FUNCTION# ====================================================================================================================
15581584
; Name...........: _WinHttpSimpleReadData
15591585
; Description ...: Reads data from a request
@@ -2163,7 +2189,18 @@ Func __WinHttpFormSend($hInternet, $sMethod, $sAction, $fMultiPart, $sBoundary,
21632189
If $sAdditionalHeaders Then _WinHttpAddRequestHeaders($hRequest, $sAdditionalHeaders, BitOR($WINHTTP_ADDREQ_FLAG_REPLACE, $WINHTTP_ADDREQ_FLAG_ADD))
21642190
_WinHttpSetOption($hRequest, $WINHTTP_OPTION_DECOMPRESSION, $WINHTTP_DECOMPRESSION_FLAG_ALL)
21652191
_WinHttpSetOption($hRequest, $WINHTTP_OPTION_UNSAFE_HEADER_PARSING, 1)
2166-
_WinHttpSendRequest($hRequest, Default, $sAddData)
2192+
Local $aClbk = _WinHttpSimpleFormFill_SetCallback()
2193+
If $aClbk[0] <> Default Then
2194+
Local $iSize = StringLen($sAddData), $iChunk = Floor($iSize / $aClbk[1]), $iRest = $iSize - ($aClbk[1] - 1) * $iChunk, $iCurCh = $iChunk
2195+
_WinHttpSendRequest($hRequest, Default, Default, $iSize)
2196+
For $i = 1 To $aClbk[1]
2197+
If $i = $aClbk[1] Then $iCurCh = $iRest
2198+
_WinHttpWriteData($hRequest, StringMid($sAddData, 1 + $iChunk * ($i -1), $iCurCh))
2199+
Call($aClbk[0], Floor($i * 100 / $aClbk[1]))
2200+
Next
2201+
Else
2202+
_WinHttpSendRequest($hRequest, Default, $sAddData)
2203+
EndIf
21672204
_WinHttpReceiveResponse($hRequest)
21682205
__WinHttpSetCredentials($hRequest, "", $sAddData, $sCredName, $sCredPass)
21692206
Return $hRequest
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
2+
#include "WinHTTP.au3"
3+
4+
$sAddress = "https://posttestserver.com/post.php?dir=WinHttp" ; the address of the target (https or http, makes no difference - handled automatically)
5+
6+
; Select some file
7+
$sFileToUpload = FileOpenDialog("Select file to upload...", "", "All Files (*)")
8+
If Not $sFileToUpload Then Exit 5 ; check if the file is selected and exit if not
9+
10+
$sForm = _
11+
'<form action="' & $sAddress & '" method="post" enctype="multipart/form-data">' & _
12+
' <input type="file" name="upload"/>' & _
13+
'</form>'
14+
15+
; Initialize and get session handle
16+
$hOpen = _WinHttpOpen()
17+
18+
$hConnect = $sForm ; will pass form as string so this is for coding correctness because $hConnect goes in byref
19+
20+
; Creates progress bar window
21+
ProgressOn("UPLOADING", $sFileToUpload, "0%")
22+
23+
; Register callback function
24+
_WinHttpSimpleFormFill_SetCallback(UploadCallback)
25+
26+
; Fill form
27+
$sHTML = _WinHttpSimpleFormFill($hConnect, $hOpen, _
28+
Default, _
29+
"name:upload", $sFileToUpload)
30+
; Collect error number
31+
$iErr = @error
32+
33+
; Unregister callback function
34+
_WinHttpSimpleFormFill_SetCallback(0)
35+
36+
; Kill progress bar window
37+
ProgressOff()
38+
39+
; Close handles
40+
_WinHttpCloseHandle($hConnect)
41+
_WinHttpCloseHandle($hOpen)
42+
43+
If $iErr Then
44+
MsgBox(4096, "Error", "Error number = " & $iErr)
45+
Else
46+
ConsoleWrite($sHTML & @CRLF)
47+
MsgBox(4096, "Success", $sHTML)
48+
EndIf
49+
50+
51+
52+
; Callback function. For example, update progress control
53+
Func UploadCallback($iPrecent)
54+
If $iPrecent = 100 Then
55+
ProgressSet(100, "Done", "Complete")
56+
Sleep(800) ; give some time for the progress bar to fill-in completely
57+
Else
58+
ProgressSet($iPrecent, $iPrecent & "%")
59+
EndIf
60+
EndFunc

0 commit comments

Comments
 (0)