AutoHotkey Community

It is currently May 27th, 2012, 9:32 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next
Author Message
PostPosted: June 15th, 2006, 8:07 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
Thanks to PhiLho I learned how to parse buffers with binary 0 seperated contents...

Code:
/* ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; HttpQueryInfo ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
QueryInfoFlag:

HTTP_QUERY_RAW_HEADERS = 21
Receives all the headers returned by the server.
Each header is terminated by "\0". An additional "\0" terminates the list of headers.

HTTP_QUERY_CONTENT_LENGTH = 5
Retrieves the size of the resource, in bytes.

HTTP_QUERY_CONTENT_TYPE = 1
Receives the content type of the resource (such as text/html).

Find more at: http://msdn.microsoft.com/library/en-us/wininet/wininet/query_info_flags.asp

Proxy Settings:

INTERNET_OPEN_TYPE_PRECONFIG                    0   // use registry configuration
INTERNET_OPEN_TYPE_DIRECT                       1   // direct to net
INTERNET_OPEN_TYPE_PROXY                        3   // via named proxy
INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY  4   // prevent using java/script/INS

*/ ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

HttpQueryInfo(URL, QueryInfoFlag=21, Proxy="", ProxyBypass="") {
hModule := DllCall("LoadLibrary", "str", "wininet.dll")

If (Proxy != "")
AccessType=3
Else
AccessType=1

io_hInternet := DllCall("wininet\InternetOpenA"
, "str", "" ;lpszAgent
, "uint", AccessType
, "str", Proxy
, "str", ProxyBypass
, "uint", 0) ;dwFlags
If (ErrorLevel != 0 or io_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}

iou_hInternet := DllCall("wininet\InternetOpenUrlA"
, "uint", io_hInternet
, "str", url
, "str", "" ;lpszHeaders
, "uint", 0 ;dwHeadersLength
, "uint", 0x80000000 ;dwFlags: INTERNET_FLAG_RELOAD = 0x80000000 // retrieve the original item
, "uint", 0) ;dwContext
If (ErrorLevel != 0 or iou_hInternet = 0) {
DllCall("FreeLibrary", "uint", hModule)
return, -1
}

VarSetCapacity(buffer, 1024, 0)
VarSetCapacity(buffer_len, 4, 0)

Loop, 5
{
  hqi := DllCall("wininet\HttpQueryInfoA"
  , "uint", iou_hInternet
  , "uint", QueryInfoFlag ;dwInfoLevel
  , "uint", &buffer
  , "uint", &buffer_len
  , "uint", 0) ;lpdwIndex
  If (hqi = 1) {
    hqi=success
    break
  }
}

IfNotEqual, hqi, success, SetEnv, res, timeout

If (hqi = "success") {
p := &buffer
Loop
{
  l := DllCall("lstrlen", "UInt", p)
  VarSetCapacity(tmp_var, l+1, 0)
  DllCall("lstrcpy", "Str", tmp_var, "UInt", p)
  p += l + 1 
  res := res  . "`n" . tmp_var
  If (*p = 0)
  Break
}
StringTrimLeft, res, res, 1
}

DllCall("wininet\InternetCloseHandle",  "uint", iou_hInternet)
DllCall("wininet\InternetCloseHandle",  "uint", io_hInternet)
DllCall("FreeLibrary", "uint", hModule)

return, res
}


Edit: Added a timeout after 5 unsuccessful calls to HttpQueryInfoA.
Edit: Replaced "res := res "`n" . tmp_var" with "res := res . "`n" . tmp_var"
Edit: Made lpszAgent blank


Last edited by olfen on June 16th, 2006, 7:15 pm, edited 4 times in total.

Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 15th, 2006, 8:36 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
I don't have to rely so much on the Firefox Web Developerextension anymore, thanks :)

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2006, 10:46 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
olfen is becoming the WinINET specialist. :-)
Very useful and educational scripts, thanks.
Also thanks for the credit. ^_^'

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: June 16th, 2006, 7:14 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
PhiLho wrote:
olfen is becoming the WinINET specialist. :-)

You may call me a beginner when I got GetRTTAndHopCount (vulgo "ping") working :)
What I find very regrettable about these DllCalls is the fact, that they cannot be executed as seperate threads!

Edit: Oops, GetRTTAndHopCount is in Iphlpapi.dll :oops:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 26th, 2006, 3:30 pm 
Offline

Joined: June 27th, 2006, 2:38 pm
Posts: 385
Location: Canada
Is it possible to host connections using this?


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 9:24 am 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
I still don't understand what you mean by "host connections"...
But then, I am not a network specialist.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 11:58 am 
Offline

Joined: July 12th, 2005, 1:21 pm
Posts: 633
Would it be too much to give me a short explanation what I can do with this function?

Thalon

_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 12:08 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
It is all in the header:
Quote:
HTTP_QUERY_RAW_HEADERS = 21
Receives all the headers returned by the server.
Each header is terminated by "\0". An additional "\0" terminates the list of headers.

HTTP_QUERY_CONTENT_LENGTH = 5
Retrieves the size of the resource, in bytes.

HTTP_QUERY_CONTENT_TYPE = 1
Receives the content type of the resource (such as text/html).

For example:
Code:
url1 = http://www.autohotkey.com
url2 = http://www.autohotkey.com/download/AutoHotkeyInstall.exe
MsgBox % HttpQueryInfo(url1)
MsgBox % HttpQueryInfo(url2, 5)
MsgBox % HttpQueryInfo(url2, 1)
will return:
Quote:
HTTP/1.1 200 OK
Date: Mon, 28 Aug 2006 11:04:51 GMT
Server: Apache/2.0.54 (Fedora)
Last-Modified: Sun, 27 Aug 2006 16:52:38 GMT
ETag: "21029b-2ba0-a30ea180"
Accept-Ranges: bytes
Content-Length: 11168
Content-Type: text/html
---------------------------
1749587
---------------------------
application/octet-stream
Extremly useful!

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 12:25 pm 
Offline

Joined: July 12th, 2005, 1:21 pm
Posts: 633
Yes, I read the header, but it doesn't say me a word :D
With your explanation it is easier to understand for those who hadn't to do with it before...

Would it be possible to add Username/Password-Option for Proxy?

Thx,
Thalon

_________________
AHK-Icon-Changer
AHK-IRC
deutsches Forum


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 12:52 pm 
Offline

Joined: December 27th, 2005, 1:46 pm
Posts: 6837
Location: France (near Paris)
Well, looking at InternetOpen function, I see no way to give these. Perhaps it needs another function.
Note that AccessType=0 indicates to use the registry settings (basically IE's settings), so it probably does what you want.

_________________
Image vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 8:02 pm 
Offline

Joined: August 8th, 2006, 3:55 pm
Posts: 49
Quote:
Thalon:

Would it be too much to give me a short explanation what I can do with this function?

PhiLho, thanks to your explanations I got an understanding of the function HttpQueryInfo (although it is all in the header).

The best method is: copy & paste & run (without error)


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: August 28th, 2006, 9:52 pm 
Offline
User avatar

Joined: August 11th, 2004, 1:47 am
Posts: 5347
Location: UK
Thalon wrote:
Would it be possible to add Username/Password-Option for Proxy?
The standard HttpQueryInfo("http://user:pass@uri", 21, "proxy", "bypass") should work.

_________________
GitHubScriptsIronAHK Contact by email not private message.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 27th, 2006, 6:14 pm 
Offline

Joined: March 2nd, 2004, 3:36 pm
Posts: 10720
This is a great script and seems like it would add a lot of value if added to the UrlDownloadToFile page in the documenation. If olfen or anyone else has the time and interest, I could use a volunteer to help adapt this for the documentation:

- Polish it up a little, such as providing more traditional indentation and adding a few more comments (such as why there's a loop that retries the request 5 times).

- Verify what version of MSIE and Windows is needed, and mention it in the comments (maybe it works on all).

- Consider whether there's any way to make it even easier to use: can the parameters be made more friendly; can using a proxy be made easier or explained better; comment on this script's relationship with MSIE's internal settings (if any); and anything else you can think of.

If no one has the time or interest to do this, I'll take care of it at a later time. Thanks to everyone who contributed to this script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: September 30th, 2006, 8:49 pm 
Offline

Joined: June 4th, 2005, 1:30 am
Posts: 113
Location: Stuttgart, Germany
Thanks for considering adding the script to the documentation.
However, due to lack of time, it might well be 2 or 3 weeks, until I get to overhaul and comment the script.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: November 17th, 2006, 2:01 am 
Offline

Joined: September 12th, 2005, 2:12 am
Posts: 190
It should be noted that this script returns the header information for the final destination and not necessarily the header of the specified url.

Is it possible to modify this script to properly capture the header from an url with a Location:(url) redirect?


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 21 posts ]  Go to page 1, 2  Next

All times are UTC [ DST ]


Who is online

Users browsing this forum: Google [Bot], Google Feedfetcher, nomissenrojb, SKAN, Stigg, Yahoo [Bot] and 18 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
cron
Powered by phpBB® Forum Software © phpBB Group