Here is something I use in my own creation of a web browser, still WIP.
Code: Select all
Constructor Cook()
'load in cookies
'ReDim CookieArray(0)
If FileExists(ExePath + "/Cookie.txt") Then
Dim As Integer ff = FreeFile
Open ExePath + "/Cookie.txt" For Input As #ff
While Eof(ff) = 0
ReDim Preserve CookieArray(CookieCount)
Input #ff, CookieArray(CookieCount).Host
Input #ff, CookieArray(CookieCount).Path
Input #ff, CookieArray(CookieCount).CookName
Input #ff, CookieArray(CookieCount).CookValue
Input #ff, CookieArray(CookieCount).Expires
'perm or non expired?
If IsCurrent(CookieCount) = 1 Then
'keep
CookieCount += 1
EndIf
Wend
Close #ff
EndIf
End Constructor
Destructor Cook()
'save non expired cookies
If FileExists(ExePath + "/Cookie.txt") Then Kill(ExePath + "/Cookie.txt")
If CookieCount Then
Dim As Integer x, Valid, ff
'any "Valid"
For x = 0 To CookieCount - 1
If IsCurrent(x) = 1 And CookieArray(x).CookValue <> "" Then Valid += 1
Next
If Valid Then
ff = FreeFile
Open ExePath + "/Cookie.txt" For Output As #ff
For x = 0 To CookieCount - 1
If IsCurrent(x) = 1 And CookieArray(x).CookValue <> "" Then
Print #ff, CookieArray(x).Host
Print #ff, CookieArray(x).Path
Print #ff, CookieArray(x).CookName
Print #ff, CookieArray(x).CookValue
Print #ff, CookieArray(x).Expires
EndIf
Next
EndIf
EndIf
End Destructor
Function Cook.SendCookie(Host As String, Path As String) As String
If CookieCount = 0 Then Return ""
Dim As String R = "" 'return value
Dim As String CookieList = "" 'listing of cookies to add
Dim As Integer x, S
Dim As String PathOnly
'Cookie: <name>=<value> [;<name>=<value>]...
While x < CookieCount
If IsCurrent(x) Then
If CookieArray(x).Host = Host Then
'exact match, check path
If Left(Path, Len(CookieArray(x).Path)) = CookieArray(x).Path Then
'Path match
S = 1
Else
S = 0
EndIf
Else
'check for partal match (global host)
If Right(Host, Len(CookieArray(x).Host)) = CookieArray(x).Host Then
S = 1 'flag to add
Else
S = 0 'do not add
EndIf
EndIf
If S > 0 And Len(CookieArray(x).CookValue) > 0 Then
'add
If Len(CookieList) Then CookieList += ";"
CookieList += CookieArray(x).CookName + "=" + CookieArray(x).CookValue
EndIf
EndIf
x += 1
Wend
If Len(CookieList) Then R = "Cookie: " + CookieList 'add this to header
Return R
End Function
Sub Cook.RecCookie(Header As String, Host As String, Path As String)
Dim As Integer CookieStart = InStr(Header, "Set-Cookie:")
Dim As Integer S, F
Dim As CookData TempCookie
Dim As String CookiePart
'
While CookieStart
S = InStr(CookieStart, Header, Chr(13)) 'end of set-cookie
CookiePart = Mid(Header, CookieStart, S - CookieStart)
'Cookie name
S = InStr(CookiePart, " ") + 1
F = InStr(S, CookiePart, "=")
TempCookie.CookName = Mid(CookiePart, S, F - S)
'Cookie value
S = F + 1
F = InStr(S, CookiePart, ";")
TempCookie.CookValue = Mid(CookiePart, S, F - S)
'expires
S = InStr(CookiePart, "expires=")
If S Then
'has expire
S += 8
F = InStr(S, CookiePart, ";")
TempCookie.Expires = HttpDate(Mid(CookiePart, S, F - S)) 'GMT
Else
TempCookie.Expires = 0 'permanent or session
EndIf
'host
S = InStr(CookiePart, "domain=")
If S Then
'has domain
S += 7
F = InStr(S, CookiePart, ";")
TempCookie.Host = Mid(CookiePart, S, F - S)
Else
'SEE COMMENTS AT END OF FILE
'session cookie
TempCookie.Host = Host
TempCookie.Expires = -1 'mark as so
EndIf
'path spec for host
S = InStr(CookiePart, "path=")
If S Then
'has path
S += 5
F = InStr(S, CookiePart, ";")
TempCookie.Path = Mid(CookiePart, S, F - S)
Else
'SEE COMMENTS AT END OF FILE
S = InStrRev(Path, "/")
If S Then
TempCookie.Path = Left(Path, S)
Else
TempCookie.Path = "/"
EndIf
EndIf
'search cookie array for one already set
If CookieCount Then
For S = 0 To CookieCount - 1
If TempCookie.CookName = CookieArray(S).CookName And TempCookie.Host = CookieArray(S).Host And TempCookie.Path = CookieArray(S).Path Then
CookieArray(S).CookValue = TempCookie.CookValue
CookieArray(S).Expires = TempCookie.Expires
Exit For
EndIf
Next
Else
S = 0
EndIf
If S = CookieCount Then
'add
ReDim Preserve CookieArray(CookieCount)
CookieArray(S).Host = TempCookie.Host
CookieArray(S).Path = TempCookie.Path
CookieArray(S).CookName = TempCookie.CookName
CookieArray(S).CookValue = TempCookie.CookValue
CookieArray(S).Expires = TempCookie.Expires
CookieCount += 1
EndIf
CookieStart = InStr(CookieStart + 5, Header, "Set-Cookie:")
Wend
End Sub
Function Cook.IsCurrent(Index As Integer) As Integer
If CookieArray(Index).Expires < 0 Then Return 2 'Session cookie
If CookieArray(Index).Expires = 0 Then Return 1 'permenant cookie
If CookieArray(Index).Expires > Now + TimeSerial(0, LocalTimeBias, 0) Then Return 1 'not expired
Return 0 'EXPIRED
End Function
/'
Specifying the domain name, using the pattern domain=domain_name, is optional for persistent cookies and is used to indicate the end of the domain for which the cookie is valid.
Session cookies that specify a domain are rejected.
If the specified domain name ending matches the request, the cookie tries to match the path to determine if the cookie should be sent.
For example, if the domain name ending is .microsoft.com, requests to home.microsoft.com and support.microsoft.com would be checked to see if the specified pattern matches the request.
The domain name must have at least two or three periods in it to prevent cookies from being set for widely used domain name endings, such as .com, .edu, and co.jp.
Allowable domain names would be similar to .microsoft.com, .someschool.edu, and .someserver.co.jp. Only hosts within the specified domain can set a cookie for a domain.
Setting the path, using the pattern path=some_path, is optional and can be used to specify a subset of the URLs for which the cookie is valid.
If a path is specified, the cookie is considered valid for any requests that match that path.
For example, if the specified path is /example, requests with the paths /examplecode and /example/code.htm would match.
If no path is specified, the path is assumed to be the path of the resource associated with the Set-Cookie header.
'/