2013-10-16 07:55:58 +00:00
|
|
|
/*
|
|
|
|
Serval DNA - HTTP Server API
|
2013-12-04 06:26:55 +00:00
|
|
|
Copyright (C) 2013 Serval Project Inc.
|
2013-10-16 07:55:58 +00:00
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU General Public License
|
|
|
|
as published by the Free Software Foundation; either version 2
|
|
|
|
of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with this program; if not, write to the Free Software
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#ifndef __SERVAL_DNA__HTTP_SERVER_H
|
|
|
|
#define __SERVAL_DNA__HTTP_SERVER_H
|
2013-10-16 07:55:58 +00:00
|
|
|
|
2017-11-24 00:03:39 +00:00
|
|
|
#include <stdint.h> // for uint16_t, uint8_t
|
2013-10-16 07:55:58 +00:00
|
|
|
#include <limits.h>
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "lang.h" // for bool_t
|
|
|
|
#include "os.h" // for time_ms_t
|
|
|
|
#include "idebug.h" // for struct idebug
|
2013-10-16 07:55:58 +00:00
|
|
|
#include "strbuf.h"
|
2017-11-24 00:03:39 +00:00
|
|
|
#include "strbuf_helpers.h" // for struct json_atom
|
|
|
|
#include "fdqueue.h" // for struct sched_ent
|
|
|
|
#include "socket.h" // for struct socket_address
|
2013-10-16 07:55:58 +00:00
|
|
|
|
|
|
|
/* Generic HTTP request handling.
|
|
|
|
*
|
|
|
|
* @author Andrew Bettison <andrew@servalproject.com>
|
|
|
|
*/
|
|
|
|
|
|
|
|
extern const char HTTP_VERB_GET[];
|
|
|
|
extern const char HTTP_VERB_POST[];
|
|
|
|
extern const char HTTP_VERB_PUT[];
|
|
|
|
extern const char HTTP_VERB_HEAD[];
|
|
|
|
extern const char HTTP_VERB_DELETE[];
|
|
|
|
extern const char HTTP_VERB_TRACE[];
|
|
|
|
extern const char HTTP_VERB_OPTIONS[];
|
|
|
|
extern const char HTTP_VERB_CONNECT[];
|
|
|
|
extern const char HTTP_VERB_PATCH[];
|
|
|
|
|
|
|
|
typedef uint64_t http_size_t;
|
|
|
|
#define PRIhttp_size_t PRIu64
|
|
|
|
|
|
|
|
struct http_request;
|
|
|
|
|
|
|
|
struct http_range {
|
|
|
|
enum http_range_type { NIL = 0, CLOSED, OPEN, SUFFIX } type;
|
|
|
|
http_size_t first; // only for CLOSED or OPEN
|
|
|
|
http_size_t last; // only for CLOSED or SUFFIX
|
|
|
|
};
|
|
|
|
|
2013-10-26 09:58:47 +00:00
|
|
|
unsigned http_range_close(struct http_range *dst, const struct http_range *src, unsigned nranges, http_size_t content_length);
|
|
|
|
http_size_t http_range_bytes(const struct http_range *range, unsigned nranges);
|
2013-10-16 07:55:58 +00:00
|
|
|
|
|
|
|
#define CONTENT_LENGTH_UNKNOWN UINT64_MAX
|
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
struct mime_content_type {
|
|
|
|
char type[64];
|
|
|
|
char subtype[64];
|
|
|
|
char multipart_boundary[71];
|
|
|
|
char charset[31];
|
2014-07-10 11:42:40 +00:00
|
|
|
char format[31];
|
2013-10-28 11:57:27 +00:00
|
|
|
};
|
|
|
|
|
2017-10-19 23:25:21 +00:00
|
|
|
int mime_content_types_are_equal(const struct mime_content_type *a, const struct mime_content_type *b);
|
|
|
|
|
|
|
|
extern const struct mime_content_type CONTENT_TYPE_FAVICON;
|
|
|
|
extern const struct mime_content_type CONTENT_TYPE_TEXT;
|
|
|
|
extern const struct mime_content_type CONTENT_TYPE_HTML;
|
|
|
|
extern const struct mime_content_type CONTENT_TYPE_JSON;
|
|
|
|
extern const struct mime_content_type CONTENT_TYPE_BLOB;
|
|
|
|
|
2013-10-29 07:02:04 +00:00
|
|
|
struct http_client_authorization {
|
|
|
|
enum http_authorization_scheme { NOAUTH = 0, BASIC } scheme;
|
|
|
|
union {
|
|
|
|
struct http_client_credentials_basic {
|
|
|
|
const char *user;
|
|
|
|
const char *password;
|
|
|
|
} basic;
|
|
|
|
} credentials;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct http_www_authenticate {
|
|
|
|
enum http_authorization_scheme scheme;
|
|
|
|
const char *realm;
|
|
|
|
};
|
|
|
|
|
2015-09-30 21:25:00 +00:00
|
|
|
struct http_origin {
|
2015-10-13 08:14:46 +00:00
|
|
|
uint8_t null;
|
|
|
|
char scheme[10]; // enough for "https"
|
|
|
|
char hostname[40]; // enough for "localhost"
|
2015-09-30 21:25:00 +00:00
|
|
|
uint16_t port;
|
|
|
|
};
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_request_headers {
|
|
|
|
http_size_t content_length;
|
2013-10-28 11:57:27 +00:00
|
|
|
struct mime_content_type content_type;
|
2013-10-16 07:55:58 +00:00
|
|
|
unsigned short content_range_count;
|
2015-09-30 21:25:00 +00:00
|
|
|
struct http_origin origin;
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_range content_ranges[5];
|
2013-10-29 07:02:04 +00:00
|
|
|
struct http_client_authorization authorization;
|
2017-05-22 06:47:10 +00:00
|
|
|
bool_t expect:1;
|
|
|
|
bool_t chunked:1;
|
2013-10-16 07:55:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct http_response_headers {
|
2017-05-17 02:37:50 +00:00
|
|
|
uint8_t minor_version;
|
2013-10-16 07:55:58 +00:00
|
|
|
http_size_t content_length;
|
|
|
|
http_size_t content_range_start; // range_end = range_start + content_length - 1
|
|
|
|
http_size_t resource_length; // size of entire resource
|
2018-03-19 07:37:23 +00:00
|
|
|
const char *location; // used with 301 and 302 responses
|
2017-10-19 23:25:21 +00:00
|
|
|
const struct mime_content_type *content_type; // one of the CONTENT_TYPE_ consts declared above
|
2013-10-16 07:55:58 +00:00
|
|
|
const char *boundary;
|
2015-10-13 08:14:46 +00:00
|
|
|
struct http_origin allow_origin;
|
2014-07-28 03:24:57 +00:00
|
|
|
const char *allow_methods;
|
|
|
|
const char *allow_headers;
|
2013-10-29 07:02:04 +00:00
|
|
|
struct http_www_authenticate www_authenticate;
|
2013-10-16 07:55:58 +00:00
|
|
|
};
|
|
|
|
|
2013-11-08 08:00:11 +00:00
|
|
|
struct http_content_generator_result {
|
|
|
|
size_t generated;
|
|
|
|
size_t need;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef int (HTTP_CONTENT_GENERATOR)(struct http_request *, unsigned char *, size_t, struct http_content_generator_result *);
|
2013-10-25 13:27:23 +00:00
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_response {
|
2015-11-30 13:52:45 +00:00
|
|
|
uint16_t status_code;
|
|
|
|
const char *reason;
|
2014-07-02 07:46:19 +00:00
|
|
|
struct {
|
|
|
|
const char *label;
|
|
|
|
struct json_atom value;
|
|
|
|
} result_extra[4];
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_response_headers header;
|
|
|
|
const char *content;
|
2013-11-08 08:00:11 +00:00
|
|
|
HTTP_CONTENT_GENERATOR *content_generator; // callback to produce more content
|
2013-10-16 07:55:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#define MIME_FILENAME_MAXLEN 127
|
|
|
|
|
|
|
|
struct mime_content_disposition {
|
|
|
|
char type[64];
|
|
|
|
char name[64];
|
|
|
|
char filename[MIME_FILENAME_MAXLEN + 1];
|
|
|
|
http_size_t size;
|
|
|
|
time_t creation_date;
|
|
|
|
time_t modification_date;
|
|
|
|
time_t read_date;
|
|
|
|
};
|
|
|
|
|
2013-10-28 11:57:27 +00:00
|
|
|
struct mime_part_headers {
|
|
|
|
http_size_t content_length;
|
|
|
|
struct mime_content_type content_type;
|
|
|
|
struct mime_content_disposition content_disposition;
|
|
|
|
};
|
|
|
|
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_mime_handler {
|
2013-12-21 12:52:47 +00:00
|
|
|
// All these functions may abort the request processing by returning an HTTP
|
|
|
|
// filure status code in the range 400-599 or by initiating an HTTP response
|
|
|
|
// directly (changing the phase from RECEIVE to TRANSMIT). They can return
|
|
|
|
// zero to indicate that parsing should proceed.
|
2013-12-30 07:39:56 +00:00
|
|
|
int (*handle_mime_preamble)(struct http_request *, char *, size_t);
|
2013-12-21 12:52:47 +00:00
|
|
|
int (*handle_mime_part_start)(struct http_request *);
|
|
|
|
int (*handle_mime_part_header)(struct http_request *, const struct mime_part_headers *);
|
2013-12-30 07:39:56 +00:00
|
|
|
int (*handle_mime_body)(struct http_request *, char *, size_t);
|
2013-12-21 12:52:47 +00:00
|
|
|
int (*handle_mime_part_end)(struct http_request *);
|
2013-12-30 07:39:56 +00:00
|
|
|
int (*handle_mime_epilogue)(struct http_request *, char *, size_t);
|
2013-10-16 07:55:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
struct http_request;
|
|
|
|
|
|
|
|
void http_request_init(struct http_request *r, int sockfd);
|
|
|
|
void http_request_free_response_buffer(struct http_request *r);
|
|
|
|
int http_request_set_response_bufsize(struct http_request *r, size_t bufsiz);
|
|
|
|
void http_request_finalise(struct http_request *r);
|
2013-11-19 05:08:45 +00:00
|
|
|
void http_request_pause_response(struct http_request *r, time_ms_t until);
|
2015-06-01 16:48:14 +00:00
|
|
|
void http_request_resume_response(struct http_request *r);
|
2017-10-19 23:25:21 +00:00
|
|
|
void http_request_response_static(struct http_request *r, int result, const struct mime_content_type *content_type, const char *body, uint64_t bytes);
|
|
|
|
void http_request_response_generated(struct http_request *r, int result, const struct mime_content_type *content_type, HTTP_CONTENT_GENERATOR *);
|
2013-10-16 07:55:58 +00:00
|
|
|
void http_request_simple_response(struct http_request *r, uint16_t result, const char *body);
|
2013-10-25 07:08:51 +00:00
|
|
|
|
2014-01-22 07:23:29 +00:00
|
|
|
typedef int (HTTP_CONTENT_GENERATOR_STRBUF_CHUNKER)(struct http_request *, strbuf);
|
|
|
|
int generate_http_content_from_strbuf_chunks(struct http_request *, char *, size_t, struct http_content_generator_result *, HTTP_CONTENT_GENERATOR_STRBUF_CHUNKER *);
|
|
|
|
|
2013-12-13 04:19:40 +00:00
|
|
|
typedef int HTTP_REQUEST_PARSER(struct http_request *);
|
2017-10-19 11:43:30 +00:00
|
|
|
typedef void HTTP_RENDERER(const struct http_request *, strbuf);
|
2013-10-16 07:55:58 +00:00
|
|
|
|
|
|
|
struct http_request {
|
|
|
|
struct sched_ent alarm; // MUST BE FIRST ELEMENT
|
2013-10-28 11:57:27 +00:00
|
|
|
// The following control the lifetime of this struct.
|
2013-11-19 05:08:45 +00:00
|
|
|
enum http_request_phase { RECEIVE, TRANSMIT, PAUSE, DONE } phase;
|
2013-10-28 11:57:27 +00:00
|
|
|
void (*finalise)(struct http_request *);
|
2016-10-31 04:51:22 +00:00
|
|
|
void (*release)(void*);
|
2015-06-22 08:43:06 +00:00
|
|
|
// Identify request from others being run. Monotonic counter feeds it. Only
|
|
|
|
// used for debugging when we write post-<uuid>.log files for multi-part form
|
|
|
|
// requests.
|
|
|
|
unsigned int uuid;
|
2015-07-13 08:53:36 +00:00
|
|
|
// These indirect debug flags allow different instances of HTTP servers to
|
|
|
|
// control their debug output independently of each other.
|
|
|
|
struct idebug debug;
|
|
|
|
struct idebug disable_tx;
|
2013-10-28 11:57:27 +00:00
|
|
|
// The following are used for parsing the HTTP request.
|
2013-10-16 07:55:58 +00:00
|
|
|
time_ms_t initiate_time; // time connection was initiated
|
|
|
|
time_ms_t idle_timeout; // disconnect if no bytes received for this long
|
2016-10-25 05:21:31 +00:00
|
|
|
struct socket_address client_addr; // caller may supply this
|
2013-10-28 11:57:27 +00:00
|
|
|
// The parsed HTTP request is accumulated into the following fields.
|
2013-10-16 07:55:58 +00:00
|
|
|
const char *verb; // points to nul terminated static string, "GET", "PUT", etc.
|
|
|
|
const char *path; // points into buffer; nul terminated
|
2015-08-17 10:17:25 +00:00
|
|
|
struct query_parameter {
|
|
|
|
const char *name; // points into buffer; nul terminated
|
|
|
|
const char *value; // points into buffer; nul terminated
|
|
|
|
}
|
|
|
|
query_parameters[10]; // can make this as big as needed, but not dynamic
|
2013-10-16 07:55:58 +00:00
|
|
|
uint8_t version_major; // m from from HTTP/m.n
|
|
|
|
uint8_t version_minor; // n from HTTP/m.n
|
|
|
|
struct http_request_headers request_header;
|
2013-10-28 11:57:27 +00:00
|
|
|
// Parsing is done by setting 'parser' to point to a series of parsing
|
|
|
|
// functions as the parsing state progresses.
|
2013-12-13 04:19:40 +00:00
|
|
|
HTTP_REQUEST_PARSER *parser; // current parser function
|
2017-05-22 06:47:10 +00:00
|
|
|
HTTP_REQUEST_PARSER *decoder; // decode any transfer encoding
|
2013-10-28 11:57:27 +00:00
|
|
|
// The caller may set these up, and they are invoked by the parser as request
|
|
|
|
// parsing reaches different stages.
|
2013-12-13 04:19:40 +00:00
|
|
|
HTTP_REQUEST_PARSER *handle_first_line; // called after first line is parsed
|
|
|
|
HTTP_REQUEST_PARSER *handle_headers; // called after all HTTP headers are parsed
|
|
|
|
HTTP_REQUEST_PARSER *handle_content_end; // called after all content is received
|
2013-10-28 11:57:27 +00:00
|
|
|
// The following are used for managing the buffer during RECEIVE phase.
|
2013-12-30 07:39:56 +00:00
|
|
|
char *reserved; // end of reserved data in buffer[]
|
|
|
|
char *received; // start of received data in buffer[]
|
2017-09-11 08:41:09 +00:00
|
|
|
char *end_decoded; // end of decoded data in buffer[]
|
2017-05-23 05:53:28 +00:00
|
|
|
char *decode_ptr; // end of received data in buffer[]
|
2017-05-22 06:47:10 +00:00
|
|
|
char *end_received; // end of received data in buffer[]
|
2013-12-30 07:39:56 +00:00
|
|
|
char *parsed; // start of unparsed data in buffer[]
|
|
|
|
char *cursor; // for parsing
|
2013-10-16 07:55:58 +00:00
|
|
|
http_size_t request_content_remaining;
|
2017-05-22 06:47:10 +00:00
|
|
|
enum chunk_state {CHUNK_SIZE, CHUNK_DATA, CHUNK_NEWLINE} chunk_state;
|
|
|
|
uint64_t chunk_size;
|
2013-10-28 11:57:27 +00:00
|
|
|
// The following are used for parsing a multipart body.
|
|
|
|
enum mime_state { START, PREAMBLE, HEADER, BODY, EPILOGUE } form_data_state;
|
|
|
|
struct http_mime_handler form_data;
|
|
|
|
struct mime_part_headers part_header;
|
|
|
|
http_size_t part_body_length;
|
|
|
|
// The following are used for constructing the response that will be sent in
|
|
|
|
// TRANSMIT phase.
|
2013-10-16 07:55:58 +00:00
|
|
|
struct http_response response;
|
2013-12-13 04:19:40 +00:00
|
|
|
HTTP_RENDERER *render_extra_headers;
|
2013-10-28 11:57:27 +00:00
|
|
|
// The following are used during TRANSMIT phase to control buffering and
|
|
|
|
// sending.
|
2013-10-16 07:55:58 +00:00
|
|
|
http_size_t response_length; // total response bytes (header + content)
|
|
|
|
http_size_t response_sent; // for counting up to response_length
|
|
|
|
char *response_buffer;
|
2013-11-07 13:09:24 +00:00
|
|
|
size_t response_buffer_need;
|
2013-10-16 07:55:58 +00:00
|
|
|
size_t response_buffer_size;
|
|
|
|
size_t response_buffer_length;
|
|
|
|
size_t response_buffer_sent;
|
|
|
|
void (*response_free_buffer)(void*);
|
2013-10-28 11:57:27 +00:00
|
|
|
// This buffer is used during RECEIVE and TRANSMIT phase.
|
2013-10-16 07:55:58 +00:00
|
|
|
char buffer[8 * 1024];
|
|
|
|
};
|
|
|
|
|
2015-08-17 10:17:25 +00:00
|
|
|
/* Return the nul-terminated string value of a given query parameter: NULL if
|
|
|
|
* no such parameter was supplied; HTTP_REQUEST_PARAM_NOVALUE if the parameter
|
|
|
|
* was supplied without an '=value' part.
|
|
|
|
*/
|
|
|
|
const char *http_request_get_query_param(struct http_request *r, const char *name);
|
|
|
|
extern const char HTTP_REQUEST_PARAM_NOVALUE[];
|
|
|
|
|
2013-12-04 06:44:14 +00:00
|
|
|
#endif // __SERVAL_DNA__HTTP_SERVER_H
|