| 1 | /*
|
|---|
| 2 | * Copyright (c) 2013 Martin Sucha
|
|---|
| 3 | * All rights reserved.
|
|---|
| 4 | *
|
|---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
|---|
| 6 | * modification, are permitted provided that the following conditions
|
|---|
| 7 | * are met:
|
|---|
| 8 | *
|
|---|
| 9 | * - Redistributions of source code must retain the above copyright
|
|---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
|---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
|---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
|---|
| 13 | * documentation and/or other materials provided with the distribution.
|
|---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
|---|
| 15 | * derived from this software without specific prior written permission.
|
|---|
| 16 | *
|
|---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
|---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
|---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
|---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
|---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
|---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|---|
| 27 | */
|
|---|
| 28 |
|
|---|
| 29 | /** @addtogroup http
|
|---|
| 30 | * @{
|
|---|
| 31 | */
|
|---|
| 32 | /**
|
|---|
| 33 | * @file
|
|---|
| 34 | */
|
|---|
| 35 |
|
|---|
| 36 | #ifndef HTTP_HTTP_H_
|
|---|
| 37 | #define HTTP_HTTP_H_
|
|---|
| 38 |
|
|---|
| 39 | #include <net/socket.h>
|
|---|
| 40 | #include <adt/list.h>
|
|---|
| 41 | #include <inet/addr.h>
|
|---|
| 42 |
|
|---|
| 43 | #include "receive-buffer.h"
|
|---|
| 44 |
|
|---|
| 45 | typedef struct {
|
|---|
| 46 | char *host;
|
|---|
| 47 | uint16_t port;
|
|---|
| 48 | inet_addr_t addr;
|
|---|
| 49 |
|
|---|
| 50 | bool connected;
|
|---|
| 51 | int conn_sd;
|
|---|
| 52 |
|
|---|
| 53 | size_t buffer_size;
|
|---|
| 54 | receive_buffer_t recv_buffer;
|
|---|
| 55 | } http_t;
|
|---|
| 56 |
|
|---|
| 57 | typedef struct {
|
|---|
| 58 | uint8_t minor;
|
|---|
| 59 | uint8_t major;
|
|---|
| 60 | } http_version_t;
|
|---|
| 61 |
|
|---|
| 62 | typedef struct {
|
|---|
| 63 | link_t link;
|
|---|
| 64 | char *name;
|
|---|
| 65 | char *value;
|
|---|
| 66 | } http_header_t;
|
|---|
| 67 |
|
|---|
| 68 | typedef struct {
|
|---|
| 69 | list_t list;
|
|---|
| 70 | } http_headers_t;
|
|---|
| 71 |
|
|---|
| 72 | typedef struct {
|
|---|
| 73 | char *method;
|
|---|
| 74 | char *path;
|
|---|
| 75 | http_headers_t headers;
|
|---|
| 76 | } http_request_t;
|
|---|
| 77 |
|
|---|
| 78 | typedef struct {
|
|---|
| 79 | http_version_t version;
|
|---|
| 80 | uint16_t status;
|
|---|
| 81 | char *message;
|
|---|
| 82 | http_headers_t headers;
|
|---|
| 83 | } http_response_t;
|
|---|
| 84 |
|
|---|
| 85 | extern http_t *http_create(const char *, uint16_t);
|
|---|
| 86 | extern int http_connect(http_t *);
|
|---|
| 87 |
|
|---|
| 88 | extern void http_header_init(http_header_t *);
|
|---|
| 89 | extern http_header_t *http_header_create(const char *, const char *);
|
|---|
| 90 | extern int http_header_receive_name(receive_buffer_t *, char **);
|
|---|
| 91 | extern int http_header_receive_value(receive_buffer_t *, char **);
|
|---|
| 92 | extern int http_header_receive(receive_buffer_t *, http_header_t *);
|
|---|
| 93 | extern void http_header_normalize_value(char *);
|
|---|
| 94 | extern bool http_header_name_match(const char *, const char *);
|
|---|
| 95 | ssize_t http_header_encode(http_header_t *, char *, size_t);
|
|---|
| 96 | extern void http_header_destroy(http_header_t *);
|
|---|
| 97 |
|
|---|
| 98 | extern void http_headers_init(http_headers_t *);
|
|---|
| 99 | extern int http_headers_find_single(http_headers_t *, const char *,
|
|---|
| 100 | http_header_t **);
|
|---|
| 101 | extern int http_headers_append(http_headers_t *, const char *, const char *);
|
|---|
| 102 | extern int http_headers_set(http_headers_t *, const char *, const char *);
|
|---|
| 103 | extern int http_headers_get(http_headers_t *, const char *, char **);
|
|---|
| 104 | extern int http_headers_receive(receive_buffer_t *, http_headers_t *);
|
|---|
| 105 | extern void http_headers_clear(http_headers_t *);
|
|---|
| 106 |
|
|---|
| 107 | #define http_headers_foreach(headers, iter) \
|
|---|
| 108 | list_foreach((headers).list, link, http_header_t, (iter))
|
|---|
| 109 |
|
|---|
| 110 | static inline void http_headers_remove(http_headers_t *headers,
|
|---|
| 111 | http_header_t *header)
|
|---|
| 112 | {
|
|---|
| 113 | list_remove(&header->link);
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | static inline void http_headers_append_header(http_headers_t *headers,
|
|---|
| 117 | http_header_t *header)
|
|---|
| 118 | {
|
|---|
| 119 | list_append(&header->link, &headers->list);
|
|---|
| 120 | }
|
|---|
| 121 |
|
|---|
| 122 | extern http_request_t *http_request_create(const char *, const char *);
|
|---|
| 123 | extern void http_request_destroy(http_request_t *);
|
|---|
| 124 | extern int http_request_format(http_request_t *, char **, size_t *);
|
|---|
| 125 | extern int http_send_request(http_t *, http_request_t *);
|
|---|
| 126 | extern int http_parse_status(const char *, http_version_t *, uint16_t *,
|
|---|
| 127 | char **);
|
|---|
| 128 | extern int http_receive_response(http_t *, http_response_t **);
|
|---|
| 129 | extern int http_receive_body(http_t *, void *, size_t);
|
|---|
| 130 | extern void http_response_destroy(http_response_t *);
|
|---|
| 131 | extern int http_close(http_t *);
|
|---|
| 132 | extern void http_destroy(http_t *);
|
|---|
| 133 |
|
|---|
| 134 | #endif
|
|---|
| 135 |
|
|---|
| 136 | /** @}
|
|---|
| 137 | */
|
|---|