# Module httpclient

This module implements a simple HTTP client that can be used to retrieve webpages/other data.

# Retrieving a website

This example uses HTTP GET to retrieve `http://google.com`

    echo(getContent("http://google.com"))

# Using HTTP POST

This example demonstrates the usage of the W3 HTML Validator, it uses `multipart/form-data` as the `Content-Type` to send the HTML to the server.

    var headers: string = "Content-Type: multipart/form-data; boundary=xyz\c\L"
    var body: string = "--xyz\c\L"
    # soap 1.2 output
    body.add("Content-Disposition: form-data; name=\"output\"\c\L")
    body.add("\c\Lsoap12\c\L")

    # html
    body.add("--xyz\c\L")
    body.add("Content-Disposition: form-data; name=\"uploaded_file\";" &
             " filename=\"test.html\"\c\L")
    body.add("Content-Type: text/html\c\L")
    body.add("\c\L<html><head></head><body><p>test</p></body></html>\c\L")
    body.add("--xyz--")

    echo(postContent("http://validator.w3.org/check", headers, body))

# SSL/TLS support

This requires the OpenSSL library, fortunately it's widely used and installed on many operating systems. httpclient will use SSL automatically if you give any of the functions a url with the `https` schema, for example: `https://github.com/`, you also have to compile with `ssl` defined like so: `nimrod c -d:ssl ...`.

# Timeouts

Currently all functions support an optional timeout, by default the timeout is set to -1 which means that the function will never time out. The timeout is measured in miliseconds, once it is set any call on a socket which may block will be susceptible to this timeout, however please remember that the function as a whole can take longer than the specified timeout, only individual internal calls on the socket are affected. In practice this means that as long as the server is sending data an exception will not be raised, if however data does not reach client within the specified timeout an ETimeout exception will then be raised.

# Imports

sockets, strutils, parseurl, parseutils, strtabs

# Types

    TResponse = tuple[version: string, status: string, headers: PStringTable,
                      body: string]

    EInvalidProtocol = object of ESynch

  exception that is raised when server does not conform to the implemented protocol

    EHttpRequestErr = object of ESynch

  Thrown in the `getContent` proc and `postContent` proc, when the server returns an error

    THttpMethod = enum
      httpHEAD, ## Asks for the response identical to the one that would
                ## correspond to a GET request, but without the response
                ## body.
      httpGET,                    ## Retrieves the specified resource.
      httpPOST, ## Submits data to be processed to the identified
                ## resource. The data is included in the body of the
                ## request.
      httpPUT,                    ## Uploads a representation of the specified resource.
      httpDELETE,                 ## Deletes the specified resource.
      httpTRACE, ## Echoes back the received request, so that a client
                 ## can see what intermediate servers are adding or
                 ## changing in the request.
      httpOPTIONS, ## Returns the HTTP methods that the server supports
                   ## for specified address.
      httpCONNECT ## Converts the request connection to a transparent
                  ## TCP/IP tunnel, usually used for proxies.

  the requested HttpMethod

# Consts

    defUserAgent = "Nimrod httpclient/0.1"

# Procs

    proc request(url: string; httpMethod = httpGET; extraHeaders = ""; body = "";
                 sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
                 userAgent = defUserAgent): TResponse {.raises: [EHttpRequestErr,
        EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol, E_Base],
        tags: [FReadIO, FWriteIO, FTime].}

  Requests `url` with the specified `httpMethod`.
  Extra headers can be specified and must be seperated by `\c\L`
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

    proc get(url: string; extraHeaders = ""; maxRedirects = 5;
             sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
             userAgent = defUserAgent): TResponse {.raises: [EHttpRequestErr,
        EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol, E_Base],
        tags: [FReadIO, FWriteIO, FTime].}

  GETs the `url` and returns a `TResponse` object
  This proc also handles redirection
  Extra headers can be specified and must be separated by `\c\L`.
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

    proc getContent(url: string; extraHeaders = ""; maxRedirects = 5;
                    sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
                    userAgent = defUserAgent): string {.raises: [EHttpRequestErr,
        EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol, E_Base,
        EHttpRequestErr], tags: [FReadIO, FWriteIO, FTime].}

  GETs the body and returns it as a string.
  Raises exceptions for the status codes `4xx` and `5xx`
  Extra headers can be specified and must be separated by `\c\L`.
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

    proc post(url: string; extraHeaders = ""; body = ""; maxRedirects = 5;
              sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
              userAgent = defUserAgent): TResponse {.raises: [EHttpRequestErr,
        EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol, E_Base],
        tags: [FReadIO, FWriteIO, FTime].}

  POSTs `body` to the `url` and returns a `TResponse` object.
  This proc adds the necessary Content-Length header.
  This proc also handles redirection.
  Extra headers can be specified and must be separated by `\c\L`.
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

    proc postContent(url: string; extraHeaders = ""; body = ""; maxRedirects = 5;
                     sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
                     userAgent = defUserAgent): string {.raises: [EHttpRequestErr,
        EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol, E_Base,
        EHttpRequestErr], tags: [FReadIO, FWriteIO, FTime].}

  POSTs `body` to `url` and returns the response's body as a string
  Raises exceptions for the status codes `4xx` and `5xx`
  Extra headers can be specified and must be separated by `\c\L`.
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

    proc downloadFile(url: string; outputFilename: string;
                      sslContext: PSSLContext = defaultSSLContext; timeout = - 1;
                      userAgent = defUserAgent) {.raises: [EOutOfMemory, EIO,
        EHttpRequestErr, EOverflow, EInvalidValue, EOS, ETimeout, EInvalidProtocol,
        E_Base], tags: [FWriteIO, FReadIO, FTime].}

  Downloads `url` and saves it to `outputFilename`
  An optional timeout can be specified in miliseconds, if reading from the

  server takes longer than specified an ETimeout exception will be raised.

Generated: 2013-05-21 09:55:15 UTC