The MASM Forum

General => The Workshop => Topic started by: TouEnMasm on August 25, 2021, 05:46:10 PM

Title: Winhttp asynchronous
Post by: TouEnMasm on August 25, 2021, 05:46:10 PM
Hello,
This time ,it's fully tested and must be in the very few number of samples who works in asynchronous mode (There is only  a few c++ existing and working)
The sample will give a full information of what is happened in the callback and save the result in download .htm and made a report.
in the screen and in rapport.txt.
The exe accept command line parameters or batch (see check.bat).
The source need /safeseh to work without too many problems
Winhttp errors and HTTP_status are reported,the WINHTTP_CALLBACK_STATUS_REQUEST_ERROR give now an answer in text and give valuable informations .
Use JWASM to compile (/safeseh)

Quote
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Wed, 25 Aug 2021 07:06:12 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Expires: Wed, 25 Aug 2021 07:05:12 GMT
P3P: CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND"
Set-Cookie: MUID=3FA559A961F86FB23B394937604C6ED6; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: MUIDB=3FA559A961F86FB23B394937604C6ED6; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/; HttpOnly
Set-Cookie: _EDGE_S=F=1&SID=2AC07C33CB67613A17F96CADCAD3607A; domain=.bing.com; path=/; HttpOnly
Set-Cookie: _EDGE_V=1; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/; HttpOnly
Set-Cookie: SRCHD=AF=NOFORM; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHUID=V=2&GUID=135D188FB00C49DA9E193E214FB23495&dmnchg=1; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHUSR=DOB=20210825; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHHPGUSR=SRCHLANG=fr; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: _SS=SID=2AC07C33CB67613A17F96CADCAD3607A; domain=.bing.com; path=/
X-SNR-Routing: 1
X-Cache: CONFIG_NOCACHE
X-MSEdge-Ref: Ref A: DFC8A63D54344001AF7071FB13997BE4 Ref B: PRAEDGE0919 Ref C: 2021-08-25T07:06:12Z




Title: Re: Winhttp asynchronous
Post by: bomz on August 25, 2021, 07:57:02 PM
invoke GetStdHandle,STD_ERROR_HANDLE or STD_OUTPUT_HANDLE (https://docs.microsoft.com/en-us/windows/console/getstdhandle)

you need not only download http://www.bing.com/search?q=useragent but run scripts to get reaserch result
so you need offline browser, not console download manager

"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe" --headless --disable-gpu --dump-dom !_url!

IEInspector HTTP Analyzer (https://www.ieinspector.com/httpanalyzer/)

Quote(Request-Line):GET /search?q=useragent HTTP/1.1
Host:www.bing.com
Connection:keep-alive
Upgrade-Insecure-Requests:1
User-Agent:Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9
Sec-Fetch-Site:none
Sec-Fetch-Mode:navigate
Sec-Fetch-User:?1
Sec-Fetch-Dest:document
sec-ch-ua:"Chromium";v="92", " Not A;Brand";v="99", "Google Chrome";v="92"
sec-ch-ua-mobile:?0
sec-ch-ua-full-version:"92.0.4515.159"
sec-ch-ua-arch:"x86"
sec-ch-ua-platform:"Windows"
sec-ch-ua-platform-version:"6.1"
sec-ch-ua-model:""
Accept-Language:ru-RU,ru;q=0.9,en-US;q=0.8,en;q=0.7
Cookie:MUID=18AA8E6567076A4D1F349E7363076CE2; MUIDB=18AA8E6567076A4D1F349E7363076CE2; SRCHD=AF=NOFORM; SRCHUID=V=2&GUID=77633CDE97564DB784A4C09CA0A6AC5E&dmnchg=1; _EDGE_S=SID=16E5C2E8E4AA65102E1CD276E5B464B7; _SS=SID=16E5C2E8E4AA65102E1CD276E5B464B7; ABDEF=V=13&ABDV=13&MRNB=1629885620684&MRB=0; SRCHUSR=DOB=20210823&T=1629885627000&TPC=1629885622000; SRCHHPGUSR=SRCHLANG=ru&BRW=M&BRH=M&CW=1280&CH=881&SW=1280&SH=1024&DPR=1&UTC=420&DM=0&WTS=63765482427&HV=1629885624; ipv6=hit=1629889223847&t=4
Accept-Encoding:gzip, deflate
Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 26, 2021, 04:16:48 AM

Problem is simple,in synchronous it work without problem.
In asynchronous ,instead of receive data i receive a header with no interest.
How can this be changed ?
Title: Re: Winhttp asynchronous
Post by: bomz on August 26, 2021, 05:11:11 AM
may be no-redirect. analyze what your application do with traffic analyzer, it help find big part of mistakes

interesting if http redirect to https flag secure change or not
Title: Re: Winhttp asynchronous
Post by: bomz on August 26, 2021, 08:45:26 PM
[ebx].REQUEST_CONTEXT.SessionHandle
cool. I always forget how use assume
Title: Re: Winhttp asynchronous
Post by: bomz on August 28, 2021, 12:35:49 AM
how you want decide problem http to https redirection?
how change this flag WINHTTP_FLAG_SECURE ?
Title: Re: Winhttp asynchronous
Post by: LiaoMi on August 28, 2021, 09:06:37 AM
Quote from: TouEnMasm on August 25, 2021, 05:46:10 PM
Hello,
This project need to be verify.
There is no bug,no failed (very easy to do in asynchronous mode).
The sample will give a full information of what is happened in the callback and save the result in donnload .htm
The exe accept command line parameters or batch (see check.bat).
At the instant,I get very usefull informations from the server like this one below.
Need help to establish a correct dialog with the server
The source need /safeseh to work without too many problems

Quote
HTTP/1.1 200 OK
Cache-Control: private, max-age=0
Date: Wed, 25 Aug 2021 07:06:12 GMT
Transfer-Encoding: chunked
Content-Type: text/html; charset=utf-8
Expires: Wed, 25 Aug 2021 07:05:12 GMT
P3P: CP="NON UNI COM NAV STA LOC CURa DEVa PSAa PSDa OUR IND"
Set-Cookie: MUID=3FA559A961F86FB23B394937604C6ED6; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: MUIDB=3FA559A961F86FB23B394937604C6ED6; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/; HttpOnly
Set-Cookie: _EDGE_S=F=1&SID=2AC07C33CB67613A17F96CADCAD3607A; domain=.bing.com; path=/; HttpOnly
Set-Cookie: _EDGE_V=1; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/; HttpOnly
Set-Cookie: SRCHD=AF=NOFORM; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHUID=V=2&GUID=135D188FB00C49DA9E193E214FB23495&dmnchg=1; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHUSR=DOB=20210825; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: SRCHHPGUSR=SRCHLANG=fr; domain=.bing.com; expires=Mon, 19-Sep-2022 07:06:12 GMT; path=/
Set-Cookie: _SS=SID=2AC07C33CB67613A17F96CADCAD3607A; domain=.bing.com; path=/
X-SNR-Routing: 1
X-Cache: CONFIG_NOCACHE
X-MSEdge-Ref: Ref A: DFC8A63D54344001AF7071FB13997BE4 Ref B: PRAEDGE0919 Ref C: 2021-08-25T07:06:12Z


Hi TouEnMasm,

Windows7_SDK_x64
:arrow_down:
Title: Re: Winhttp asynchronous
Post by: bomz on August 28, 2021, 12:24:11 PM
MSDN say 12175 error get more information from callback function
how interpret this number 246FD64 ?
Quote
   Аh\Л Internet Status: 10000 Data: 246FD64
12175
Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 28, 2021, 03:57:04 PM
LiaoMi  :thumbsup:
QuoteMany thanks for the samples in c
bomz
Quotehow interpret this number 246FD64 ?
use the GetWinHttpError function given in the source code.

I suspect that the winhttp need a proxy to work https://docs.microsoft.com/en-us/archive/msdn-magazine/2008/august/windows-with-c-asynchronous-winhttp (https://docs.microsoft.com/en-us/archive/msdn-magazine/2008/august/windows-with-c-asynchronous-winhttp)
I will study all that and hope to get an answer
Title: Re: Winhttp asynchronous
Post by: bomz on August 28, 2021, 05:34:07 PM
your internal GetWinHttpError function return 12175, winhttpcallback back 4 bytes - 246FD64h  (38206820), which more look like part of some text string
Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 28, 2021, 09:21:39 PM

An error can be describe in winerror.h in decimal or hexadecimal value (just to made things more simple !)
I get L'identificateur de pool de média ne représente pas un pool de média valide.     (2F8F = 12175)
BAD MEDIA



Title: Re: Winhttp asynchronous
Post by: bomz on August 28, 2021, 09:30:56 PM
QuoteERROR_WINHTTP_SECURE_FAILURE

12175

One or more errors were found in the Secure Sockets Layer (SSL) certificate sent by the server. To determine what type of error was encountered, check for a WINHTTP_CALLBACK_STATUS_SECURE_FAILURE notification in a status callback function. For more information, see WINHTTP_STATUS_CALLBACK.
Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 29, 2021, 02:24:44 AM
Finally found a bad use of the WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE event.
The WinHttpQueryHeaders function can do manything like download the headers .
The good answer is to just get the informations he give and let's go.
Further macro had been added to simplify the code.
I have updated the source in the first post
Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 29, 2021, 03:23:20 PM
The macro to enter a crical section nee to be modify :

;*******************************************************************
CSER MACRO fonction:REQ,parameters:VARARG
local etiquette
change CATSTR <">,<fonction>,<",0>
.data
etiquette db change
.code
@@:
invoke TryEnterCriticalSection,addr CriticalSection ;------- +1
.if eax == 1
invoke fonction,parameters
.if eax==0
.if dword ptr [phrase] != 0
invoke printf,addr phrase
.endif
invoke GetWinHttpError,addr etiquette
mov hr,0
.else
mov hr,eax
.endif
invoke LeaveCriticalSection,addr CriticalSection ;------ -1 ,always 0 like that
.else
;inc echecenter
invoke Sleep,10
jmp @B
.endif
EXITM <>
ENDM
;*******************************************************************

The TryEnterCriticalSection function give a better stabiity.The Number of failed to enter are beetween 0,2 .

The WINHTTP_CALLBACK_STATUS_REQUEST_ERROR event need also a modify:
add this line at the end : invoke SetEvent,Hevent <<<<<< stop the wait loop
This is needed when a site don't accept the http  (I have try it on my site)

STATUS_REQUEST_ERROR                  <<<< coming from  WINHTTP_CALLBACK_STATUS_REQUEST_ERROR  event
ERROR_WINHTTP_INVALID_SERVER_RESPONSE        <<<<<<<< the fault
<Status:Request error exit
Ha Status: sendrequest complete
WinHttpQueryDataAvailableERROR_WINHTTP_INCORRECT_HANDLE_STATE
<Ha Status: sendrequest complete
Nombre Echec critical :0
Status: Handle Closing
Status: Handle Closing


In the init proc,change LeaveCriticalSection by DeleteCriticalSection

Title: Re: Winhttp asynchronous
Post by: TouEnMasm on August 31, 2021, 02:22:35 AM

Added the redirect event who allow to download all url.
added the report for the HTTP_STATUS.

Title: Re: Winhttp asynchronous
Post by: TouEnMasm on September 02, 2021, 11:13:53 PM
At last,it seem to be perfect.
I have found two functions who don't work the same way as in synchronous mode.
Quote
WinHttpQueryDataAvailable
WinHttpReadData
For thos two functions the  lpdwNumberOfBytesRead must be set to NULL.
If the parameter is not NULL,this producr garbage in the dowloded file.
The WinHttpCrackUrl is the function who give the correct port to use in the WinHttpConnect. (url_components.nPort).
The url redirection failed if the correct port is not in use.
In this case,the WINHTTP_CALLBACK_STATUS_REQUEST_ERROR  give the good advice,that is connection_error.
For each question asked to the server there is only one event who is really usable for the answer ,that is:
Quote
WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE          WinHttpQueryDataAvailable
WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE       WinHttpReceiveResponse
WINHTTP_CALLBACK_STATUS_READ_COMPLETE           WinHttpReadData
WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE      WinHttpSendRequest
WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE         WinHttpWriteData
WINHTTP_CALLBACK_STATUS_REQUEST_ERROR         Any of the above functions when an error occurs.

The WinHttpReadData used in the WINHTTP_CALLBACK_STATUS_DATA_AVAILABLE must be execute as fast as possible and there is need
not to add too many code in the event.

The new dowload is in the first post
The synchrone sample as been also modify to show the callback (only usable for informations)