Main Page Data Structures File List Data Fields Globals Related Pages

# inet/url.cpp File Reference

`#include <stdio.h>`
`#include <stdlib.h>`
`#include <string.h>`
`#include <misc.h>`
`#include <ctype.h>`
`#include "inet/url.h"`

Go to the source code of this file.

## Data Structures
struct | scheme_t
## Functions
unsigned | port_for_scheme (char *scheme)
| Return the default port for a given scheme.
int | htoi (char *s)
| Convert a 2-digit hexadecimal string to an int.
void | free_url (URL *url)
| Free an URL structure.
URL * | parse_url (const char *urlString)
| Parse an URL and return its components.
char * | url_encode (char *s, unsigned len)
| URL-encode according to RFC1738.
unsigned | url_decode (char *str, unsigned len)
| Decode URL-encoded data.
## Variables
scheme_t | schemes []
unsigned char | hexchars [] = "0123456789ABCDEF"

## Function Documentation

void free_url | ( | URL * | url | ) |

  | Free an URL structure. Use this function to free all memory allocated by an URL structure, that was previously allocated by parse_url(). All strings and the structure itself are free'd. Parameters:
  url | Pointer to URL structure to free.
Definition at line 103 of file url.cpp. References URL::fragment, URL::host, URL::pass, URL::path, URL::query, URL::scheme, and URL::user. Referenced by http_connect().

int htoi | ( | char * | s | ) | `[static]`

  | Convert a 2-digit hexadecimal string to an int. Parameters:
  s | pointer to a string containing the digits
Returns: converted integer value Definition at line 76 of file url.cpp. Referenced by url_decode().

URL* parse_url | ( | const char * | urlString | ) |

  | Parse an URL and return its components. This function returns an URL data structure, returning any of the various components of the URL that are present. This includes the scheme, host, port, user, pass, path, query, and fragment. The parsing is done according to RFC 1738 (atleast, i hope so, send me a mail, if you find an error!) If the URL contains a scheme but no port, parse_url() tries to find out the default port for this scheme. Parameters:
  urlString | URL string to parse
Returns: pointer to an URL structure containing all components, or NULL if out of memory. This has to be freed by a call to free_url() after use. Todo: perhaps we should detect some syntax errors.. Definition at line 133 of file url.cpp. References URL::fragment, URL::host, URL::pass, URL::path, URL::port, port_for_scheme(), URL::query, URL::scheme, and URL::user. Referenced by http_connect(), and http_get().

unsigned port_for_scheme | ( | char * | scheme | ) | `[static]`

  | Return the default port for a given scheme. Parameters:
  scheme | Pointer to scheme string
Returns: default port for scheme, or 0 if unknown. Todo: is it a good idea to return 0 for unknown schemes?! [isn't 0 a regular port, too?] Definition at line 55 of file url.cpp. References scheme_t::port, and scheme_t::scheme. Referenced by parse_url().

unsigned url_decode | ( | char * | str,
|  | unsigned | len
| ) |

  | Decode URL-encoded data. Returns a string in which the sequences with percent (%) signs followed by two hex digits have been replaced with literal characters. For example, the string "foo%20bar%40baz" decodes into "foo bar@baz". Parameters:
  str | Data to decode. The data is decoded in-place, and also functions as the target buffer.
  len | number of encoded characters to decode.
Returns: Number of decoded characters. Definition at line 271 of file url.cpp. References htoi(), and src.

char* url_encode | ( | char * | s,
|  | unsigned | len
| ) |

  | URL-encode according to RFC1738. Returns a string in which all non-alphanumeric characters except -_. have been replaced with a percent (%) sign followed by two hex digits. This is the encoding described in RFC1738 for protecting literal characters from being interpreted as special URL delimiters, and for protecting URL's from being mangled by transmission media with character conversions (like some email systems). Parameters:
  s | String to encode
  len | Number of string characters to encode
Returns: Pointer to encoded string. (has to be free'd after use) Definition at line 234 of file url.cpp. References hexchars.

## Variable Documentation

unsigned char hexchars[] = "0123456789ABCDEF"`[static]`

| Definition at line 67 of file url.cpp. Referenced by url_encode().

scheme_t schemes[]`[static]`

| Initial value:
     {
      {"ftp",      DEFAULT_FTP_PORT},
      {"telnet",   DEFAULT_TELNET_PORT},
      {"mailto",   DEFAULT_SMTP_PORT},
      {"gopher",   DEFAULT_GOPHER_PORT},
      {"http",     DEFAULT_HTTP_PORT},
      {"nntp",     DEFAULT_NNTP_PORT},
      {"news",     DEFAULT_NNTP_PORT},
      {"wais",     DEFAULT_WAIS_PORT},
      {"https",    DEFAULT_HTTPS_PORT},
      {"snews",    DEFAULT_SNEWS_PORT},
      { NULL, 0 }
    }
Definition at line 32 of file url.cpp.