nettobac  0.0.0
Network features for FreeBASIC code
nettobac_http.bas File Reference

Utility functions to handle http requests. More...

This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  MimeTypes {
  MIME_HTM = &b0000000000000, MIME_TXT = &b0000000000010, MIME_BMP = &b0000000000100, MIME_GIF = &b0000000001000,
  MIME_JPG = &b0000000010000, MIME_PNG = &b0000000100000, MIME_TIF = &b0000001000000, MIME_WAV = &b0000010000000,
  MIME_MP3 = &b0000100000000, MIME_OGG = &b0001000000000, MIME_PDF = &b0010000000000, MIME_ZIP = &b0100000000000,
  MIME_GZ = &b1000000000000
}
 

Functions

FUNCTION_AS_STRING MimeType (BYVAL_AS_MimeTypes Typ)
 Create a text version of the mime type bit mask. More...
 
FUNCTION_AS_STRING urlEncode (BYREF_AS_STRING Url)
 Encode an URL text. More...
 
FUNCTION_AS_STRING urlDecode (BYREF_AS_STRING Url)
 Decode an URL text. More...
 
FUNCTION_AS_STRING httpGetReq (BYREF_AS_STRING Host, BYREF_AS_STRING Targ, BYVAL_AS_MimeTypes Mime=MIME_HTM OR MIME_TXT)
 Create a HTTP GET request. More...
 
FUNCTION_AS_STRING httpHeadReq (BYREF_AS_STRING Host, BYREF_AS_STRING Targ, BYVAL_AS_MimeTypes Mime=MIME_HTM OR MIME_TXT)
 Create a HTTP HEAD request. More...
 
FUNCTION_AS_STRING httpPostReq (BYREF_AS_STRING Host, BYREF_AS_STRING Targ, BYREF_AS_STRING Query, BYVAL_AS_INTEGER Refer=1)
 Create a HTTP POST request. More...
 
FUNCTION_AS_STRING httpPutReq (BYREF_AS_STRING Host, BYREF_AS_STRING Targ, BYREF_AS_STRING Content="")
 Create a HTTP HEAD request. More...
 
FUNCTION_AS_CONST_ZSTRING_CONST_PTR httpLoad (BYREF_AS_STRING Res, BYREF_AS_STRING Adr, BYVAL_AS_MimeTypes Mim=MIME_HTM OR MIME_TXT, BYVAL_AS_USHORT Port=80, BYVAL_AS_SHORT Mo=&b10)
 Load a file over network via http protocol. More...
 

Variables

const STRING LINEEND = !"\r\n"
 The line end characters. More...
 
const STRING HEADEREND = !"\r\n\r\n"
 The end of header characters. More...
 

Detailed Description

Utility functions to handle http requests.

This file contains

  • constants for end of line, header
  • enumerators for mime types and a function to create a type string
  • functions to create http requests (low level API)
  • a function to download a file (high level API)

Copyright (C) LGPLv2.1, see ReadMe.md for details.

Since
0.0.0

Definition in file nettobac_http.bas.

Enumeration Type Documentation

enum MimeTypes
Enumerator
MIME_HTM 

Mime type "text/html".

MIME_TXT 

Mime type "text/plain".

MIME_BMP 

Mime type "image/bmp".

MIME_GIF 

Mime type "image/gif".

MIME_JPG 

Mime type "image/jpeg".

MIME_PNG 

Mime type "image/png".

MIME_TIF 

Mime type "image/tiff".

MIME_WAV 

Mime type "audio/wav".

MIME_MP3 

Mime type "audio/mpeg".

MIME_OGG 

Mime type "audio/ogg".

MIME_PDF 

Mime type "application/pdf".

MIME_ZIP 

Mime type "application/x-compressed".

MIME_GZ 

Mime type "application/gzip".

Definition at line 20 of file nettobac_http.bas.

Function Documentation

FUNCTION_AS_STRING MimeType ( BYVAL_AS_MimeTypes  Typ)

Create a text version of the mime type bit mask.

Parameters
Typthe bitmask containing the type bits (see enumerator MimeTypes)
Returns
the text list of mime types (; separated)

Mime types get handled as a bit mask at module level, for better readability. This function translate the bit mask in to a STRING representation.

Since
0.0.0

Definition at line 47 of file nettobac_http.bas.

Here is the caller graph for this function:

FUNCTION_AS_STRING urlEncode ( BYREF_AS_STRING  Url)

Encode an URL text.

Parameters
Urlthe URL text
Returns
the encoded version of the input

Encode special characters in an URL. Normal characters are

  • A to Z
  • a to z
  • 0 to 9
  • _ (underscore), / (slash)

All other characters get replaced by their %<hexval> representation.

Since
0.0.0

Definition at line 81 of file nettobac_http.bas.

FUNCTION_AS_STRING urlDecode ( BYREF_AS_STRING  Url)

Decode an URL text.

Parameters
Urlthe encoded URL text
Returns
the decodeded version of the input

Decode special characters in an URL. The %<hexval> representations get resolved in to the related characters.

Since
0.0.0

Definition at line 104 of file nettobac_http.bas.

Here is the caller graph for this function:

FUNCTION_AS_STRING httpGetReq ( BYREF_AS_STRING  Host,
BYREF_AS_STRING  Targ,
BYVAL_AS_MimeTypes  Mime = MIME_HTM OR MIME_TXT 
)

Create a HTTP GET request.

Parameters
Hostthe host adress (ie. "domain.com")
Targthe path to search for (ie. "img/test.jpg")
Mimethe mime type (ie. MIME_JPG, see MimeTypes)
Returns
the complete http request STRING

Requests a representation of the specified resource.

Since
0.0.0

Definition at line 126 of file nettobac_http.bas.

Here is the call graph for this function:

FUNCTION_AS_STRING httpHeadReq ( BYREF_AS_STRING  Host,
BYREF_AS_STRING  Targ,
BYVAL_AS_MimeTypes  Mime = MIME_HTM OR MIME_TXT 
)

Create a HTTP HEAD request.

Parameters
Hostthe host adress (ie. "domain.com")
Targthe path to search for (ie. "html/index.html")
Mimethe mime type (ie. MIME_HTM, see MimeTypes)
Returns
the request STRING

Asks for the response identical to the one that would correspond to a GET request, but without the response body. It's possible to extract the content length and Mime type from it.

Since
0.0.0

Definition at line 146 of file nettobac_http.bas.

Here is the call graph for this function:

FUNCTION_AS_STRING httpPostReq ( BYREF_AS_STRING  Host,
BYREF_AS_STRING  Targ,
BYREF_AS_STRING  Query,
BYVAL_AS_INTEGER  Refer = 1 
)

Create a HTTP POST request.

Parameters
Hostthe host adress (ie. "domain.com")
Targthe path to search for (ie. "data/script.php")
Query(ie. "key1=value&key2=value")
Referif a Referer: should get added
Returns
the request STRING

Requests that the server accept the entity enclosed in the request as a new subordinate of the web resource identified by the URI.

Since
0.0.0

Definition at line 166 of file nettobac_http.bas.

FUNCTION_AS_STRING httpPutReq ( BYREF_AS_STRING  Host,
BYREF_AS_STRING  Targ,
BYREF_AS_STRING  Content = "" 
)

Create a HTTP HEAD request.

Parameters
Hostthe host adress (ie. "domain.com")
Targthe path to search for (ie. "data/test.txt")
Contentany content (defaults to "")
Returns
the request STRING

Requests that the enclosed entity be stored under the supplied URI.

Since
0.0.0

Definition at line 188 of file nettobac_http.bas.

FUNCTION_AS_CONST_ZSTRING_CONST_PTR httpLoad ( BYREF_AS_STRING  Res,
BYREF_AS_STRING  Adr,
BYVAL_AS_MimeTypes  Mim = MIME_HTM OR MIME_TXT,
BYVAL_AS_USHORT  Port = 80,
BYVAL_AS_SHORT  Mo = &b10 
)

Load a file over network via http protocol.

Parameters
Resa STRING variable to append the result
Adrthe address of the target (ie. freebasic.net/index.html)
Mimthe mime type (see enumerators MimeTypes)
PortThe port number to use (defaults to 80)
Mothe modus which data the result variable Res should contain
Returns
the requested file context, if OK

This function loads a file over a network connection. It tries to

  • create a nettobacClient instance to the server adress
  • open a n2bConnetion
  • request the target file
  • receive the http reponse
  • check the http header
    • if header OK, strip header and return data only
    • if not OK, return all data unchanged
  • delete the nettobacClient instance (and close the n2bConnetion)

An error check gets done after each step. In case of an error the function breaks and the error text gets returned. Otherwise the return value 0 (zero) indicates successful operation.

Note
Although the result variable Res may contains data, the function can report an error (ie. when the connection gets lost during transfer). It's recommended to check .Errr always.
By default the http header gets checked and if it includes 200 OK it gets extracted from the result variable Res (only data get returned). In order to receive the header (unchecked) as well, set bit 0 in the modus parameter (Mo OR= &b1).
The received data get appended to result variable Res. In order to reset the result STRING (Res = "") before operaion, set bit 1 in the modus parameter (Mo OR= &b10).
Since
0.0.0

Definition at line 233 of file nettobac_http.bas.

Here is the call graph for this function:

Here is the caller graph for this function:

Variable Documentation

const STRING LINEEND = !"\r\n"

The line end characters.

Definition at line 17 of file nettobac_http.bas.

const STRING HEADEREND = !"\r\n\r\n"

The end of header characters.

Definition at line 18 of file nettobac_http.bas.