Functions


Library: libHTTP.so


io_printf()


Declared in: DataIOUtils.h

	int io_printf( BDataIO *io, const char *format, ... )

printf() for BDataIO derived classes. This implementation calls vsprintf() to print to a temporary buffer and then calls BDataIO::Write() to send the string on its way. Because vsprintf() has no provisions for specifying the size of the buffer, bad things will happen if the output is greater than the temporary buffer size. The temporary buffer is currently 4096 bytes.


io_getline()


Declared in: DataIOUtils.h

	char *io_getline( BDataIO *io, char *dest, int32 length, char delim = '\n' )

Read an entire line ( excluding the delim character ) from io and store it in dest. No more than length characters are read.

Returns NULL if error ( like eof ) and dest if success.


parse_URI()


Declared in: HTTPUtils.h

	void parse_URI( const char *URI, brokenURI *brURI )

Break URI into its sub-components, which are placed in brURI.

This works on both absolute and relative URIs. The URI type and path type are identified and stored in brURI->URIType and brURI->PathType respectively.

struct brokenURI
{
	URI_Type 	URIType;
	
	char		scheme[16];
	char		host[64];
	char		port[8];
	
	Path_Type	PathType;
	
	char		path[2048];
	char		params[2048];
	char		query[2048];
};

URI_to_string()


Declared in: HTTPUtils.h

	char *URI_to_string( brokenURI *brURI, char *s, int32 size, bool full=true )

Convert a broken URI into a URI string. No more than size characters are written to s. If full is true, an absolute URI is created, otherwise, a relative URI is created. Returns s.

Absolute URI Sample: http://www.foo-bar.com/foo/bar/goober.html?thingy=whatszit
Relative URI Sample: /foo/bar/goober.html?thingy=whatszit

Some web servers will choke if they are sent an absolute URI; don't send absolute URIs to web servers. Most proxy servers require an absolute URI; do send absolute URIs to proxy servers.


get_status_str()


Declared in: HTTPUtils.h

	const char *get_status_str( int32 status_code )

Converts known http Status-Codes to standard Reason-Phrase's. For example, 200 will return "OK" and 404 will return "Not Found."


basic_authenticate()


Declared in: HTTPUtils.h

	bool basic_authenticate( const char *basic_cookie, const char *user, const char *pass, bool encrypted=true )

Implements "basic" HTTP authentication. basic_cookie should be set to the the field-value of an "WWW-Authenticate" header and if the provided user and pass match, basic_authenticate() returns true. If they don't match, false is returned. If encrypted is true, pass is assumed to be encrypted; the password stored in the network prefs file is encrypted.

Note: If you wish to get the user and password stored in the network prefs file, you can use getusername() and getpassword() declared in be/kit/net/netdb.h.

Security Note: The basic authentication method is not very secure. The user name and password sent with the "WWW-Authenticate" header is only thinly veiled in a base64 encoded wrapper. Anyone who can decode base64 ( try decode_base64() ) can read your password in plain-text if they can intercept the HTTP request. The only advantage to this authentication method is it is widely supported.


http_to_cgi_header()


Declared in: HTTPUtils.h

	char *http_to_cgi_header( char *header )

Convert an HTTP header to CGI HTTP_* format. All characters are converted to upper-case and '_' is subsituted for '-'. "HTTP_" is NOT prefixed; you will have to do this yourself if you need it.


uri_esc_str()


Declared in: StringUtils.h

	size_t uri_esc_str( char *dst, const char *src, size_t bufSize, bool usePlus = false, const char *protectedChars = "" );

URI encode a string. The string src is encoded and output to dst. No more than bufSize-1 characters are copied to dst and the resulting string length is returned. Characters from the reserved set ( ';' | '/' | '?' | ':' | '@' | '&' | SPACE | '=' | '+' | '\' | QUOTE | '#' | '%' | '<' | '>' ) are replaced with "%xx", where xx is the hex code of the character. The use of '+' in-place of a SPACE character is in common use; setting usePlus to true will enable this convention. Reserverd characers can be protected from encoding by specifing them in protectedChars.


uri_unescape_str()


Declared in: StringUtils.h

	size_t uri_unescape_str( char *dst, const char *src, size_t bufSize )

Decode a URI encoded string. The string src is decoded and copied to dst and the length of the resulting string is returned. The resulting string will be constrained to bufSize-1 characters. src and dst can be the same address.


get_next_token()


Declared in: StringUtils.h

	const char *get_next_token( char *dest, const char *source, size_t size, const char *delim = " ", const char *quote = "" )

Break a string into tokens. Something like strtok() declared in "string.h", but unlike strtok(), get_next_token() is thread-safe. The string source is scanned for the next token, which is copied to dest. No more than size-1 characters are copied. A pointer to the character which caused get_next_token() to stop is returned.

The string delim specifies the characters which define token boundries. All leading characters from this set as well any LWS are ignored. The first character not of this set ( or LWS ) marks the first character of the token. The next encountered character from the delim set or the '\0' character marks the end of the token.

If quote is not empty, quote characters can be used to protect characters in the delim set. If only one quote character is provided ( Example quote = "\"" ), the quote characters toggles delimiter protection. If two characters are provided ( Example quote = "()" ), the first character is the begin quote character and the second is the end quote character. This permits quote characters to be nested.


Example 1:

	char *string = " image/gif,    image/x-xbitmap, image/jpeg";
	char token[64];
	const char *s = string;
	
	while( *s && (s = get_next_token( token, string, 64, "," )) )
		printf( "%s\n", token );
	
	OUTPUT
	image/gif
	image/x-xbitmap
	image/jpeg
	

Example 2:

	char *string = "src=\"/happy picture\" width=60 height=30";
	char token[64];
	char fieldName[32];
	char fieldValue[256];
	const char *s1, *s2;
	
	s1 = string;
	while( *s1 && (s2 = get_next_token( token, string, 64, ",", "\"" )) )
	{
		s2 = get_next_token( fieldName, token, 32, "=" );
		s2 = get_next_token( fieldValue, token, 32, "=" );
		printf( "Field Name: %s, Field Value: %s\n", fieldName, fieldValue );
	}
	
	OUTPUT
	Field Name: src, Field Value: "happy picture"
	Field Name: width, Field Value: 60
	Field Name: height, Field Value: 30
	

strxcpy()


Declared in: StringUtils.h

	char *strxcpy( char *to, const char *from, size_t size )

Copies up to size characters from from to to and returns a pointer to the '\0' character in to. This is very similar to strncpy() declared in "string.h." strncpy() is not efficient when size is large and from is small; it will copy all the characters from the string from followed by as many null characters as it takes to add up to size. This is a waste of time! strxcpy() will stop after one null character.

The behavior of strxcpy differs from strncpy by its return value as well. strncpy() returns to, while strxcpy() returns a pointer to the null character in to. This is usefull because you can use strxcpy() to concatenate strings.


decode_base64


Declared in: StringUtils.h

	size_t decode_base64( char *dst, const char *src, size_t length )

Decode a base64 encoded string. The base64 encoded string, src, is decoded to the string dst. length is the size of dst. src and dst can have the same address. The size of the decoded string is returned.


libHTTP - A high-level HTTP API for the BeOS


Copyright (C) 1999 Joe Kloss