I was looking for a small HTTP library fully written in PureBasic which is able to handle features like chunked data transfer encoding or gzip compression, but unfortunately most other user-written implementations only implement a very minimal set of features (or are simply broken) - so I decided to quickly write my own implementation in a few hours. I also found another bug in the network implementation, a workaround for this issue can be found below.
Supported features:
- HTTP 1.1/1.0
- GET/POST/HEAD requests
- Ascii/Unicode - 32/64 bit compatible!
- Linux / Windows support, Mac OS should work but I didn't test it.
- gzip/deflate support without any additional external libraries (uses zlib implementation bundled with PB)!
- POST: Both application/x-www-form-urlencoded (default) and multipart/form-data supported
- "chunked" Transfer-Encoding supported (used by Apache+PHP, so this is very important if you want to query your own PHP scripts)
- HTTP Basic Authentication supported (must be included in the url http://username:passwort@www...)
- Automatically follows redirects (both permanent and temporary redirects)
- Read headers or send your own raw post data!
- Read/EOF functions similar to files which allows on-the-fly reading of HTTP streams (You could feed this stream to an audio/movie decoder for example)
- For lazy users: SimpleHTTP functions, see examples at the end

Limitations:
- The POST-data is put together in one big memory block - not suitable for large files!
- When using multipart/form-data there is a very small chance that the boundary-string also appears in the transmitted data, which might confuse the server
- No https support - this library will fallback to http if you try to request https urls. YOU ARE RESPONSIBLE FOR THE SECURITY OF YOUR USERS! IF AVOIDABLE NEVER SEND PLAINTEXT PASSWORDS!
Due to bugs in Purebasics network implementation you need to use the following workaround code: http://www.purebasic.fr/english/viewtop ... 12&t=54302. This code is also included in the download package below.
Download link: https://dl.dropboxusercontent.com/u/614 ... -1.0.2.zip (md5: c549d2c6cd35a68c605826548feee510)
The included image is from the Tango base icon theme which is public domain, so you can redistribute it in your own program without any restrictions.
Changelog:
2013-04-12: Added DownloadDialog module, several enhancements in http.pbi.
2013-04-13: Improved DownloadDialog on Windows
2013-06-15: Fixed a small bug in redirection handling
2013-06-22: Fixed a small bug in URL Encoding
Example usage (see examples.pb in archive):
Code: Select all
XIncludeFile "disconnect.pbi" ;Must be in front of all HTTP Query Includes!!
XIncludeFile "http.pbi"
XIncludeFile "downloaddialog.pbi" ;Must be in front of all code using Windows!!
; ---- EXAMPLES ----
; You still have to manually initialize the network first
InitNetwork()
;- SimpleHTTP_GET
Debug "SimpleHTTP_GET Example:"
; Download a file directly to a string
Debug SimpleHTTP_GET("http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi")
Debug SimpleHTTP_GET("http://www.fds-team.de/")
;- SimpleHTTP_POST
Debug "-------------------"
Debug "SimpleHTTP_POST Example:"
NewList PostData.HTTPQuery_KeyValue()
AddElement(PostData())
PostData()\key = "key1"
PostData()\value = "value!with%special$characters&"
AddElement(PostData())
PostData()\key = "key2"
PostData()\value = "Here" + #CRLF$ + "some" + #CRLF$ + "value" + #CRLF$ + "with" + #CRLF$ + "newlines"
; Perform a POST request using the key-value pairs given above
; In this case we use multipart as encoding, but the other method also works for most scripts
Debug SimpleHTTP_POST("http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi", PostData(), #True)
;- SimpleHTTP_GET_File
Debug "-------------------"
Debug "SimpleHTTP_GET_File Example:"
; This downloads a file and on-the-fly calculates the md5 checksum.
Define filename.s = GetTemporaryDirectory() + "5MB.qsc"
Define md5.s = SimpleHTTP_GET_File("http://speedtest.qsc.de/5MB.qsc", filename)
If LCase(md5) = "8075a5bbe46e4d00d8e5eec439cf9a39"
Debug "Checksum okay!"
Else
Debug "Something was wrong with the download! MD5 checksum doesnt match!"
Debug "MD5 = " + md5
EndIf
;You may want to delete this file ;-)
;DeleteFile(filename)
;- SimpleHTTP_GET_Lines
Debug "-------------------"
Debug "SimpleHTTP_GET_Lines Example:"
NewList Lines.s()
; This will perform a GET request and add each line to the output list
If SimpleHTTP_GET_Lines("http://www.cs.tut.fi/cgi-bin/run/~jkorpela/echo.cgi", Lines())
Debug "Downloaded " + Str(ListSize(Lines())) + " lines"
; Show the lines
ForEach Lines()
Debug "Line: " + Lines()
Next
EndIf
;- DownloadDialog
Debug "-------------------"
Debug "DownloadDialog Example:"
Define *dl = DownloadDialog_Create()
If *dl
DownloadDialog_AddFile(*dl, "http://speedtest.qsc.de/5MB.qsc", GetTemporaryDirectory() + "5MB.qsc")
DownloadDialog_AddFile(*dl, "http://speedtest.tweak.nl/10mb.bin", GetTemporaryDirectory() + "10mb.bin")
;DownloadDialog_AddFile(*dl, "http://speedtest.qsc.de/10GB.qsc", "/dev/null")
DownloadDialog_Start(*dl)
While #True
Define Event.i = WaitWindowEvent()
; You can handle your own events here as usual ...
If DownloadDialog_Status(*dl) <> #DOWNLOAD_STATUS_INPROGRESS
Break
EndIf
Wend
Debug "Overall download status is: " + Str(DownloadDialog_Status(*dl))
While #True
Define filename.s = DownloadDialog_NextFailedFile(*dl)
If filename = ""
Break
EndIf
Debug "File " + filename + " could not be downloaded"
Wend
DownloadDialog_Free(*dl)
EndIf