home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / PSC_Multi-53447262002.psc / modHTTP.bas < prev    next >
Encoding:
BASIC Source File  |  2002-02-03  |  9.1 KB  |  284 lines

  1. Attribute VB_Name = "modHTTP"
  2. Option Explicit
  3.  
  4. 'this module will hold generic http functions...
  5.  
  6. Public Function GetHttpResponseCode(strHeader As String) As Long
  7. 'this routine parses a string header and returns an error code
  8. Dim varCode As String
  9.  
  10. varCode = Mid(strHeader, InStr(1, strHeader, " ") + 1, 3)
  11. If IsNumeric(varCode) Then
  12.     GetHttpResponseCode = CInt(varCode)
  13. End If
  14.  
  15. End Function
  16.  
  17.  
  18. Public Function createDir(dirName As String) As String
  19. 'this routine will create a folder with the name of the html file
  20. 'and return the name and path of the newly created directory
  21. Dim i As Long
  22. Dim sTemp As String
  23.  
  24. If Len(dir$(dirName, vbDirectory)) = 0 Then
  25.     MkDir dirName
  26. Else
  27.     sTemp = dirName
  28.     For i = 1 To 1000 'the number of folders that can have the same name
  29.         If Len(dir$(sTemp & "." & i, vbDirectory)) = 0 Then
  30.             MkDir sTemp & "." & i
  31.             dirName = sTemp & "." & i
  32.             Exit For
  33.         End If
  34.     Next 'i
  35. End If
  36.  
  37. createDir = dirName
  38. End Function
  39.  
  40.  
  41. Public Function findDownloadLink(html As String) As String
  42. Dim posStart As Long, posEnd As Long, pos As Long
  43. Dim sLink As String
  44.  
  45. '<a href="/vb/scripts/ShowZip.asp?lngWId=1&lngCodeId=30651&strZipAccessCode=PDAT306514112"><img border="0" src="/vb/images//winzipicon_medium.gif" alt="winzip icon" width="42" height="41">Download code</a>
  46.  
  47. pos = InStr(1, html, "Download code", vbTextCompare) 'this will give us a position after the download link...
  48.  
  49. If pos = 0 Then
  50.     findDownloadLink = "No Link Found"
  51.     Exit Function
  52. End If
  53.  
  54. posStart = InStrRev&(html, "<a href", pos, vbTextCompare)
  55. posStart = posStart + 7
  56. posEnd = InStr(posStart, html, ">", vbTextCompare) 'this will give us the ending position
  57.  
  58. sLink = Mid$(html, posStart, posEnd - posStart)
  59. sLink = Replace$(sLink, Chr$(34), "")
  60.  
  61. pos = InStr(1, sLink, "=", vbTextCompare)
  62. sLink = Mid$(sLink, pos + 1, Len(sLink))
  63. sLink = Trim$(sLink)
  64.  
  65. findDownloadLink = sLink
  66. End Function
  67.  
  68. Public Function contentLength(sHeader As String) As Long
  69. 'this function finds the "Content-Length:" in the header and returns the
  70. 'number of bytes
  71. Dim startPos As Long
  72. Dim endPos As Long
  73. Dim i As Long
  74. Dim upper As Long
  75.  
  76. startPos = InStr(1, sHeader, "Content-Length:", vbTextCompare)
  77. startPos = startPos + Len("Content-Length: ")
  78.  
  79. endPos = startPos
  80. upper = Len(sHeader)
  81. For i = 1 To upper
  82.     If IsNumeric(Mid$(sHeader, endPos, 1)) Then
  83.         endPos = endPos + 1
  84.     Else
  85.         Exit For
  86.     End If
  87. Next i
  88.  
  89. contentLength = CLng(Trim$(Mid$(sHeader, startPos, endPos - startPos)))
  90.  
  91. End Function
  92.  
  93. Public Function contentType(sHeader As String) As String
  94. 'this function is used to determine what type of content is being downloaded
  95. 'if the content type is: Content-Type: application/x-zip-compressed then return "File"
  96. 'if it is: Content-Type: text/html then return "HTML"
  97. Dim startPos As Long
  98. Dim endPos As Long
  99. Dim i As Long
  100. Dim upper As Long
  101.  
  102. startPos = InStr(1, sHeader, "Content-Type:", vbTextCompare)
  103. startPos = startPos + Len("Content-Type: ")
  104.  
  105. endPos = InStr(startPos, sHeader, "/", vbTextCompare)
  106.  
  107. contentType = Trim$(Mid$(sHeader, startPos, endPos - startPos))
  108.  
  109. If StrComp(contentType, "text", vbTextCompare) = 0 Then
  110.     contentType = "HTML"
  111.     Exit Function
  112. End If
  113.  
  114. If StrComp(contentType, "application", vbTextCompare) = 0 Then
  115.     'we know it is an application, but is it a zip
  116.     If Mid$(sHeader, endPos + 1, Len("x-zip-compressed")) = "x-zip-compressed" Then
  117.     contentType = "File"
  118.     Exit Function
  119.     End If
  120. End If
  121.  
  122. contentType = "Unknown"
  123.  
  124. End Function
  125.  
  126. 'Private Function getHTMLDocumentTitle(html As String) As String
  127. ''this function will return the document title ie the words between <title> </title>
  128. ''html - the html document
  129. 'Dim posStart As Long
  130. 'Dim posEnd As Long
  131. 'posStart = InStr(1, html, "<title>", vbTextCompare) 'this will give us the starting position
  132. 'posEnd = InStr(1, html, "</title>", vbTextCompare) 'this will give us the ending position
  133. '
  134. 'If posStart = 0 Or posEnd = 0 Then 'a webpage with no title has been downloaded
  135. '    getDocumentTitle = "Unknown"
  136. '    Exit Function
  137. 'End If
  138. '
  139. 'posStart = posStart + 7 '7-is the number of chars in <title>
  140. '
  141. 'getDocumentTitle = Mid$(html, posStart, posEnd - posStart) 'now we need to strip out "- visual basic, vb, vbscript"
  142. 'getDocumentTitle = Left$(getDocumentTitle, InStr(1, getDocumentTitle, "- visual basic, vb, vbscript", vbTextCompare) - 1)
  143. 'getDocumentTitle = Trim$(getDocumentTitle)
  144. '
  145. 'End Function
  146.  
  147. Public Function createRequestHeader(theServer As String, htmlURL As String) As String
  148. Dim strRequestTemplate As String
  149.  
  150.  
  151.     
  152.  
  153.     strRequestTemplate = "GET _$-$_$- HTTP/1.0" & Chr(13) & Chr(10) & _
  154.     "Accept: text/html, image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/vnd.ms-powerpoint, application/vnd.ms-excel, application/msword, application/x-comet, */*" & Chr(13) & Chr(10) & _
  155.     "Accept-Language: en-ca" & Chr(13) & Chr(10) & _
  156.     "Accept-Encoding: gzip , deflate" & Chr(13) & Chr(10) & _
  157.     "User-Agent: Mozilla/4.0 (compatible; MSIE 1.0; Windows 3.11; PSC Rulez!!)" & _
  158.     "Cache-Control: no-cache" & Chr(13) & Chr(10) & _
  159.     "Connection: Keep-Alive" & Chr(13) & Chr(10) & _
  160.     "User-Agent: SSM Agent 1.0" & Chr(13) & Chr(10) & _
  161.     "Host: @$@@$@" & Chr(13) & Chr(10)
  162.  
  163. '    If strServerAddress = "" Or strDocumentURI = "" Then
  164. '        MsgBox "Unable To detect target page!", vbCritical + vbOK
  165. '        Exit Sub
  166. '    End If
  167.  
  168. '    If mblnIsProxyUsed Then
  169. '        strServerHostIP = txtProxy.Text
  170. '        mstrRequestHeader = strRequestTemplate
  171. '        mstrRequestHeader = Replace(mstrRequestHeader, "_$-$_$-", mstrURL)
  172. '        lngServerPort = 80
  173. '    Else
  174. '        strServerHostIP = strServerAddress
  175. '        lngServerPort = 80
  176. '        mstrRequestHeader = strRequestTemplate
  177. '        mstrRequestHeader = Replace(mstrRequestHeader, "_$-$_$-", strDocumentURI)
  178. '    End If
  179.  
  180.     'we are not worrying about a proxy for now
  181.  
  182.  
  183.  
  184.     createRequestHeader = strRequestTemplate
  185.     createRequestHeader = Replace(createRequestHeader, "_$-$_$-", htmlURL) 'the relative path to the file to be downloaded -if this was proxied then it would be the full url
  186.     createRequestHeader = Replace(createRequestHeader, "@$@@$@", theServer)
  187.     createRequestHeader = createRequestHeader & vbCrLf
  188.  
  189. End Function
  190.  
  191. Public Function extractHeader(sRaw As String) As String
  192. 'this function will extract the header from a partial download....
  193. 'this function will generally be called when a complete header is detected.
  194. 'sRAW - is the data that is pulled from the server
  195. Dim posStart As Long, posEnd As Long
  196.  
  197. 'find the vbcrlf & vbcrlf
  198. posEnd = InStr(1, sRaw, vbCrLf & vbCrLf)
  199.  
  200. posStart = 1
  201. 'if posend = posstart then 'there is a problem
  202.  
  203. extractHeader = Trim$(Left$(sRaw, posEnd - 1))
  204.  
  205. End Function
  206.  
  207. Public Function extractHTML(sRaw As String) As String
  208. 'this function is used to extract the html portion from
  209. 'the raw data sent by the server
  210. Dim posStart As Long
  211. Dim posEnd As Long
  212.  
  213. posStart = InStr(sRaw, vbCrLf & vbCrLf)
  214.  
  215. If posStart = 0 Then 'the html was not found
  216.     extractHTML = sRaw 'return the input string as the seperator was not found
  217.     'the error handling could be a little better
  218.     Exit Function
  219. End If
  220.  
  221. posStart = posStart + Len(vbCrLf & vbCrLf)
  222. posEnd = Len(sRaw) + 1
  223.  
  224.  
  225. extractHTML = Mid$(sRaw, posStart, posEnd - posStart)
  226.  
  227. End Function
  228.  
  229. Public Function getDocumentTitle(html As String) As String
  230. Dim posStart As Long
  231. Dim posEnd As Long
  232. posStart = InStr(1, html, "<title>", vbTextCompare) 'this will give us the starting position
  233. posEnd = InStr(1, html, "</title>", vbTextCompare) 'this will give us the ending position
  234.  
  235. If posStart = 0 Or posEnd = 0 Then 'a webpage with no title has been downloaded
  236.     getDocumentTitle = "Unknown Webpage"
  237.     Exit Function
  238. End If
  239.  
  240. posStart = posStart + Len("<title>")
  241.  
  242. getDocumentTitle = Mid$(html, posStart, posEnd - posStart) 'now we need to strip out "- visual basic, vb, vbscript"
  243. getDocumentTitle = Left$(getDocumentTitle, InStr(1, getDocumentTitle, "- visual basic, vb, vbscript", vbTextCompare) - 1)
  244. getDocumentTitle = Trim$(stripIllegalChars(getDocumentTitle))
  245. 'we need to strip out the special chars in the title if it contains any...
  246.  
  247.  
  248. End Function
  249.  
  250. Private Function stripIllegalChars(sTitle As String) As String
  251. '        ? [ ] / \ = + < > : ; " ,    are illegal characters for file names
  252. 'chr$(63) = ?
  253. 'chr$(91) = [
  254. 'chr$(93) = ]
  255. 'chr$(47) = /
  256. 'chr$(92) = \
  257. 'chr$(61) = =
  258. 'chr$(43) = +
  259. 'chr$(60) = <
  260. 'chr$(62) = >
  261. 'chr$(58) = :
  262. 'chr$(59) = ;
  263. 'chr$(34) = "
  264. 'chr$(44) = ,
  265. 'chr$(46) = .
  266. 'chr$(42) = *
  267. 'this funciton takes a string and strips out the illegal chars for a file name
  268. 'and replaces them with a space " "
  269. Dim i As Long, upper As Long
  270.  
  271. upper = Len(sTitle)
  272.  
  273. For i = 1 To upper
  274.  
  275. Select Case Mid$(sTitle, i, 1)
  276.     Case Chr$(63), Chr$(91), Chr$(93), Chr$(47), Chr$(92), Chr$(61), Chr$(43), Chr$(60), Chr$(62), Chr$(58), Chr$(59), Chr$(34), Chr$(44), Chr$(46), Chr$(42)
  277.         Mid$(sTitle, i, 1) = " " 'replace the illegal char with a space
  278. End Select
  279. Next 'i
  280. stripIllegalChars = sTitle
  281. End Function
  282.  
  283.  
  284.