msgbox

HttpQueryFix

Jun 25th, 2011
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. loop
  2. {
  3.    httpQuery(html:="",URL:="http://volk-blog.ru:8080/500/")
  4.    varSetCapacity(html,-1)
  5.     if html contains 500 Internal Server Error
  6.     {
  7.     TrayTip, 500 Internal Server Error, sleep 2000
  8.     sleep, 2000
  9.     MsgBox, Request #%A_Index% not sended..
  10.     }else
  11.    MsgBox, Request #%A_Index% sended..
  12. }
  13.  
  14.  
  15. httpQuery(byref Result, lpszUrl, POSTDATA="", HEADERS="")
  16. {   global httpQueryOps, httpAgent, httpProxy, httpProxyByPass, httpQueryReferer, httpQueryAcceptType
  17.        , httpQueryDwFlags
  18.  
  19.    defaultOps =
  20.    (LTrim Join|
  21.       httpAgent=script|httpProxy=0|httpProxyByPass=0|INTERNET_FLAG_SECURE=0x00800000
  22.       SECURITY_FLAG_IGNORE_UNKNOWN_CA=0x00000100|SECURITY_FLAG_IGNORE_CERT_CN_INVALID=0x00001000
  23.       SECURITY_FLAG_IGNORE_CERT_DATE_INVALID=0x00002000|SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE=0x00000200
  24.       INTERNET_OPEN_TYPE_PROXY=3|INTERNET_OPEN_TYPE_DIRECT=1|INTERNET_SERVICE_HTTP=3
  25.    )
  26.    Loop,Parse,defaultOps,|
  27.    {
  28.       RegExMatch(A_LoopField,"(?P<Option>[^=]+)=(?P<Default>.*)",http)
  29.       if StrLen(%httpOption%)=0
  30.          %httpOption% := httpDefault
  31.    }
  32.  
  33.    hModule := DllCall("LoadLibrary", "Str", "WinINet.Dll")
  34.  
  35.    offset_name_length:= "4-lpszScheme-255|16-lpszHostName-1024|28-lpszUserName-1024|"
  36.                   . "36-lpszPassword-1024|44-lpszUrlPath-1024|52-lpszExtrainfo-1024"
  37.    VarSetCapacity(URL_COMPONENTS,60,0)
  38.    NumPut(60,URL_COMPONENTS,0), NumPut(255,URL_COMPONENTS,12), NumPut(0xffff,URL_COMPONENTS,24)
  39.    
  40.    Loop,Parse,offset_name_length,|
  41.    {
  42.       RegExMatch(A_LoopField,"(?P<Offset>\d+)-(?P<Name>[a-zA-Z]+)-(?P<Size>\d+)",iCU_)
  43.       VarSetCapacity(%iCU_Name%,iCU_Size,0)
  44.       NumPut(&%iCU_Name%,URL_COMPONENTS,iCU_Offset)
  45.       NumPut(iCU_Size,URL_COMPONENTS,iCU_Offset+4)
  46.    }
  47.  
  48.    DllCall("WinINet\InternetCrackUrlA","Str",lpszUrl,"uInt",StrLen(lpszUrl),"uInt",0,"uInt",&URL_COMPONENTS)
  49.  
  50.    Loop,Parse,offset_name_length,|
  51.    {
  52.       RegExMatch(A_LoopField,"-(?P<Name>[a-zA-Z]+)-",iCU_)
  53.       VarSetCapacity(%iCU_Name%,-1)
  54.    }
  55.    nPort:=NumGet(URL_COMPONENTS,24,"uInt")
  56.    
  57.    dwFlags := httpQueryDwFlags
  58.    if (lpszScheme = "https")
  59.       dwFlags |= (INTERNET_FLAG_SECURE|SECURITY_FLAG_IGNORE_CERT_CN_INVALID
  60.                |SECURITY_FLAG_IGNORE_CERT_WRONG_USAGE)
  61.  
  62.    if (lpszScheme="unknown") {
  63.       Result := "ERR: No Valid URL supplied."
  64.       Return StrLen(Result)
  65.    }
  66.  
  67.    hInternet := DllCall("WinINet\InternetOpenA"
  68.                   ,"Str",httpAgent,"UInt"
  69.                   ,(httpProxy != 0 ?  INTERNET_OPEN_TYPE_PROXY : INTERNET_OPEN_TYPE_DIRECT )
  70.                   ,"Str",httpProxy,"Str",httpProxyBypass,"Uint",0)
  71.  
  72.    hConnect := DllCall("WinINet\InternetConnectA"
  73.                   ,"uInt",hInternet,"Str",lpszHostname, "Int",nPort
  74.                   ,"Str",lpszUserName, "Str",lpszPassword,"uInt",INTERNET_SERVICE_HTTP
  75.                   ,"uInt",0,"uInt*",0)
  76.  
  77.    if (Strlen(POSTDATA)>0) {
  78.       HTTPVerb:="POST"
  79.       if StrLen(Headers)=0
  80.          Headers:="Content-Type: application/x-www-form-urlencoded"
  81.    } else
  82.       HTTPVerb:="GET"    
  83.  
  84.    hRequest := DllCall("WinINet\HttpOpenRequestA"
  85.                   ,"uInt",hConnect,"Str",HTTPVerb,"Str",lpszUrlPath . lpszExtrainfo
  86.                   ,"Str",ProVer := "HTTP/1.1", "Str",httpQueryReferer,"Str",httpQueryAcceptTypes
  87.                   ,"uInt",dwFlags,"uInt",Context:=0 )
  88.  
  89.    sRequest := DllCall("WinINet\HttpSendRequestA"
  90.                   , "uInt",hRequest,"Str",Headers, "uInt",Strlen(Headers)
  91.                   , "Str",POSTData,"uInt",Strlen(POSTData))
  92.  
  93.    VarSetCapacity(header, 2048, 0)  ; max 2K header data for httpResponseHeader
  94.    VarSetCapacity(header_len, 4, 0)
  95.    
  96.    Loop, 5
  97.      if ((headerRequest:=DllCall("WinINet\HttpQueryInfoA","uint",hRequest
  98.       ,"uint",21,"uint",&header,"uint",&header_len,"uint",0))=1)
  99.       break
  100.  
  101.    If (headerRequest=1) {
  102.       VarSetCapacity(res,headerLength:=NumGet(header_len),32)
  103.       DllCall("RtlMoveMemory","uInt",&res,"uInt",&header,"uInt",headerLength)
  104.       Loop,% headerLength
  105.          if (*(&res-1+a_index)=0) ; Change binary zero to linefeed
  106.             NumPut(Asc("`n"),res,a_index-1,"uChar")
  107.       VarSetCapacity(res,-1)
  108.    } else
  109.       res := "timeout"
  110.  
  111.    Loop,Parse,res,`n,`r
  112.    {
  113.       RetValue := A_LoopField
  114.       break
  115.    }
  116.    
  117.    If (RetValue="timeout") {
  118.       html := "Error: timeout"
  119.       return -1
  120.    }
  121.    RetValue := RegExReplace(RetValue,"HTTP/1\.[01]\s+")
  122.    
  123.    RetValue := SubStr(RetValue,1,3)
  124.    
  125.    if strlen(HTTPQueryOps)>0 {
  126.       if (instr(HTTPQueryOps,"showHeader"))
  127.          MsgBox % res
  128.       if (instr(HTTPQueryOps,"storeHeader"))
  129.          global HttpQueryHeader := res
  130.       if (instr(HTTPQueryOps,"updateSize")) {
  131.          Loop,Parse,res,`n
  132.             If RegExMatch(A_LoopField,"Content-Length:\s+?(?P<Size>\d+)",full) {
  133.                global HttpQueryFullSize := fullSize
  134.                break
  135.             }
  136.          if (fullSize+0=0)
  137.             HttpQueryFullSize := "size unavailable"
  138.       }
  139.    }
  140.  
  141.    if !(InStr("100 200 201 202 302 500",RetValue)) {
  142.       Result := RetValue " " RetMsg
  143.       return StrLen(Result)
  144.    }
  145.  
  146.    VarSetCapacity(BytesRead,4,0)
  147.    fsize := 0
  148.    Loop            
  149.    {  bc := A_Index
  150.       VarSetCapacity(buffer%bc%,1024,0) ; setup new chunk for this receive round
  151.       ReadFile := DllCall("wininet\InternetReadFile"
  152.                   ,"uInt",hRequest,"uInt",&buffer%bc%,"uInt",1024,"uInt",&BytesRead)
  153.       ReadBytes := NumGet(BytesRead)  
  154.       If ((ReadFile!=0)&&(!ReadBytes))  
  155.          break                      
  156.       Else {
  157.          fsize += ReadBytes  
  158.          sizeArray .= ReadBytes "|"
  159.       }
  160.       if (instr(HTTPQueryOps,"updateSize"))
  161.          Global HttpQueryCurrentSize := fsize
  162.    }
  163.    sizeArray := SubStr(sizeArray,1,-1)  
  164.    
  165.    VarSetCapacity(result,fSize+1,0)      
  166.    Dest := &result            
  167.    Loop,Parse,SizeArray,|
  168.       DllCall("RtlMoveMemory","uInt",Dest,"uInt",&buffer%A_Index%,"uInt",A_LoopField)
  169.       , Dest += A_LoopField
  170.      
  171.    DllCall("WinINet\InternetCloseHandle", "uInt", hRequest)  
  172.    DllCall("WinINet\InternetCloseHandle", "uInt", hInternet)
  173.    DllCall("WinINet\InternetCloseHandle", "uInt", hConnect)
  174.    DllCall("FreeLibrary", "UInt", hModule)
  175.  
  176.  
  177.              
  178.    return fSize
  179. }
Advertisement
Add Comment
Please, Sign In to add comment