View Source hackney_http (hackney v1.23.0)

HTTP parser in pure Erlang This parser is able to parse HTTP responses and requests in a streaming fashion. If not set it will be autodetect the type of binary parsed, if it's a request or a response.

Internally it is keeping a buffer for intermediary steps but don't keep any state in memory.

The first time you initialise a parser using hackney_http:parser/0 or hackney_http:parser/1 you will receive an opaque record You can then process it using the function hackney_http:execute/2.

Each steps will return the status, some data and the new parser that you can process later with hackney_http:execute/2 when {more, ...} is returnned or hackney_http:execute/1 in other cases:
  • {response, http_version(), status(), http_reason(), parser()}: when the first line of a response is parsed
  • {request, http_version(), http_method(), uri(), parser()}: when the first line of a request (on servers) is parsed
  • {more, parser()}: when the parser need more data. The new data should be passed to hackney_http:execute/2 with the new parser() state received.
  • {header, {Name :: binary(), Value :: binary()}, parser()}: when an header has been parsed. To continue the parsing you must call the given parser() with hackney_http:execute/1.
  • {headers_complete, parser()} : when all headers have been parsed. To continue the parsing you must call the given parser() state with hackney_http:execute/1.
  • {more, parser(), binary()}: on body, when the parser need more data. The new data should be passed to hackney_http:execute/2 (with parser() ) when received. The binary at the end of the tuple correspond to the actual buffer of the parser. It may be used for other purpose, like start to parse a new request on pipeline connections, for a proxy...
  • {ok, binary(), parser()}: on body, when a chunk has been parsed. To continue the parsing you must call hackney_http:execute/1 with the given parser().
  • {done, binary()}: when the parsing is done. The binary given correpond to the non parsed part of the internal buffer.
  • {error, term{}}: when an error happen

Summary

Functions

Execute the parser with the current buffer.
Execute the parser with the new buffer
retrieve a parser property. Properties are:
  • buffer: internal buffer of the parser (non parsed)
  • state: the current state (on_status, on_header, on_body, done)
  • version: HTTP version
  • content_length: content length header if any
  • transfer_encoding: transfer encoding header if any
  • content_type: content type header if any
  • location: location header if any
  • connection: connection header if any.
Create a new HTTP parser. The parser will autodetect if the parded binary is a response or a request.

create a new HTTP parser with options. By default the type of parsed binary will be detected.

Types

body_result/0

-type body_result() :: {more, parser(), binary()} | {ok, binary(), parser()} | {done, binary()} | done.

header_result/0

-type header_result() :: {headers_complete, parser()} | {header, {binary(), binary()}, parser()}.

http_method/0

-type http_method() :: binary().

http_reason/0

-type http_reason() :: binary().

http_version/0

-type http_version() :: {integer(), integer()}.

parser/0

-type parser() ::
          #hparser{type :: atom(),
                   max_line_length :: integer(),
                   max_empty_lines :: integer(),
                   empty_lines :: integer(),
                   state :: atom(),
                   buffer :: binary(),
                   version :: {integer(), integer()} | undefined,
                   method :: binary(),
                   partial_headers :: list(),
                   clen :: integer() | undefined | bad_int,
                   te :: binary(),
                   connection :: binary(),
                   ctype :: binary(),
                   location :: binary(),
                   body_state :: atom() | tuple()}.

parser_option/0

-type parser_option() ::
          request | response | auto | {max_empty_lines, integer()} | {max_line_length, integer()}.

parser_options/0

-type parser_options() :: [parser_option()].

parser_result/0

-type parser_result() ::
          {response, http_version(), status(), http_reason(), parser()} |
          {request, http_method(), uri(), http_version(), parser()} |
          {more, parser()} |
          header_result() |
          body_result() |
          {error, term()}.

status/0

-type status() :: integer().

uri/0

-type uri() :: binary().

Functions

execute(Hparser)

-spec execute(#hparser{type :: atom(),
                       max_line_length :: integer(),
                       max_empty_lines :: integer(),
                       empty_lines :: integer(),
                       state :: atom(),
                       buffer :: binary(),
                       version :: {integer(), integer()} | undefined,
                       method :: binary(),
                       partial_headers :: list(),
                       clen :: integer() | undefined | bad_int,
                       te :: binary(),
                       connection :: binary(),
                       ctype :: binary(),
                       location :: binary(),
                       body_state :: atom() | tuple()}) ->
                 parser_result().
Execute the parser with the current buffer.

execute(Hparser, Bin)

-spec execute(#hparser{type :: atom(),
                       max_line_length :: integer(),
                       max_empty_lines :: integer(),
                       empty_lines :: integer(),
                       state :: atom(),
                       buffer :: binary(),
                       version :: {integer(), integer()} | undefined,
                       method :: binary(),
                       partial_headers :: list(),
                       clen :: integer() | undefined | bad_int,
                       te :: binary(),
                       connection :: binary(),
                       ctype :: binary(),
                       location :: binary(),
                       body_state :: atom() | tuple()},
              binary()) ->
                 parser_result().
Execute the parser with the new buffer

get(Parser, Props)

-spec get(parser(), atom() | [atom()]) -> any().
retrieve a parser property. Properties are:
  • buffer: internal buffer of the parser (non parsed)
  • state: the current state (on_status, on_header, on_body, done)
  • version: HTTP version
  • content_length: content length header if any
  • transfer_encoding: transfer encoding header if any
  • content_type: content type header if any
  • location: location header if any
  • connection: connection header if any.

parse_response_version(_, St)

parser()

-spec parser() -> parser().
Create a new HTTP parser. The parser will autodetect if the parded binary is a response or a request.

parser(Options)

-spec parser(parser_options()) -> parser().

create a new HTTP parser with options. By default the type of parsed binary will be detected.

Available options:
  • auto : autodetect if the binary parsed is a response or a request (default).
  • response: set the parser to parse a response
  • request: set the parser to parse a request (server)
  • {max_line_lenght, Max}: set the maximum size of a line parsed before we give up.
  • {max_lines_empty, Max}: the maximum number of empty line we accept before the first line happen