diff --git a/src/CPPLINT.cfg b/src/CPPLINT.cfg new file mode 100644 index 00000000..6809e557 --- /dev/null +++ b/src/CPPLINT.cfg @@ -0,0 +1 @@ +exclude_files=sha1\.c diff --git a/src/coap.c b/src/coap.c new file mode 100644 index 00000000..e7d85c3a --- /dev/null +++ b/src/coap.c @@ -0,0 +1,597 @@ +/* + * Copyright (c) 2015 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +#include "mongoose/src/internal.h" +#include "mongoose/src/coap.h" + +#if MG_ENABLE_COAP + +void mg_coap_free_options(struct mg_coap_message *cm) { + while (cm->options != NULL) { + struct mg_coap_option *next = cm->options->next; + MG_FREE(cm->options); + cm->options = next; + } +} + +struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm, + uint32_t number, char *value, + size_t len) { + struct mg_coap_option *new_option = + (struct mg_coap_option *) MG_CALLOC(1, sizeof(*new_option)); + + new_option->number = number; + new_option->value.p = value; + new_option->value.len = len; + + if (cm->options == NULL) { + cm->options = cm->optiomg_tail = new_option; + } else { + /* + * A very simple attention to help clients to compose options: + * CoAP wants to see options ASC ordered. + * Could be change by using sort in coap_compose + */ + if (cm->optiomg_tail->number <= new_option->number) { + /* if option is already ordered just add it */ + cm->optiomg_tail = cm->optiomg_tail->next = new_option; + } else { + /* looking for appropriate position */ + struct mg_coap_option *current_opt = cm->options; + struct mg_coap_option *prev_opt = 0; + + while (current_opt != NULL) { + if (current_opt->number > new_option->number) { + break; + } + prev_opt = current_opt; + current_opt = current_opt->next; + } + + if (prev_opt != NULL) { + prev_opt->next = new_option; + new_option->next = current_opt; + } else { + /* insert new_option to the beginning */ + new_option->next = cm->options; + cm->options = new_option; + } + } + } + + return new_option; +} + +/* + * Fills CoAP header in mg_coap_message. + * + * Helper function. + */ +static char *coap_parse_header(char *ptr, struct mbuf *io, + struct mg_coap_message *cm) { + if (io->len < sizeof(uint32_t)) { + cm->flags |= MG_COAP_NOT_ENOUGH_DATA; + return NULL; + } + + /* + * Version (Ver): 2-bit unsigned integer. Indicates the CoAP version + * number. Implementations of this specification MUST set this field + * to 1 (01 binary). Other values are reserved for future versions. + * Messages with unknown version numbers MUST be silently ignored. + */ + if (((uint8_t) *ptr >> 6) != 1) { + cm->flags |= MG_COAP_IGNORE; + return NULL; + } + + /* + * Type (T): 2-bit unsigned integer. Indicates if this message is of + * type Confirmable (0), Non-confirmable (1), Acknowledgement (2), or + * Reset (3). + */ + cm->msg_type = ((uint8_t) *ptr & 0x30) >> 4; + cm->flags |= MG_COAP_MSG_TYPE_FIELD; + + /* + * Token Length (TKL): 4-bit unsigned integer. Indicates the length of + * the variable-length Token field (0-8 bytes). Lengths 9-15 are + * reserved, MUST NOT be sent, and MUST be processed as a message + * format error. + */ + cm->token.len = *ptr & 0x0F; + if (cm->token.len > 8) { + cm->flags |= MG_COAP_FORMAT_ERROR; + return NULL; + } + + ptr++; + + /* + * Code: 8-bit unsigned integer, split into a 3-bit class (most + * significant bits) and a 5-bit detail (least significant bits) + */ + cm->code_class = (uint8_t) *ptr >> 5; + cm->code_detail = *ptr & 0x1F; + cm->flags |= (MG_COAP_CODE_CLASS_FIELD | MG_COAP_CODE_DETAIL_FIELD); + + ptr++; + + /* Message ID: 16-bit unsigned integer in network byte order. */ + cm->msg_id = (uint8_t) *ptr << 8 | (uint8_t) * (ptr + 1); + cm->flags |= MG_COAP_MSG_ID_FIELD; + + ptr += 2; + + return ptr; +} + +/* + * Fills token information in mg_coap_message. + * + * Helper function. + */ +static char *coap_get_token(char *ptr, struct mbuf *io, + struct mg_coap_message *cm) { + if (cm->token.len != 0) { + if (ptr + cm->token.len > io->buf + io->len) { + cm->flags |= MG_COAP_NOT_ENOUGH_DATA; + return NULL; + } else { + cm->token.p = ptr; + ptr += cm->token.len; + cm->flags |= MG_COAP_TOKEN_FIELD; + } + } + + return ptr; +} + +/* + * Returns Option Delta or Length. + * + * Helper function. + */ +static int coap_get_ext_opt(char *ptr, struct mbuf *io, uint16_t *opt_info) { + int ret = 0; + + if (*opt_info == 13) { + /* + * 13: An 8-bit unsigned integer follows the initial byte and + * indicates the Option Delta/Length minus 13. + */ + if (ptr < io->buf + io->len) { + *opt_info = (uint8_t) *ptr + 13; + ret = sizeof(uint8_t); + } else { + ret = -1; /* LCOV_EXCL_LINE */ + } + } else if (*opt_info == 14) { + /* + * 14: A 16-bit unsigned integer in network byte order follows the + * initial byte and indicates the Option Delta/Length minus 269. + */ + if (ptr + sizeof(uint8_t) < io->buf + io->len) { + *opt_info = ((uint8_t) *ptr << 8 | (uint8_t) * (ptr + 1)) + 269; + ret = sizeof(uint16_t); + } else { + ret = -1; /* LCOV_EXCL_LINE */ + } + } + + return ret; +} + +/* + * Fills options in mg_coap_message. + * + * Helper function. + * + * General options format: + * +---------------+---------------+ + * | Option Delta | Option Length | 1 byte + * +---------------+---------------+ + * \ Option Delta (extended) \ 0-2 bytes + * +-------------------------------+ + * / Option Length (extended) \ 0-2 bytes + * +-------------------------------+ + * \ Option Value \ 0 or more bytes + * +-------------------------------+ + */ +static char *coap_get_options(char *ptr, struct mbuf *io, + struct mg_coap_message *cm) { + uint16_t prev_opt = 0; + + if (ptr == io->buf + io->len) { + /* end of packet, ok */ + return NULL; + } + + /* 0xFF is payload marker */ + while (ptr < io->buf + io->len && (uint8_t) *ptr != 0xFF) { + uint16_t option_delta, option_lenght; + int optinfo_len; + + /* Option Delta: 4-bit unsigned integer */ + option_delta = ((uint8_t) *ptr & 0xF0) >> 4; + /* Option Length: 4-bit unsigned integer */ + option_lenght = *ptr & 0x0F; + + if (option_delta == 15 || option_lenght == 15) { + /* + * 15: Reserved for future use. If the field is set to this value, + * it MUST be processed as a message format error + */ + cm->flags |= MG_COAP_FORMAT_ERROR; + break; + } + + ptr++; + + /* check for extended option delta */ + optinfo_len = coap_get_ext_opt(ptr, io, &option_delta); + if (optinfo_len == -1) { + cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */ + break; /* LCOV_EXCL_LINE */ + } + + ptr += optinfo_len; + + /* check or extended option lenght */ + optinfo_len = coap_get_ext_opt(ptr, io, &option_lenght); + if (optinfo_len == -1) { + cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */ + break; /* LCOV_EXCL_LINE */ + } + + ptr += optinfo_len; + + /* + * Instead of specifying the Option Number directly, the instances MUST + * appear in order of their Option Numbers and a delta encoding is used + * between them. + */ + option_delta += prev_opt; + + mg_coap_add_option(cm, option_delta, ptr, option_lenght); + + prev_opt = option_delta; + + if (ptr + option_lenght > io->buf + io->len) { + cm->flags |= MG_COAP_NOT_ENOUGH_DATA; /* LCOV_EXCL_LINE */ + break; /* LCOV_EXCL_LINE */ + } + + ptr += option_lenght; + } + + if ((cm->flags & MG_COAP_ERROR) != 0) { + mg_coap_free_options(cm); + return NULL; + } + + cm->flags |= MG_COAP_OPTIOMG_FIELD; + + if (ptr == io->buf + io->len) { + /* end of packet, ok */ + return NULL; + } + + ptr++; + + return ptr; +} + +uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm) { + char *ptr; + + memset(cm, 0, sizeof(*cm)); + + if ((ptr = coap_parse_header(io->buf, io, cm)) == NULL) { + return cm->flags; + } + + if ((ptr = coap_get_token(ptr, io, cm)) == NULL) { + return cm->flags; + } + + if ((ptr = coap_get_options(ptr, io, cm)) == NULL) { + return cm->flags; + } + + /* the rest is payload */ + cm->payload.len = io->len - (ptr - io->buf); + if (cm->payload.len != 0) { + cm->payload.p = ptr; + cm->flags |= MG_COAP_PAYLOAD_FIELD; + } + + return cm->flags; +} + +/* + * Calculates extended size of given Opt Number/Length in coap message. + * + * Helper function. + */ +static size_t coap_get_ext_opt_size(uint32_t value) { + int ret = 0; + + if (value >= 13 && value <= 0xFF + 13) { + ret = sizeof(uint8_t); + } else if (value > 0xFF + 13 && value <= 0xFFFF + 269) { + ret = sizeof(uint16_t); + } + + return ret; +} + +/* + * Splits given Opt Number/Length into base and ext values. + * + * Helper function. + */ +static int coap_split_opt(uint32_t value, uint8_t *base, uint16_t *ext) { + int ret = 0; + + if (value < 13) { + *base = value; + } else if (value >= 13 && value <= 0xFF + 13) { + *base = 13; + *ext = value - 13; + ret = sizeof(uint8_t); + } else if (value > 0xFF + 13 && value <= 0xFFFF + 269) { + *base = 14; + *ext = value - 269; + ret = sizeof(uint16_t); + } + + return ret; +} + +/* + * Puts uint16_t (in network order) into given char stream. + * + * Helper function. + */ +static char *coap_add_uint16(char *ptr, uint16_t val) { + *ptr = val >> 8; + ptr++; + *ptr = val & 0x00FF; + ptr++; + return ptr; +} + +/* + * Puts extended value of Opt Number/Length into given char stream. + * + * Helper function. + */ +static char *coap_add_opt_info(char *ptr, uint16_t val, size_t len) { + if (len == sizeof(uint8_t)) { + *ptr = (char) val; + ptr++; + } else if (len == sizeof(uint16_t)) { + ptr = coap_add_uint16(ptr, val); + } + + return ptr; +} + +/* + * Verifies given mg_coap_message and calculates message size for it. + * + * Helper function. + */ +static uint32_t coap_calculate_packet_size(struct mg_coap_message *cm, + size_t *len) { + struct mg_coap_option *opt; + uint32_t prev_opt_number; + + *len = 4; /* header */ + if (cm->msg_type > MG_COAP_MSG_MAX) { + return MG_COAP_ERROR | MG_COAP_MSG_TYPE_FIELD; + } + if (cm->token.len > 8) { + return MG_COAP_ERROR | MG_COAP_TOKEN_FIELD; + } + if (cm->code_class > 7) { + return MG_COAP_ERROR | MG_COAP_CODE_CLASS_FIELD; + } + if (cm->code_detail > 31) { + return MG_COAP_ERROR | MG_COAP_CODE_DETAIL_FIELD; + } + + *len += cm->token.len; + if (cm->payload.len != 0) { + *len += cm->payload.len + 1; /* ... + 1; add payload marker */ + } + + opt = cm->options; + prev_opt_number = 0; + while (opt != NULL) { + *len += 1; /* basic delta/length */ + *len += coap_get_ext_opt_size(opt->number - prev_opt_number); + *len += coap_get_ext_opt_size((uint32_t) opt->value.len); + /* + * Current implementation performs check if + * option_number > previous option_number and produces an error + * TODO(alashkin): write design doc with limitations + * May be resorting is more suitable solution. + */ + if ((opt->next != NULL && opt->number > opt->next->number) || + opt->value.len > 0xFFFF + 269 || + opt->number - prev_opt_number > 0xFFFF + 269) { + return MG_COAP_ERROR | MG_COAP_OPTIOMG_FIELD; + } + *len += opt->value.len; + prev_opt_number = opt->number; + opt = opt->next; + } + + return 0; +} + +uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io) { + struct mg_coap_option *opt; + uint32_t res, prev_opt_number; + size_t prev_io_len, packet_size; + char *ptr; + + res = coap_calculate_packet_size(cm, &packet_size); + if (res != 0) { + return res; + } + + /* saving previous lenght to handle non-empty mbuf */ + prev_io_len = io->len; + if (mbuf_append(io, NULL, packet_size) == 0) return MG_COAP_ERROR; + ptr = io->buf + prev_io_len; + + /* + * since cm is verified, it is possible to use bits shift operator + * without additional zeroing of unused bits + */ + + /* ver: 2 bits, msg_type: 2 bits, toklen: 4 bits */ + *ptr = (1 << 6) | (cm->msg_type << 4) | (uint8_t)(cm->token.len); + ptr++; + + /* code class: 3 bits, code detail: 5 bits */ + *ptr = (cm->code_class << 5) | (cm->code_detail); + ptr++; + + ptr = coap_add_uint16(ptr, cm->msg_id); + + if (cm->token.len != 0) { + memcpy(ptr, cm->token.p, cm->token.len); + ptr += cm->token.len; + } + + opt = cm->options; + prev_opt_number = 0; + while (opt != NULL) { + uint8_t delta_base = 0, length_base = 0; + uint16_t delta_ext = 0, length_ext = 0; + + size_t opt_delta_len = + coap_split_opt(opt->number - prev_opt_number, &delta_base, &delta_ext); + size_t opt_lenght_len = + coap_split_opt((uint32_t) opt->value.len, &length_base, &length_ext); + + *ptr = (delta_base << 4) | length_base; + ptr++; + + ptr = coap_add_opt_info(ptr, delta_ext, opt_delta_len); + ptr = coap_add_opt_info(ptr, length_ext, opt_lenght_len); + + if (opt->value.len != 0) { + memcpy(ptr, opt->value.p, opt->value.len); + ptr += opt->value.len; + } + + prev_opt_number = opt->number; + opt = opt->next; + } + + if (cm->payload.len != 0) { + *ptr = (char) -1; + ptr++; + memcpy(ptr, cm->payload.p, cm->payload.len); + } + + return 0; +} + +uint32_t mg_coap_send_message(struct mg_connection *nc, + struct mg_coap_message *cm) { + struct mbuf packet_out; + uint32_t compose_res; + + mbuf_init(&packet_out, 0); + compose_res = mg_coap_compose(cm, &packet_out); + if (compose_res != 0) { + return compose_res; /* LCOV_EXCL_LINE */ + } + + mg_send(nc, packet_out.buf, (int) packet_out.len); + mbuf_free(&packet_out); + + return 0; +} + +uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id) { + struct mg_coap_message cm; + memset(&cm, 0, sizeof(cm)); + cm.msg_type = MG_COAP_MSG_ACK; + cm.msg_id = msg_id; + + return mg_coap_send_message(nc, &cm); +} + +static void coap_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct mbuf *io = &nc->recv_mbuf; + struct mg_coap_message cm; + uint32_t parse_res; + + memset(&cm, 0, sizeof(cm)); + + nc->handler(nc, ev, ev_data MG_UD_ARG(user_data)); + + switch (ev) { + case MG_EV_RECV: + parse_res = mg_coap_parse(io, &cm); + if ((parse_res & MG_COAP_IGNORE) == 0) { + if ((cm.flags & MG_COAP_NOT_ENOUGH_DATA) != 0) { + /* + * Since we support UDP only + * MG_COAP_NOT_ENOUGH_DATA == MG_COAP_FORMAT_ERROR + */ + cm.flags |= MG_COAP_FORMAT_ERROR; /* LCOV_EXCL_LINE */ + } /* LCOV_EXCL_LINE */ + nc->handler(nc, MG_COAP_EVENT_BASE + cm.msg_type, + &cm MG_UD_ARG(user_data)); + } + + mg_coap_free_options(&cm); + mbuf_remove(io, io->len); + break; + } +} +/* + * Attach built-in CoAP event handler to the given connection. + * + * The user-defined event handler will receive following extra events: + * + * - MG_EV_COAP_CON + * - MG_EV_COAP_NOC + * - MG_EV_COAP_ACK + * - MG_EV_COAP_RST + */ +int mg_set_protocol_coap(struct mg_connection *nc) { + /* supports UDP only */ + if ((nc->flags & MG_F_UDP) == 0) { + return -1; + } + + nc->proto_handler = coap_handler; + + return 0; +} + +#endif /* MG_ENABLE_COAP */ diff --git a/src/coap.h b/src/coap.h new file mode 100644 index 00000000..296933fe --- /dev/null +++ b/src/coap.h @@ -0,0 +1,165 @@ +/* + * Copyright (c) 2015 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +/* + * === CoAP API reference + * + * CoAP message format: + * + * ``` + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * |Ver| T | TKL | Code | Message ID | Token (if any, TKL bytes) ... + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * | Options (if any) ... |1 1 1 1 1 1 1 1| Payload (if any) ... + * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+- + * ``` + */ + +#ifndef CS_MONGOOSE_SRC_COAP_H_ +#define CS_MONGOOSE_SRC_COAP_H_ + +#if MG_ENABLE_COAP + +#define MG_COAP_MSG_TYPE_FIELD 0x2 +#define MG_COAP_CODE_CLASS_FIELD 0x4 +#define MG_COAP_CODE_DETAIL_FIELD 0x8 +#define MG_COAP_MSG_ID_FIELD 0x10 +#define MG_COAP_TOKEN_FIELD 0x20 +#define MG_COAP_OPTIOMG_FIELD 0x40 +#define MG_COAP_PAYLOAD_FIELD 0x80 + +#define MG_COAP_ERROR 0x10000 +#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000) +#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000) +#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000) +#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000) + +#define MG_COAP_MSG_CON 0 +#define MG_COAP_MSG_NOC 1 +#define MG_COAP_MSG_ACK 2 +#define MG_COAP_MSG_RST 3 +#define MG_COAP_MSG_MAX 3 + +#define MG_COAP_CODECLASS_REQUEST 0 +#define MG_COAP_CODECLASS_RESP_OK 2 +#define MG_COAP_CODECLASS_CLIENT_ERR 4 +#define MG_COAP_CODECLASS_SRV_ERR 5 + +#define MG_COAP_EVENT_BASE 300 +#define MG_EV_COAP_CON (MG_COAP_EVENT_BASE + MG_COAP_MSG_CON) +#define MG_EV_COAP_NOC (MG_COAP_EVENT_BASE + MG_COAP_MSG_NOC) +#define MG_EV_COAP_ACK (MG_COAP_EVENT_BASE + MG_COAP_MSG_ACK) +#define MG_EV_COAP_RST (MG_COAP_EVENT_BASE + MG_COAP_MSG_RST) + +/* + * CoAP options. + * Use mg_coap_add_option and mg_coap_free_options + * for creation and destruction. + */ +struct mg_coap_option { + struct mg_coap_option *next; + uint32_t number; + struct mg_str value; +}; + +/* CoAP message. See RFC 7252 for details. */ +struct mg_coap_message { + uint32_t flags; + uint8_t msg_type; + uint8_t code_class; + uint8_t code_detail; + uint16_t msg_id; + struct mg_str token; + struct mg_coap_option *options; + struct mg_str payload; + struct mg_coap_option *optiomg_tail; +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* Sets CoAP protocol handler - triggers CoAP specific events. */ +int mg_set_protocol_coap(struct mg_connection *nc); + +/* + * Adds a new option to mg_coap_message structure. + * Returns pointer to the newly created option. + * Note: options must be freed by using mg_coap_free_options + */ +struct mg_coap_option *mg_coap_add_option(struct mg_coap_message *cm, + uint32_t number, char *value, + size_t len); + +/* + * Frees the memory allocated for options. + * If the cm parameter doesn't contain any option it does nothing. + */ +void mg_coap_free_options(struct mg_coap_message *cm); + +/* + * Composes a CoAP message from `mg_coap_message` + * and sends it into `nc` connection. + * Returns 0 on success. On error, it is a bitmask: + * + * - `#define MG_COAP_ERROR 0x10000` + * - `#define MG_COAP_FORMAT_ERROR (MG_COAP_ERROR | 0x20000)` + * - `#define MG_COAP_IGNORE (MG_COAP_ERROR | 0x40000)` + * - `#define MG_COAP_NOT_ENOUGH_DATA (MG_COAP_ERROR | 0x80000)` + * - `#define MG_COAP_NETWORK_ERROR (MG_COAP_ERROR | 0x100000)` + */ +uint32_t mg_coap_send_message(struct mg_connection *nc, + struct mg_coap_message *cm); + +/* + * Composes CoAP acknowledgement from `mg_coap_message` + * and sends it into `nc` connection. + * Return value: see `mg_coap_send_message()` + */ +uint32_t mg_coap_send_ack(struct mg_connection *nc, uint16_t msg_id); + +/* + * Parses CoAP message and fills mg_coap_message and returns cm->flags. + * This is a helper function. + * + * NOTE: usually CoAP works over UDP, so lack of data means format error. + * But, in theory, it is possible to use CoAP over TCP (according to RFC) + * + * The caller has to check results and treat COAP_NOT_ENOUGH_DATA according to + * underlying protocol: + * + * - in case of UDP COAP_NOT_ENOUGH_DATA means COAP_FORMAT_ERROR, + * - in case of TCP client can try to receive more data + * + * Return value: see `mg_coap_send_message()` + */ +uint32_t mg_coap_parse(struct mbuf *io, struct mg_coap_message *cm); + +/* + * Composes CoAP message from mg_coap_message structure. + * This is a helper function. + * Return value: see `mg_coap_send_message()` + */ +uint32_t mg_coap_compose(struct mg_coap_message *cm, struct mbuf *io); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_COAP */ + +#endif /* CS_MONGOOSE_SRC_COAP_H_ */ diff --git a/src/common.h b/src/common.h new file mode 100644 index 00000000..a3812185 --- /dev/null +++ b/src/common.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2004-2013 Sergey Lyubka + * Copyright (c) 2013-2015 Cesanta Software Limited + * All rights reserved + * + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +#ifndef CS_MONGOOSE_SRC_COMMON_H_ +#define CS_MONGOOSE_SRC_COMMON_H_ + +#define MG_VERSION "6.10" + +/* Local tweaks, applied before any of Mongoose's own headers. */ +#ifdef MG_LOCALS +#include +#endif + +#endif /* CS_MONGOOSE_SRC_COMMON_H_ */ diff --git a/src/dns.c b/src/dns.c new file mode 100644 index 00000000..93752ebc --- /dev/null +++ b/src/dns.c @@ -0,0 +1,377 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_DNS + +#include "mongoose/src/internal.h" +#include "mongoose/src/dns.h" + +static int mg_dns_tid = 0xa0; + +struct mg_dns_header { + uint16_t transaction_id; + uint16_t flags; + uint16_t num_questions; + uint16_t num_answers; + uint16_t num_authority_prs; + uint16_t num_other_prs; +}; + +struct mg_dns_resource_record *mg_dns_next_record( + struct mg_dns_message *msg, int query, + struct mg_dns_resource_record *prev) { + struct mg_dns_resource_record *rr; + + for (rr = (prev == NULL ? msg->answers : prev + 1); + rr - msg->answers < msg->num_answers; rr++) { + if (rr->rtype == query) { + return rr; + } + } + return NULL; +} + +int mg_dns_parse_record_data(struct mg_dns_message *msg, + struct mg_dns_resource_record *rr, void *data, + size_t data_len) { + switch (rr->rtype) { + case MG_DNS_A_RECORD: + if (data_len < sizeof(struct in_addr)) { + return -1; + } + if (rr->rdata.p + data_len > msg->pkt.p + msg->pkt.len) { + return -1; + } + memcpy(data, rr->rdata.p, data_len); + return 0; +#if MG_ENABLE_IPV6 + case MG_DNS_AAAA_RECORD: + if (data_len < sizeof(struct in6_addr)) { + return -1; /* LCOV_EXCL_LINE */ + } + memcpy(data, rr->rdata.p, data_len); + return 0; +#endif + case MG_DNS_CNAME_RECORD: + mg_dns_uncompress_name(msg, &rr->rdata, (char *) data, data_len); + return 0; + } + + return -1; +} + +int mg_dns_insert_header(struct mbuf *io, size_t pos, + struct mg_dns_message *msg) { + struct mg_dns_header header; + + memset(&header, 0, sizeof(header)); + header.transaction_id = msg->transaction_id; + header.flags = htons(msg->flags); + header.num_questions = htons(msg->num_questions); + header.num_answers = htons(msg->num_answers); + + return mbuf_insert(io, pos, &header, sizeof(header)); +} + +int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg) { + unsigned char *begin, *end; + struct mg_dns_resource_record *last_q; + if (msg->num_questions <= 0) return 0; + begin = (unsigned char *) msg->pkt.p + sizeof(struct mg_dns_header); + last_q = &msg->questions[msg->num_questions - 1]; + end = (unsigned char *) last_q->name.p + last_q->name.len + 4; + return mbuf_append(io, begin, end - begin); +} + +int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len) { + const char *s; + unsigned char n; + size_t pos = io->len; + + do { + if ((s = strchr(name, '.')) == NULL) { + s = name + len; + } + + if (s - name > 127) { + return -1; /* TODO(mkm) cover */ + } + n = s - name; /* chunk length */ + mbuf_append(io, &n, 1); /* send length */ + mbuf_append(io, name, n); + + if (*s == '.') { + n++; + } + + name += n; + len -= n; + } while (*s != '\0'); + mbuf_append(io, "\0", 1); /* Mark end of host name */ + + return io->len - pos; +} + +int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, + const char *name, size_t nlen, const void *rdata, + size_t rlen) { + size_t pos = io->len; + uint16_t u16; + uint32_t u32; + + if (rr->kind == MG_DNS_INVALID_RECORD) { + return -1; /* LCOV_EXCL_LINE */ + } + + if (mg_dns_encode_name(io, name, nlen) == -1) { + return -1; + } + + u16 = htons(rr->rtype); + mbuf_append(io, &u16, 2); + u16 = htons(rr->rclass); + mbuf_append(io, &u16, 2); + + if (rr->kind == MG_DNS_ANSWER) { + u32 = htonl(rr->ttl); + mbuf_append(io, &u32, 4); + + if (rr->rtype == MG_DNS_CNAME_RECORD) { + int clen; + /* fill size after encoding */ + size_t off = io->len; + mbuf_append(io, &u16, 2); + if ((clen = mg_dns_encode_name(io, (const char *) rdata, rlen)) == -1) { + return -1; + } + u16 = clen; + io->buf[off] = u16 >> 8; + io->buf[off + 1] = u16 & 0xff; + } else { + u16 = htons((uint16_t) rlen); + mbuf_append(io, &u16, 2); + mbuf_append(io, rdata, rlen); + } + } + + return io->len - pos; +} + +void mg_send_dns_query(struct mg_connection *nc, const char *name, + int query_type) { + struct mg_dns_message *msg = + (struct mg_dns_message *) MG_CALLOC(1, sizeof(*msg)); + struct mbuf pkt; + struct mg_dns_resource_record *rr = &msg->questions[0]; + + DBG(("%s %d", name, query_type)); + + mbuf_init(&pkt, 64 /* Start small, it'll grow as needed. */); + + msg->transaction_id = ++mg_dns_tid; + msg->flags = 0x100; + msg->num_questions = 1; + + mg_dns_insert_header(&pkt, 0, msg); + + rr->rtype = query_type; + rr->rclass = 1; /* Class: inet */ + rr->kind = MG_DNS_QUESTION; + + if (mg_dns_encode_record(&pkt, rr, name, strlen(name), NULL, 0) == -1) { + /* TODO(mkm): return an error code */ + goto cleanup; /* LCOV_EXCL_LINE */ + } + + /* TCP DNS requires messages to be prefixed with len */ + if (!(nc->flags & MG_F_UDP)) { + uint16_t len = htons((uint16_t) pkt.len); + mbuf_insert(&pkt, 0, &len, 2); + } + + mg_send(nc, pkt.buf, pkt.len); + mbuf_free(&pkt); + +cleanup: + MG_FREE(msg); +} + +static unsigned char *mg_parse_dns_resource_record( + unsigned char *data, unsigned char *end, struct mg_dns_resource_record *rr, + int reply) { + unsigned char *name = data; + int chunk_len, data_len; + + while (data < end && (chunk_len = *data)) { + if (((unsigned char *) data)[0] & 0xc0) { + data += 1; + break; + } + data += chunk_len + 1; + } + + if (data > end - 5) { + return NULL; + } + + rr->name.p = (char *) name; + rr->name.len = data - name + 1; + data++; + + rr->rtype = data[0] << 8 | data[1]; + data += 2; + + rr->rclass = data[0] << 8 | data[1]; + data += 2; + + rr->kind = reply ? MG_DNS_ANSWER : MG_DNS_QUESTION; + if (reply) { + if (data >= end - 6) { + return NULL; + } + + rr->ttl = (uint32_t) data[0] << 24 | (uint32_t) data[1] << 16 | + data[2] << 8 | data[3]; + data += 4; + + data_len = *data << 8 | *(data + 1); + data += 2; + + rr->rdata.p = (char *) data; + rr->rdata.len = data_len; + data += data_len; + } + return data; +} + +int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg) { + struct mg_dns_header *header = (struct mg_dns_header *) buf; + unsigned char *data = (unsigned char *) buf + sizeof(*header); + unsigned char *end = (unsigned char *) buf + len; + int i; + + memset(msg, 0, sizeof(*msg)); + msg->pkt.p = buf; + msg->pkt.len = len; + + if (len < (int) sizeof(*header)) return -1; + + msg->transaction_id = header->transaction_id; + msg->flags = ntohs(header->flags); + msg->num_questions = ntohs(header->num_questions); + if (msg->num_questions > (int) ARRAY_SIZE(msg->questions)) { + msg->num_questions = (int) ARRAY_SIZE(msg->questions); + } + msg->num_answers = ntohs(header->num_answers); + if (msg->num_answers > (int) ARRAY_SIZE(msg->answers)) { + msg->num_answers = (int) ARRAY_SIZE(msg->answers); + } + + for (i = 0; i < msg->num_questions; i++) { + data = mg_parse_dns_resource_record(data, end, &msg->questions[i], 0); + if (data == NULL) return -1; + } + + for (i = 0; i < msg->num_answers; i++) { + data = mg_parse_dns_resource_record(data, end, &msg->answers[i], 1); + if (data == NULL) return -1; + } + + return 0; +} + +size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name, + char *dst, int dst_len) { + int chunk_len, num_ptrs = 0; + char *old_dst = dst; + const unsigned char *data = (unsigned char *) name->p; + const unsigned char *end = (unsigned char *) msg->pkt.p + msg->pkt.len; + + if (data >= end) { + return 0; + } + + while ((chunk_len = *data++)) { + int leeway = dst_len - (dst - old_dst); + if (data >= end) { + return 0; + } + + if ((chunk_len & 0xc0) == 0xc0) { + uint16_t off = (data[-1] & (~0xc0)) << 8 | data[0]; + if (off >= msg->pkt.len) { + return 0; + } + /* Basic circular loop avoidance: allow up to 16 pointer hops. */ + if (++num_ptrs > 15) { + return 0; + } + data = (unsigned char *) msg->pkt.p + off; + continue; + } + if (chunk_len > 63) { + return 0; + } + if (chunk_len > leeway) { + chunk_len = leeway; + } + + if (data + chunk_len >= end) { + return 0; + } + + memcpy(dst, data, chunk_len); + data += chunk_len; + dst += chunk_len; + leeway -= chunk_len; + if (leeway == 0) { + return dst - old_dst; + } + *dst++ = '.'; + } + + if (dst != old_dst) { + *--dst = 0; + } + return dst - old_dst; +} + +static void dns_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct mbuf *io = &nc->recv_mbuf; + struct mg_dns_message msg; + + /* Pass low-level events to the user handler */ + nc->handler(nc, ev, ev_data MG_UD_ARG(user_data)); + + switch (ev) { + case MG_EV_RECV: + if (!(nc->flags & MG_F_UDP)) { + mbuf_remove(&nc->recv_mbuf, 2); + } + if (mg_parse_dns(nc->recv_mbuf.buf, nc->recv_mbuf.len, &msg) == -1) { + /* reply + recursion allowed + format error */ + memset(&msg, 0, sizeof(msg)); + msg.flags = 0x8081; + mg_dns_insert_header(io, 0, &msg); + if (!(nc->flags & MG_F_UDP)) { + uint16_t len = htons((uint16_t) io->len); + mbuf_insert(io, 0, &len, 2); + } + mg_send(nc, io->buf, io->len); + } else { + /* Call user handler with parsed message */ + nc->handler(nc, MG_DNS_MESSAGE, &msg MG_UD_ARG(user_data)); + } + mbuf_remove(io, io->len); + break; + } +} + +void mg_set_protocol_dns(struct mg_connection *nc) { + nc->proto_handler = dns_handler; +} + +#endif /* MG_ENABLE_DNS */ diff --git a/src/dns.h b/src/dns.h new file mode 100644 index 00000000..02182e4a --- /dev/null +++ b/src/dns.h @@ -0,0 +1,164 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === DNS API reference + */ + +#ifndef CS_MONGOOSE_SRC_DNS_H_ +#define CS_MONGOOSE_SRC_DNS_H_ + +#include "mongoose/src/net.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MG_DNS_A_RECORD 0x01 /* Lookup IP address */ +#define MG_DNS_CNAME_RECORD 0x05 /* Lookup CNAME */ +#define MG_DNS_PTR_RECORD 0x0c /* Lookup PTR */ +#define MG_DNS_TXT_RECORD 0x10 /* Lookup TXT */ +#define MG_DNS_AAAA_RECORD 0x1c /* Lookup IPv6 address */ +#define MG_DNS_SRV_RECORD 0x21 /* Lookup SRV */ +#define MG_DNS_MX_RECORD 0x0f /* Lookup mail server for domain */ +#define MG_DNS_ANY_RECORD 0xff +#define MG_DNS_NSEC_RECORD 0x2f + +#define MG_MAX_DNS_QUESTIONS 32 +#define MG_MAX_DNS_ANSWERS 32 + +#define MG_DNS_MESSAGE 100 /* High-level DNS message event */ + +enum mg_dns_resource_record_kind { + MG_DNS_INVALID_RECORD = 0, + MG_DNS_QUESTION, + MG_DNS_ANSWER +}; + +/* DNS resource record. */ +struct mg_dns_resource_record { + struct mg_str name; /* buffer with compressed name */ + int rtype; + int rclass; + int ttl; + enum mg_dns_resource_record_kind kind; + struct mg_str rdata; /* protocol data (can be a compressed name) */ +}; + +/* DNS message (request and response). */ +struct mg_dns_message { + struct mg_str pkt; /* packet body */ + uint16_t flags; + uint16_t transaction_id; + int num_questions; + int num_answers; + struct mg_dns_resource_record questions[MG_MAX_DNS_QUESTIONS]; + struct mg_dns_resource_record answers[MG_MAX_DNS_ANSWERS]; +}; + +struct mg_dns_resource_record *mg_dns_next_record( + struct mg_dns_message *msg, int query, struct mg_dns_resource_record *prev); + +/* + * Parses the record data from a DNS resource record. + * + * - A: struct in_addr *ina + * - AAAA: struct in6_addr *ina + * - CNAME: char buffer + * + * Returns -1 on error. + * + * TODO(mkm): MX + */ +int mg_dns_parse_record_data(struct mg_dns_message *msg, + struct mg_dns_resource_record *rr, void *data, + size_t data_len); + +/* + * Sends a DNS query to the remote end. + */ +void mg_send_dns_query(struct mg_connection *nc, const char *name, + int query_type); + +/* + * Inserts a DNS header to an IO buffer. + * + * Returns the number of bytes inserted. + */ +int mg_dns_insert_header(struct mbuf *io, size_t pos, + struct mg_dns_message *msg); + +/* + * Appends already encoded questions from an existing message. + * + * This is useful when generating a DNS reply message which includes + * all question records. + * + * Returns the number of appended bytes. + */ +int mg_dns_copy_questions(struct mbuf *io, struct mg_dns_message *msg); + +/* + * Encodes and appends a DNS resource record to an IO buffer. + * + * The record metadata is taken from the `rr` parameter, while the name and data + * are taken from the parameters, encoded in the appropriate format depending on + * record type and stored in the IO buffer. The encoded values might contain + * offsets within the IO buffer. It's thus important that the IO buffer doesn't + * get trimmed while a sequence of records are encoded while preparing a DNS + * reply. + * + * This function doesn't update the `name` and `rdata` pointers in the `rr` + * struct because they might be invalidated as soon as the IO buffer grows + * again. + * + * Returns the number of bytes appended or -1 in case of error. + */ +int mg_dns_encode_record(struct mbuf *io, struct mg_dns_resource_record *rr, + const char *name, size_t nlen, const void *rdata, + size_t rlen); + +/* + * Encodes a DNS name. + */ +int mg_dns_encode_name(struct mbuf *io, const char *name, size_t len); + +/* Low-level: parses a DNS response. */ +int mg_parse_dns(const char *buf, int len, struct mg_dns_message *msg); + +/* + * Uncompresses a DNS compressed name. + * + * The containing DNS message is required because of the compressed encoding + * and reference suffixes present elsewhere in the packet. + * + * If the name is less than `dst_len` characters long, the remainder + * of `dst` is terminated with `\0` characters. Otherwise, `dst` is not + * terminated. + * + * If `dst_len` is 0 `dst` can be NULL. + * Returns the uncompressed name length. + */ +size_t mg_dns_uncompress_name(struct mg_dns_message *msg, struct mg_str *name, + char *dst, int dst_len); + +/* + * Attaches a built-in DNS event handler to the given listening connection. + * + * The DNS event handler parses the incoming UDP packets, treating them as DNS + * requests. If an incoming packet gets successfully parsed by the DNS event + * handler, a user event handler will receive an `MG_DNS_REQUEST` event, with + * `ev_data` pointing to the parsed `struct mg_dns_message`. + * + * See + * [captive_dns_server](https://github.com/cesanta/mongoose/tree/master/examples/captive_dns_server) + * example on how to handle DNS request and send DNS reply. + */ +void mg_set_protocol_dns(struct mg_connection *nc); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CS_MONGOOSE_SRC_DNS_H_ */ diff --git a/src/dns_server.c b/src/dns_server.c new file mode 100644 index 00000000..4e20bdfb --- /dev/null +++ b/src/dns_server.c @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_DNS_SERVER + +#include "mongoose/src/internal.h" +#include "mongoose/src/dns-server.h" + +struct mg_dns_reply mg_dns_create_reply(struct mbuf *io, + struct mg_dns_message *msg) { + struct mg_dns_reply rep; + rep.msg = msg; + rep.io = io; + rep.start = io->len; + + /* reply + recursion allowed */ + msg->flags |= 0x8080; + mg_dns_copy_questions(io, msg); + + msg->num_answers = 0; + return rep; +} + +void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r) { + size_t sent = r->io->len - r->start; + mg_dns_insert_header(r->io, r->start, r->msg); + if (!(nc->flags & MG_F_UDP)) { + uint16_t len = htons((uint16_t) sent); + mbuf_insert(r->io, r->start, &len, 2); + } + + if (&nc->send_mbuf != r->io) { + mg_send(nc, r->io->buf + r->start, r->io->len - r->start); + r->io->len = r->start; + } +} + +int mg_dns_reply_record(struct mg_dns_reply *reply, + struct mg_dns_resource_record *question, + const char *name, int rtype, int ttl, const void *rdata, + size_t rdata_len) { + struct mg_dns_message *msg = (struct mg_dns_message *) reply->msg; + char rname[512]; + struct mg_dns_resource_record *ans = &msg->answers[msg->num_answers]; + if (msg->num_answers >= MG_MAX_DNS_ANSWERS) { + return -1; /* LCOV_EXCL_LINE */ + } + + if (name == NULL) { + name = rname; + rname[511] = 0; + mg_dns_uncompress_name(msg, &question->name, rname, sizeof(rname) - 1); + } + + *ans = *question; + ans->kind = MG_DNS_ANSWER; + ans->rtype = rtype; + ans->ttl = ttl; + + if (mg_dns_encode_record(reply->io, ans, name, strlen(name), rdata, + rdata_len) == -1) { + return -1; /* LCOV_EXCL_LINE */ + }; + + msg->num_answers++; + return 0; +} + +#endif /* MG_ENABLE_DNS_SERVER */ diff --git a/src/dns_server.h b/src/dns_server.h new file mode 100644 index 00000000..cc2d18d8 --- /dev/null +++ b/src/dns_server.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === DNS server API reference + * + * Disabled by default; enable with `-DMG_ENABLE_DNS_SERVER`. + */ + +#ifndef CS_MONGOOSE_SRC_DNS_SERVER_H_ +#define CS_MONGOOSE_SRC_DNS_SERVER_H_ + +#if MG_ENABLE_DNS_SERVER + +#include "mongoose/src/dns.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MG_DNS_SERVER_DEFAULT_TTL 3600 + +struct mg_dns_reply { + struct mg_dns_message *msg; + struct mbuf *io; + size_t start; +}; + +/* + * Creates a DNS reply. + * + * The reply will be based on an existing query message `msg`. + * The query body will be appended to the output buffer. + * "reply + recursion allowed" will be added to the message flags and the + * message's num_answers will be set to 0. + * + * Answer records can be appended with `mg_dns_send_reply` or by lower + * level function defined in the DNS API. + * + * In order to send a reply use `mg_dns_send_reply`. + * It's possible to use a connection's send buffer as reply buffer, + * and it will work for both UDP and TCP connections. + * + * Example: + * + * ```c + * reply = mg_dns_create_reply(&nc->send_mbuf, msg); + * for (i = 0; i < msg->num_questions; i++) { + * rr = &msg->questions[i]; + * if (rr->rtype == MG_DNS_A_RECORD) { + * mg_dns_reply_record(&reply, rr, 3600, &dummy_ip_addr, 4); + * } + * } + * mg_dns_send_reply(nc, &reply); + * ``` + */ +struct mg_dns_reply mg_dns_create_reply(struct mbuf *io, + struct mg_dns_message *msg); + +/* + * Appends a DNS reply record to the IO buffer and to the DNS message. + * + * The message's num_answers field will be incremented. It's the caller's duty + * to ensure num_answers is properly initialised. + * + * Returns -1 on error. + */ +int mg_dns_reply_record(struct mg_dns_reply *reply, + struct mg_dns_resource_record *question, + const char *name, int rtype, int ttl, const void *rdata, + size_t rdata_len); + +/* + * Sends a DNS reply through a connection. + * + * The DNS data is stored in an IO buffer pointed by reply structure in `r`. + * This function mutates the content of that buffer in order to ensure that + * the DNS header reflects the size and flags of the message, that might have + * been updated either with `mg_dns_reply_record` or by direct manipulation of + * `r->message`. + * + * Once sent, the IO buffer will be trimmed unless the reply IO buffer + * is the connection's send buffer and the connection is not in UDP mode. + */ +void mg_dns_send_reply(struct mg_connection *nc, struct mg_dns_reply *r); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_DNS_SERVER */ +#endif /* CS_MONGOOSE_SRC_DNS_SERVER_H_ */ diff --git a/src/features.h b/src/features.h new file mode 100644 index 00000000..a0bd14de --- /dev/null +++ b/src/features.h @@ -0,0 +1,180 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_FEATURES_H_ +#define CS_MONGOOSE_SRC_FEATURES_H_ + +#ifndef MG_DISABLE_HTTP_DIGEST_AUTH +#define MG_DISABLE_HTTP_DIGEST_AUTH 0 +#endif + +#ifndef MG_DISABLE_HTTP_KEEP_ALIVE +#define MG_DISABLE_HTTP_KEEP_ALIVE 0 +#endif + +#ifndef MG_DISABLE_PFS +#define MG_DISABLE_PFS 0 +#endif + +#ifndef MG_DISABLE_WS_RANDOM_MASK +#define MG_DISABLE_WS_RANDOM_MASK 0 +#endif + +#ifndef MG_ENABLE_ASYNC_RESOLVER +#define MG_ENABLE_ASYNC_RESOLVER 1 +#endif + +#ifndef MG_ENABLE_BROADCAST +#define MG_ENABLE_BROADCAST 0 +#endif + +#ifndef MG_ENABLE_COAP +#define MG_ENABLE_COAP 0 +#endif + +#ifndef MG_ENABLE_DEBUG +#define MG_ENABLE_DEBUG 0 +#endif + +#ifndef MG_ENABLE_DIRECTORY_LISTING +#define MG_ENABLE_DIRECTORY_LISTING 0 +#endif + +#ifndef MG_ENABLE_DNS +#define MG_ENABLE_DNS 1 +#endif + +#ifndef MG_ENABLE_DNS_SERVER +#define MG_ENABLE_DNS_SERVER 0 +#endif + +#ifndef MG_ENABLE_FAKE_DAVLOCK +#define MG_ENABLE_FAKE_DAVLOCK 0 +#endif + +#ifndef MG_ENABLE_FILESYSTEM +#define MG_ENABLE_FILESYSTEM 0 +#endif + +#ifndef MG_ENABLE_GETADDRINFO +#define MG_ENABLE_GETADDRINFO 0 +#endif + +#ifndef MG_ENABLE_HEXDUMP +#define MG_ENABLE_HEXDUMP CS_ENABLE_STDIO +#endif + +#ifndef MG_ENABLE_HTTP +#define MG_ENABLE_HTTP 1 +#endif + +#ifndef MG_ENABLE_HTTP_CGI +#define MG_ENABLE_HTTP_CGI 0 +#endif + +#ifndef MG_ENABLE_HTTP_SSI +#define MG_ENABLE_HTTP_SSI MG_ENABLE_FILESYSTEM +#endif + +#ifndef MG_ENABLE_HTTP_SSI_EXEC +#define MG_ENABLE_HTTP_SSI_EXEC 0 +#endif + +#ifndef MG_ENABLE_HTTP_STREAMING_MULTIPART +#define MG_ENABLE_HTTP_STREAMING_MULTIPART 0 +#endif + +#ifndef MG_ENABLE_HTTP_WEBDAV +#define MG_ENABLE_HTTP_WEBDAV 0 +#endif + +#ifndef MG_ENABLE_HTTP_WEBSOCKET +#define MG_ENABLE_HTTP_WEBSOCKET MG_ENABLE_HTTP +#endif + +#ifndef MG_ENABLE_IPV6 +#define MG_ENABLE_IPV6 0 +#endif + +#ifndef MG_ENABLE_MQTT +#define MG_ENABLE_MQTT 1 +#endif + +#ifndef MG_ENABLE_SOCKS +#define MG_ENABLE_SOCKS 0 +#endif + +#ifndef MG_ENABLE_MQTT_BROKER +#define MG_ENABLE_MQTT_BROKER 0 +#endif + +#ifndef MG_ENABLE_SSL +#define MG_ENABLE_SSL 0 +#endif + +#ifndef MG_ENABLE_SYNC_RESOLVER +#define MG_ENABLE_SYNC_RESOLVER 0 +#endif + +#ifndef MG_ENABLE_STDIO +#define MG_ENABLE_STDIO CS_ENABLE_STDIO +#endif + +#ifndef MG_NET_IF +#define MG_NET_IF MG_NET_IF_SOCKET +#endif + +#ifndef MG_SSL_IF +#define MG_SSL_IF MG_SSL_IF_OPENSSL +#endif + +#ifndef MG_ENABLE_THREADS /* ifdef-ok */ +#ifdef _WIN32 +#define MG_ENABLE_THREADS 1 +#else +#define MG_ENABLE_THREADS 0 +#endif +#endif + +#if MG_ENABLE_DEBUG && !defined(CS_ENABLE_DEBUG) +#define CS_ENABLE_DEBUG 1 +#endif + +/* MQTT broker requires MQTT */ +#if MG_ENABLE_MQTT_BROKER && !MG_ENABLE_MQTT +#undef MG_ENABLE_MQTT +#define MG_ENABLE_MQTT 1 +#endif + +#ifndef MG_ENABLE_HTTP_URL_REWRITES +#define MG_ENABLE_HTTP_URL_REWRITES \ + (CS_PLATFORM == CS_P_WINDOWS || CS_PLATFORM == CS_P_UNIX) +#endif + +#ifndef MG_ENABLE_TUN +#define MG_ENABLE_TUN MG_ENABLE_HTTP_WEBSOCKET +#endif + +#ifndef MG_ENABLE_SNTP +#define MG_ENABLE_SNTP 0 +#endif + +#ifndef MG_ENABLE_EXTRA_ERRORS_DESC +#define MG_ENABLE_EXTRA_ERRORS_DESC 0 +#endif + +#ifndef MG_ENABLE_CALLBACK_USERDATA +#define MG_ENABLE_CALLBACK_USERDATA 0 +#endif + +#if MG_ENABLE_CALLBACK_USERDATA +#define MG_UD_ARG(ud) , ud +#define MG_CB(cb, ud) cb, ud +#else +#define MG_UD_ARG(ud) +#define MG_CB(cb, ud) cb +#endif + +#endif /* CS_MONGOOSE_SRC_FEATURES_H_ */ diff --git a/src/http.c b/src/http.c new file mode 100644 index 00000000..26467d6d --- /dev/null +++ b/src/http.c @@ -0,0 +1,2911 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_HTTP + +#include "common/md5.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/util.h" + +static const char *mg_version_header = "Mongoose/" MG_VERSION; + +enum mg_http_proto_data_type { DATA_NONE, DATA_FILE, DATA_PUT }; + +struct mg_http_proto_data_file { + FILE *fp; /* Opened file. */ + int64_t cl; /* Content-Length. How many bytes to send. */ + int64_t sent; /* How many bytes have been already sent. */ + int keepalive; /* Keep connection open after sending. */ + enum mg_http_proto_data_type type; +}; + +#if MG_ENABLE_HTTP_CGI +struct mg_http_proto_data_cgi { + struct mg_connection *cgi_nc; +}; +#endif + +struct mg_http_proto_data_chuncked { + int64_t body_len; /* How many bytes of chunked body was reassembled. */ +}; + +struct mg_http_endpoint { + struct mg_http_endpoint *next; + struct mg_str uri_pattern; /* owned */ + char *auth_domain; /* owned */ + char *auth_file; /* owned */ + + mg_event_handler_t handler; +#if MG_ENABLE_CALLBACK_USERDATA + void *user_data; +#endif +}; + +enum mg_http_multipart_stream_state { + MPS_BEGIN, + MPS_WAITING_FOR_BOUNDARY, + MPS_WAITING_FOR_CHUNK, + MPS_GOT_CHUNK, + MPS_GOT_BOUNDARY, + MPS_FINALIZE, + MPS_FINISHED +}; + +struct mg_http_multipart_stream { + const char *boundary; + int boundary_len; + const char *var_name; + const char *file_name; + void *user_data; + int prev_io_len; + enum mg_http_multipart_stream_state state; + int processing_part; +}; + +struct mg_reverse_proxy_data { + struct mg_connection *linked_conn; +}; + +struct mg_ws_proto_data { + /* + * Defragmented size of the frame so far. + * + * First byte of nc->recv_mbuf.buf is an op, the rest of the data is + * defragmented data. + */ + size_t reass_len; +}; + +struct mg_http_proto_data { +#if MG_ENABLE_FILESYSTEM + struct mg_http_proto_data_file file; +#endif +#if MG_ENABLE_HTTP_CGI + struct mg_http_proto_data_cgi cgi; +#endif +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + struct mg_http_multipart_stream mp_stream; +#endif +#if MG_ENABLE_HTTP_WEBSOCKET + struct mg_ws_proto_data ws_data; +#endif + struct mg_http_proto_data_chuncked chunk; + struct mg_http_endpoint *endpoints; + mg_event_handler_t endpoint_handler; + struct mg_reverse_proxy_data reverse_proxy_data; + size_t rcvd; /* How many bytes we have received. */ +}; + +static void mg_http_conn_destructor(void *proto_data); +struct mg_connection *mg_connect_http_base( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *scheme1, const char *scheme2, + const char *scheme_ssl1, const char *scheme_ssl2, const char *url, + struct mg_str *path, struct mg_str *user_info, struct mg_str *host); + +static struct mg_http_proto_data *mg_http_get_proto_data( + struct mg_connection *c) { + if (c->proto_data == NULL) { + c->proto_data = MG_CALLOC(1, sizeof(struct mg_http_proto_data)); + c->proto_data_destructor = mg_http_conn_destructor; + } + + return (struct mg_http_proto_data *) c->proto_data; +} + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +static void mg_http_free_proto_data_mp_stream( + struct mg_http_multipart_stream *mp) { + MG_FREE((void *) mp->boundary); + MG_FREE((void *) mp->var_name); + MG_FREE((void *) mp->file_name); + memset(mp, 0, sizeof(*mp)); +} +#endif + +#if MG_ENABLE_FILESYSTEM +static void mg_http_free_proto_data_file(struct mg_http_proto_data_file *d) { + if (d != NULL) { + if (d->fp != NULL) { + fclose(d->fp); + } + memset(d, 0, sizeof(struct mg_http_proto_data_file)); + } +} +#endif + +static void mg_http_free_proto_data_endpoints(struct mg_http_endpoint **ep) { + struct mg_http_endpoint *current = *ep; + + while (current != NULL) { + struct mg_http_endpoint *tmp = current->next; + MG_FREE((void *) current->uri_pattern.p); + MG_FREE((void *) current->auth_domain); + MG_FREE((void *) current->auth_file); + MG_FREE(current); + current = tmp; + } + + ep = NULL; +} + +static void mg_http_free_reverse_proxy_data(struct mg_reverse_proxy_data *rpd) { + if (rpd->linked_conn != NULL) { + /* + * Connection has linked one, we have to unlink & close it + * since _this_ connection is going to die and + * it doesn't make sense to keep another one + */ + struct mg_http_proto_data *pd = mg_http_get_proto_data(rpd->linked_conn); + if (pd->reverse_proxy_data.linked_conn != NULL) { + pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE; + pd->reverse_proxy_data.linked_conn = NULL; + } + rpd->linked_conn = NULL; + } +} + +static void mg_http_conn_destructor(void *proto_data) { + struct mg_http_proto_data *pd = (struct mg_http_proto_data *) proto_data; +#if MG_ENABLE_FILESYSTEM + mg_http_free_proto_data_file(&pd->file); +#endif +#if MG_ENABLE_HTTP_CGI + mg_http_free_proto_data_cgi(&pd->cgi); +#endif +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + mg_http_free_proto_data_mp_stream(&pd->mp_stream); +#endif + mg_http_free_proto_data_endpoints(&pd->endpoints); + mg_http_free_reverse_proxy_data(&pd->reverse_proxy_data); + MG_FREE(proto_data); +} + +#if MG_ENABLE_FILESYSTEM + +#define MIME_ENTRY(_ext, _type) \ + { _ext, sizeof(_ext) - 1, _type } +static const struct { + const char *extension; + size_t ext_len; + const char *mime_type; +} mg_static_builtin_mime_types[] = { + MIME_ENTRY("html", "text/html"), + MIME_ENTRY("html", "text/html"), + MIME_ENTRY("htm", "text/html"), + MIME_ENTRY("shtm", "text/html"), + MIME_ENTRY("shtml", "text/html"), + MIME_ENTRY("css", "text/css"), + MIME_ENTRY("js", "application/x-javascript"), + MIME_ENTRY("ico", "image/x-icon"), + MIME_ENTRY("gif", "image/gif"), + MIME_ENTRY("jpg", "image/jpeg"), + MIME_ENTRY("jpeg", "image/jpeg"), + MIME_ENTRY("png", "image/png"), + MIME_ENTRY("svg", "image/svg+xml"), + MIME_ENTRY("txt", "text/plain"), + MIME_ENTRY("torrent", "application/x-bittorrent"), + MIME_ENTRY("wav", "audio/x-wav"), + MIME_ENTRY("mp3", "audio/x-mp3"), + MIME_ENTRY("mid", "audio/mid"), + MIME_ENTRY("m3u", "audio/x-mpegurl"), + MIME_ENTRY("ogg", "application/ogg"), + MIME_ENTRY("ram", "audio/x-pn-realaudio"), + MIME_ENTRY("xml", "text/xml"), + MIME_ENTRY("ttf", "application/x-font-ttf"), + MIME_ENTRY("json", "application/json"), + MIME_ENTRY("xslt", "application/xml"), + MIME_ENTRY("xsl", "application/xml"), + MIME_ENTRY("ra", "audio/x-pn-realaudio"), + MIME_ENTRY("doc", "application/msword"), + MIME_ENTRY("exe", "application/octet-stream"), + MIME_ENTRY("zip", "application/x-zip-compressed"), + MIME_ENTRY("xls", "application/excel"), + MIME_ENTRY("tgz", "application/x-tar-gz"), + MIME_ENTRY("tar", "application/x-tar"), + MIME_ENTRY("gz", "application/x-gunzip"), + MIME_ENTRY("arj", "application/x-arj-compressed"), + MIME_ENTRY("rar", "application/x-rar-compressed"), + MIME_ENTRY("rtf", "application/rtf"), + MIME_ENTRY("pdf", "application/pdf"), + MIME_ENTRY("swf", "application/x-shockwave-flash"), + MIME_ENTRY("mpg", "video/mpeg"), + MIME_ENTRY("webm", "video/webm"), + MIME_ENTRY("mpeg", "video/mpeg"), + MIME_ENTRY("mov", "video/quicktime"), + MIME_ENTRY("mp4", "video/mp4"), + MIME_ENTRY("m4v", "video/x-m4v"), + MIME_ENTRY("asf", "video/x-ms-asf"), + MIME_ENTRY("avi", "video/x-msvideo"), + MIME_ENTRY("bmp", "image/bmp"), + {NULL, 0, NULL}}; + +static struct mg_str mg_get_mime_type(const char *path, const char *dflt, + const struct mg_serve_http_opts *opts) { + const char *ext, *overrides; + size_t i, path_len; + struct mg_str r, k, v; + + path_len = strlen(path); + + overrides = opts->custom_mime_types; + while ((overrides = mg_next_comma_list_entry(overrides, &k, &v)) != NULL) { + ext = path + (path_len - k.len); + if (path_len > k.len && mg_vcasecmp(&k, ext) == 0) { + return v; + } + } + + for (i = 0; mg_static_builtin_mime_types[i].extension != NULL; i++) { + ext = path + (path_len - mg_static_builtin_mime_types[i].ext_len); + if (path_len > mg_static_builtin_mime_types[i].ext_len && ext[-1] == '.' && + mg_casecmp(ext, mg_static_builtin_mime_types[i].extension) == 0) { + r.p = mg_static_builtin_mime_types[i].mime_type; + r.len = strlen(r.p); + return r; + } + } + + r.p = dflt; + r.len = strlen(r.p); + return r; +} +#endif + +/* + * Check whether full request is buffered. Return: + * -1 if request is malformed + * 0 if request is not yet fully buffered + * >0 actual request length, including last \r\n\r\n + */ +static int mg_http_get_request_len(const char *s, int buf_len) { + const unsigned char *buf = (unsigned char *) s; + int i; + + for (i = 0; i < buf_len; i++) { + if (!isprint(buf[i]) && buf[i] != '\r' && buf[i] != '\n' && buf[i] < 128) { + return -1; + } else if (buf[i] == '\n' && i + 1 < buf_len && buf[i + 1] == '\n') { + return i + 2; + } else if (buf[i] == '\n' && i + 2 < buf_len && buf[i + 1] == '\r' && + buf[i + 2] == '\n') { + return i + 3; + } + } + + return 0; +} + +static const char *mg_http_parse_headers(const char *s, const char *end, + int len, struct http_message *req) { + int i = 0; + while (i < (int) ARRAY_SIZE(req->header_names) - 1) { + struct mg_str *k = &req->header_names[i], *v = &req->header_values[i]; + + s = mg_skip(s, end, ": ", k); + s = mg_skip(s, end, "\r\n", v); + + while (v->len > 0 && v->p[v->len - 1] == ' ') { + v->len--; /* Trim trailing spaces in header value */ + } + + /* + * If header value is empty - skip it and go to next (if any). + * NOTE: Do not add it to headers_values because such addition changes API + * behaviour + */ + if (k->len != 0 && v->len == 0) { + continue; + } + + if (k->len == 0 || v->len == 0) { + k->p = v->p = NULL; + k->len = v->len = 0; + break; + } + + if (!mg_ncasecmp(k->p, "Content-Length", 14)) { + req->body.len = (size_t) to64(v->p); + req->message.len = len + req->body.len; + } + + i++; + } + + return s; +} + +int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req) { + const char *end, *qs; + int len = mg_http_get_request_len(s, n); + + if (len <= 0) return len; + + memset(hm, 0, sizeof(*hm)); + hm->message.p = s; + hm->body.p = s + len; + hm->message.len = hm->body.len = (size_t) ~0; + end = s + len; + + /* Request is fully buffered. Skip leading whitespaces. */ + while (s < end && isspace(*(unsigned char *) s)) s++; + + if (is_req) { + /* Parse request line: method, URI, proto */ + s = mg_skip(s, end, " ", &hm->method); + s = mg_skip(s, end, " ", &hm->uri); + s = mg_skip(s, end, "\r\n", &hm->proto); + if (hm->uri.p <= hm->method.p || hm->proto.p <= hm->uri.p) return -1; + + /* If URI contains '?' character, initialize query_string */ + if ((qs = (char *) memchr(hm->uri.p, '?', hm->uri.len)) != NULL) { + hm->query_string.p = qs + 1; + hm->query_string.len = &hm->uri.p[hm->uri.len] - (qs + 1); + hm->uri.len = qs - hm->uri.p; + } + } else { + s = mg_skip(s, end, " ", &hm->proto); + if (end - s < 4 || s[3] != ' ') return -1; + hm->resp_code = atoi(s); + if (hm->resp_code < 100 || hm->resp_code >= 600) return -1; + s += 4; + s = mg_skip(s, end, "\r\n", &hm->resp_status_msg); + } + + s = mg_http_parse_headers(s, end, len, hm); + + /* + * mg_parse_http() is used to parse both HTTP requests and HTTP + * responses. If HTTP response does not have Content-Length set, then + * body is read until socket is closed, i.e. body.len is infinite (~0). + * + * For HTTP requests though, according to + * http://tools.ietf.org/html/rfc7231#section-8.1.3, + * only POST and PUT methods have defined body semantics. + * Therefore, if Content-Length is not specified and methods are + * not one of PUT or POST, set body length to 0. + * + * So, + * if it is HTTP request, and Content-Length is not set, + * and method is not (PUT or POST) then reset body length to zero. + */ + if (hm->body.len == (size_t) ~0 && is_req && + mg_vcasecmp(&hm->method, "PUT") != 0 && + mg_vcasecmp(&hm->method, "POST") != 0) { + hm->body.len = 0; + hm->message.len = len; + } + + return len; +} + +struct mg_str *mg_get_http_header(struct http_message *hm, const char *name) { + size_t i, len = strlen(name); + + for (i = 0; hm->header_names[i].len > 0; i++) { + struct mg_str *h = &hm->header_names[i], *v = &hm->header_values[i]; + if (h->p != NULL && h->len == len && !mg_ncasecmp(h->p, name, len)) + return v; + } + + return NULL; +} + +#if MG_ENABLE_FILESYSTEM +static void mg_http_transfer_file_data(struct mg_connection *nc) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + char buf[MG_MAX_HTTP_SEND_MBUF]; + size_t n = 0, to_read = 0, left = (size_t)(pd->file.cl - pd->file.sent); + + if (pd->file.type == DATA_FILE) { + struct mbuf *io = &nc->send_mbuf; + if (io->len >= MG_MAX_HTTP_SEND_MBUF) { + to_read = 0; + } else { + to_read = MG_MAX_HTTP_SEND_MBUF - io->len; + } + if (to_read > left) { + to_read = left; + } + if (to_read > 0) { + n = mg_fread(buf, 1, to_read, pd->file.fp); + if (n > 0) { + mg_send(nc, buf, n); + pd->file.sent += n; + DBG(("%p sent %d (total %d)", nc, (int) n, (int) pd->file.sent)); + } + } else { + /* Rate-limited */ + } + if (pd->file.sent >= pd->file.cl) { + LOG(LL_DEBUG, ("%p done, %d bytes", nc, (int) pd->file.sent)); + if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE; + mg_http_free_proto_data_file(&pd->file); + } + } else if (pd->file.type == DATA_PUT) { + struct mbuf *io = &nc->recv_mbuf; + size_t to_write = left <= 0 ? 0 : left < io->len ? (size_t) left : io->len; + size_t n = mg_fwrite(io->buf, 1, to_write, pd->file.fp); + if (n > 0) { + mbuf_remove(io, n); + pd->file.sent += n; + } + if (n == 0 || pd->file.sent >= pd->file.cl) { + if (!pd->file.keepalive) nc->flags |= MG_F_SEND_AND_CLOSE; + mg_http_free_proto_data_file(&pd->file); + } + } +#if MG_ENABLE_HTTP_CGI + else if (pd->cgi.cgi_nc != NULL) { + /* This is POST data that needs to be forwarded to the CGI process */ + if (pd->cgi.cgi_nc != NULL) { + mg_forward(nc, pd->cgi.cgi_nc); + } else { + nc->flags |= MG_F_SEND_AND_CLOSE; + } + } +#endif +} +#endif /* MG_ENABLE_FILESYSTEM */ + +/* + * Parse chunked-encoded buffer. Return 0 if the buffer is not encoded, or + * if it's incomplete. If the chunk is fully buffered, return total number of + * bytes in a chunk, and store data in `data`, `data_len`. + */ +static size_t mg_http_parse_chunk(char *buf, size_t len, char **chunk_data, + size_t *chunk_len) { + unsigned char *s = (unsigned char *) buf; + size_t n = 0; /* scanned chunk length */ + size_t i = 0; /* index in s */ + + /* Scan chunk length. That should be a hexadecimal number. */ + while (i < len && isxdigit(s[i])) { + n *= 16; + n += (s[i] >= '0' && s[i] <= '9') ? s[i] - '0' : tolower(s[i]) - 'a' + 10; + i++; + } + + /* Skip new line */ + if (i == 0 || i + 2 > len || s[i] != '\r' || s[i + 1] != '\n') { + return 0; + } + i += 2; + + /* Record where the data is */ + *chunk_data = (char *) s + i; + *chunk_len = n; + + /* Skip data */ + i += n; + + /* Skip new line */ + if (i == 0 || i + 2 > len || s[i] != '\r' || s[i + 1] != '\n') { + return 0; + } + return i + 2; +} + +MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc, + struct http_message *hm, char *buf, + size_t blen) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + char *data; + size_t i, n, data_len, body_len, zero_chunk_received = 0; + /* Find out piece of received data that is not yet reassembled */ + body_len = (size_t) pd->chunk.body_len; + assert(blen >= body_len); + + /* Traverse all fully buffered chunks */ + for (i = body_len; + (n = mg_http_parse_chunk(buf + i, blen - i, &data, &data_len)) > 0; + i += n) { + /* Collapse chunk data to the rest of HTTP body */ + memmove(buf + body_len, data, data_len); + body_len += data_len; + hm->body.len = body_len; + + if (data_len == 0) { + zero_chunk_received = 1; + i += n; + break; + } + } + + if (i > body_len) { + /* Shift unparsed content to the parsed body */ + assert(i <= blen); + memmove(buf + body_len, buf + i, blen - i); + memset(buf + body_len + blen - i, 0, i - body_len); + nc->recv_mbuf.len -= i - body_len; + pd->chunk.body_len = body_len; + + /* Send MG_EV_HTTP_CHUNK event */ + nc->flags &= ~MG_F_DELETE_CHUNK; + mg_call(nc, nc->handler, nc->user_data, MG_EV_HTTP_CHUNK, hm); + + /* Delete processed data if user set MG_F_DELETE_CHUNK flag */ + if (nc->flags & MG_F_DELETE_CHUNK) { + memset(buf, 0, body_len); + memmove(buf, buf + body_len, blen - i); + nc->recv_mbuf.len -= body_len; + hm->body.len = 0; + pd->chunk.body_len = 0; + } + + if (zero_chunk_received) { + /* Total message size is len(body) + len(headers) */ + hm->message.len = + (size_t) pd->chunk.body_len + blen - i + (hm->body.p - hm->message.p); + } + } + + return body_len; +} + +struct mg_http_endpoint *mg_http_get_endpoint_handler(struct mg_connection *nc, + struct mg_str *uri_path) { + struct mg_http_proto_data *pd; + struct mg_http_endpoint *ret = NULL; + int matched, matched_max = 0; + struct mg_http_endpoint *ep; + + if (nc == NULL) { + return NULL; + } + + pd = mg_http_get_proto_data(nc); + + ep = pd->endpoints; + while (ep != NULL) { + if ((matched = mg_match_prefix_n(ep->uri_pattern, *uri_path)) > 0) { + if (matched > matched_max) { + /* Looking for the longest suitable handler */ + ret = ep; + matched_max = matched; + } + } + + ep = ep->next; + } + + return ret; +} + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +static void mg_http_multipart_continue(struct mg_connection *nc); + +static void mg_http_multipart_begin(struct mg_connection *nc, + struct http_message *hm, int req_len); + +#endif + +static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev, + struct http_message *hm); + +static void deliver_chunk(struct mg_connection *c, struct http_message *hm, + int req_len) { + /* Incomplete message received. Send MG_EV_HTTP_CHUNK event */ + hm->body.len = c->recv_mbuf.len - req_len; + c->flags &= ~MG_F_DELETE_CHUNK; + mg_call(c, c->handler, c->user_data, MG_EV_HTTP_CHUNK, hm); + /* Delete processed data if user set MG_F_DELETE_CHUNK flag */ + if (c->flags & MG_F_DELETE_CHUNK) c->recv_mbuf.len = req_len; +} + +/* + * lx106 compiler has a bug (TODO(mkm) report and insert tracking bug here) + * If a big structure is declared in a big function, lx106 gcc will make it + * even bigger (round up to 4k, from 700 bytes of actual size). + */ +#ifdef __xtensa__ +static void mg_http_handler2(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data), + struct http_message *hm) __attribute__((noinline)); + +void mg_http_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct http_message hm; + mg_http_handler2(nc, ev, ev_data MG_UD_ARG(user_data), &hm); +} + +static void mg_http_handler2(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data), + struct http_message *hm) { +#else /* !__XTENSA__ */ +void mg_http_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct http_message shm, *hm = &shm; +#endif /* __XTENSA__ */ + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + struct mbuf *io = &nc->recv_mbuf; + int req_len; + const int is_req = (nc->listener != NULL); +#if MG_ENABLE_HTTP_WEBSOCKET + struct mg_str *vec; +#endif + if (ev == MG_EV_CLOSE) { +#if MG_ENABLE_HTTP_CGI + /* Close associated CGI forwarder connection */ + if (pd->cgi.cgi_nc != NULL) { + pd->cgi.cgi_nc->user_data = NULL; + pd->cgi.cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +#endif +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + if (pd->mp_stream.boundary != NULL) { + /* + * Multipart message is in progress, but connection is closed. + * Finish part and request with an error flag. + */ + struct mg_http_multipart_part mp; + memset(&mp, 0, sizeof(mp)); + mp.status = -1; + mp.var_name = pd->mp_stream.var_name; + mp.file_name = pd->mp_stream.file_name; + mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler), + nc->user_data, MG_EV_HTTP_PART_END, &mp); + mp.var_name = NULL; + mp.file_name = NULL; + mg_call(nc, (pd->endpoint_handler ? pd->endpoint_handler : nc->handler), + nc->user_data, MG_EV_HTTP_MULTIPART_REQUEST_END, &mp); + } else +#endif + if (io->len > 0 && + (req_len = mg_parse_http(io->buf, io->len, hm, is_req)) > 0) { + /* + * For HTTP messages without Content-Length, always send HTTP message + * before MG_EV_CLOSE message. + */ + int ev2 = is_req ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY; + hm->message.len = io->len; + hm->body.len = io->buf + io->len - hm->body.p; + deliver_chunk(nc, hm, req_len); + mg_http_call_endpoint_handler(nc, ev2, hm); + } + pd->rcvd = 0; + } + +#if MG_ENABLE_FILESYSTEM + if (pd->file.fp != NULL) { + mg_http_transfer_file_data(nc); + } +#endif + + mg_call(nc, nc->handler, nc->user_data, ev, ev_data); + + if (ev == MG_EV_RECV) { + struct mg_str *s; + pd->rcvd += *(int *) ev_data; + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + if (pd->mp_stream.boundary != NULL) { + mg_http_multipart_continue(nc); + return; + } +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ + + req_len = mg_parse_http(io->buf, io->len, hm, is_req); + + if (req_len > 0 && + (s = mg_get_http_header(hm, "Transfer-Encoding")) != NULL && + mg_vcasecmp(s, "chunked") == 0) { + mg_handle_chunked(nc, hm, io->buf + req_len, io->len - req_len); + } + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + if (req_len > 0 && (s = mg_get_http_header(hm, "Content-Type")) != NULL && + s->len >= 9 && strncmp(s->p, "multipart", 9) == 0) { + mg_http_multipart_begin(nc, hm, req_len); + mg_http_multipart_continue(nc); + return; + } +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ + + /* TODO(alashkin): refactor this ifelseifelseifelseifelse */ + if ((req_len < 0 || + (req_len == 0 && io->len >= MG_MAX_HTTP_REQUEST_SIZE))) { + DBG(("invalid request")); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else if (req_len == 0) { + /* Do nothing, request is not yet fully buffered */ + } +#if MG_ENABLE_HTTP_WEBSOCKET + else if (nc->listener == NULL && + mg_get_http_header(hm, "Sec-WebSocket-Accept")) { + /* We're websocket client, got handshake response from server. */ + /* TODO(lsm): check the validity of accept Sec-WebSocket-Accept */ + mbuf_remove(io, req_len); + nc->proto_handler = mg_ws_handler; + nc->flags |= MG_F_IS_WEBSOCKET; + mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE, + NULL); + mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data)); + } else if (nc->listener != NULL && + (vec = mg_get_http_header(hm, "Sec-WebSocket-Key")) != NULL) { + struct mg_http_endpoint *ep; + + /* This is a websocket request. Switch protocol handlers. */ + mbuf_remove(io, req_len); + nc->proto_handler = mg_ws_handler; + nc->flags |= MG_F_IS_WEBSOCKET; + + /* + * If we have a handler set up with mg_register_http_endpoint(), + * deliver subsequent websocket events to this handler after the + * protocol switch. + */ + ep = mg_http_get_endpoint_handler(nc->listener, &hm->uri); + if (ep != NULL) { + nc->handler = ep->handler; +#if MG_ENABLE_CALLBACK_USERDATA + nc->user_data = ep->user_data; +#endif + } + + /* Send handshake */ + mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_REQUEST, + hm); + if (!(nc->flags & (MG_F_CLOSE_IMMEDIATELY | MG_F_SEND_AND_CLOSE))) { + if (nc->send_mbuf.len == 0) { + mg_ws_handshake(nc, vec, hm); + } + mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_HANDSHAKE_DONE, + NULL); + mg_ws_handler(nc, MG_EV_RECV, ev_data MG_UD_ARG(user_data)); + } + } +#endif /* MG_ENABLE_HTTP_WEBSOCKET */ + else if (hm->message.len > pd->rcvd) { + /* Not yet received all HTTP body, deliver MG_EV_HTTP_CHUNK */ + deliver_chunk(nc, hm, req_len); + if (nc->recv_mbuf_limit > 0 && nc->recv_mbuf.len >= nc->recv_mbuf_limit) { + LOG(LL_ERROR, ("%p recv buffer (%lu bytes) exceeds the limit " + "%lu bytes, and not drained, closing", + nc, (unsigned long) nc->recv_mbuf.len, + (unsigned long) nc->recv_mbuf_limit)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + } else { + /* We did receive all HTTP body. */ + int trigger_ev = nc->listener ? MG_EV_HTTP_REQUEST : MG_EV_HTTP_REPLY; + char addr[32]; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + DBG(("%p %s %.*s %.*s", nc, addr, (int) hm->method.len, hm->method.p, + (int) hm->uri.len, hm->uri.p)); + deliver_chunk(nc, hm, req_len); + /* Whole HTTP message is fully buffered, call event handler */ + mg_http_call_endpoint_handler(nc, trigger_ev, hm); + mbuf_remove(io, hm->message.len); + pd->rcvd = 0; + } + } +} + +static size_t mg_get_line_len(const char *buf, size_t buf_len) { + size_t len = 0; + while (len < buf_len && buf[len] != '\n') len++; + return len == buf_len ? 0 : len + 1; +} + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +static void mg_http_multipart_begin(struct mg_connection *nc, + struct http_message *hm, int req_len) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + struct mg_str *ct; + struct mbuf *io = &nc->recv_mbuf; + + char boundary[100]; + int boundary_len; + + ct = mg_get_http_header(hm, "Content-Type"); + if (ct == NULL) { + /* We need more data - or it isn't multipart mesage */ + goto exit_mp; + } + + /* Content-type should start with "multipart" */ + if (ct->len < 9 || strncmp(ct->p, "multipart", 9) != 0) { + goto exit_mp; + } + + boundary_len = + mg_http_parse_header(ct, "boundary", boundary, sizeof(boundary)); + if (boundary_len == 0) { + /* + * Content type is multipart, but there is no boundary, + * probably malformed request + */ + nc->flags = MG_F_CLOSE_IMMEDIATELY; + DBG(("invalid request")); + goto exit_mp; + } + + /* If we reach this place - that is multipart request */ + + if (pd->mp_stream.boundary != NULL) { + /* + * Another streaming request was in progress, + * looks like protocol error + */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else { + struct mg_http_endpoint *ep = NULL; + pd->mp_stream.state = MPS_BEGIN; + pd->mp_stream.boundary = strdup(boundary); + pd->mp_stream.boundary_len = strlen(boundary); + pd->mp_stream.var_name = pd->mp_stream.file_name = NULL; + pd->endpoint_handler = nc->handler; + + ep = mg_http_get_endpoint_handler(nc->listener, &hm->uri); + if (ep != NULL) { + pd->endpoint_handler = ep->handler; + } + + mg_http_call_endpoint_handler(nc, MG_EV_HTTP_MULTIPART_REQUEST, hm); + + mbuf_remove(io, req_len); + } +exit_mp: + ; +} + +#define CONTENT_DISPOSITION "Content-Disposition: " + +static void mg_http_multipart_call_handler(struct mg_connection *c, int ev, + const char *data, size_t data_len) { + struct mg_http_multipart_part mp; + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + memset(&mp, 0, sizeof(mp)); + + mp.var_name = pd->mp_stream.var_name; + mp.file_name = pd->mp_stream.file_name; + mp.user_data = pd->mp_stream.user_data; + mp.data.p = data; + mp.data.len = data_len; + mg_call(c, pd->endpoint_handler, c->user_data, ev, &mp); + pd->mp_stream.user_data = mp.user_data; +} + +static int mg_http_multipart_got_chunk(struct mg_connection *c) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + struct mbuf *io = &c->recv_mbuf; + + mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_DATA, io->buf, + pd->mp_stream.prev_io_len); + mbuf_remove(io, pd->mp_stream.prev_io_len); + pd->mp_stream.prev_io_len = 0; + pd->mp_stream.state = MPS_WAITING_FOR_CHUNK; + + return 0; +} + +static int mg_http_multipart_finalize(struct mg_connection *c) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + + mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_END, NULL, 0); + MG_FREE((void *) pd->mp_stream.file_name); + pd->mp_stream.file_name = NULL; + MG_FREE((void *) pd->mp_stream.var_name); + pd->mp_stream.var_name = NULL; + mg_http_multipart_call_handler(c, MG_EV_HTTP_MULTIPART_REQUEST_END, NULL, 0); + mg_http_free_proto_data_mp_stream(&pd->mp_stream); + pd->mp_stream.state = MPS_FINISHED; + + return 1; +} + +static int mg_http_multipart_wait_for_boundary(struct mg_connection *c) { + const char *boundary; + struct mbuf *io = &c->recv_mbuf; + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + + if (pd->mp_stream.boundary == NULL) { + pd->mp_stream.state = MPS_FINALIZE; + DBG(("Invalid request: boundary not initialized")); + return 0; + } + + if ((int) io->len < pd->mp_stream.boundary_len + 2) { + return 0; + } + + boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len); + if (boundary != NULL) { + const char *boundary_end = (boundary + pd->mp_stream.boundary_len); + if (io->len - (boundary_end - io->buf) < 4) { + return 0; + } + if (strncmp(boundary_end, "--\r\n", 4) == 0) { + pd->mp_stream.state = MPS_FINALIZE; + mbuf_remove(io, (boundary_end - io->buf) + 4); + } else { + pd->mp_stream.state = MPS_GOT_BOUNDARY; + } + } else { + return 0; + } + + return 1; +} + +static int mg_http_multipart_process_boundary(struct mg_connection *c) { + int data_size; + const char *boundary, *block_begin; + struct mbuf *io = &c->recv_mbuf; + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + char file_name[100], var_name[100]; + int line_len; + boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len); + block_begin = boundary + pd->mp_stream.boundary_len + 2; + data_size = io->len - (block_begin - io->buf); + + while (data_size > 0 && + (line_len = mg_get_line_len(block_begin, data_size)) != 0) { + if (line_len > (int) sizeof(CONTENT_DISPOSITION) && + mg_ncasecmp(block_begin, CONTENT_DISPOSITION, + sizeof(CONTENT_DISPOSITION) - 1) == 0) { + struct mg_str header; + + header.p = block_begin + sizeof(CONTENT_DISPOSITION) - 1; + header.len = line_len - sizeof(CONTENT_DISPOSITION) - 1; + mg_http_parse_header(&header, "name", var_name, sizeof(var_name) - 2); + mg_http_parse_header(&header, "filename", file_name, + sizeof(file_name) - 2); + block_begin += line_len; + data_size -= line_len; + continue; + } + + if (line_len == 2 && mg_ncasecmp(block_begin, "\r\n", 2) == 0) { + mbuf_remove(io, block_begin - io->buf + 2); + + if (pd->mp_stream.processing_part != 0) { + mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_END, NULL, 0); + } + + MG_FREE((void *) pd->mp_stream.file_name); + pd->mp_stream.file_name = strdup(file_name); + MG_FREE((void *) pd->mp_stream.var_name); + pd->mp_stream.var_name = strdup(var_name); + + mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_BEGIN, NULL, 0); + pd->mp_stream.state = MPS_WAITING_FOR_CHUNK; + pd->mp_stream.processing_part++; + return 1; + } + + block_begin += line_len; + } + + pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY; + + return 0; +} + +static int mg_http_multipart_continue_wait_for_chunk(struct mg_connection *c) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + struct mbuf *io = &c->recv_mbuf; + + const char *boundary; + if ((int) io->len < pd->mp_stream.boundary_len + 6 /* \r\n, --, -- */) { + return 0; + } + + boundary = c_strnstr(io->buf, pd->mp_stream.boundary, io->len); + if (boundary == NULL && pd->mp_stream.prev_io_len == 0) { + pd->mp_stream.prev_io_len = io->len; + return 0; + } else if (boundary == NULL && + (int) io->len > + pd->mp_stream.prev_io_len + pd->mp_stream.boundary_len + 4) { + pd->mp_stream.state = MPS_GOT_CHUNK; + return 1; + } else if (boundary != NULL) { + int data_size = (boundary - io->buf - 4); + mg_http_multipart_call_handler(c, MG_EV_HTTP_PART_DATA, io->buf, data_size); + mbuf_remove(io, (boundary - io->buf)); + pd->mp_stream.prev_io_len = 0; + pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY; + return 1; + } else { + return 0; + } +} + +static void mg_http_multipart_continue(struct mg_connection *c) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(c); + while (1) { + switch (pd->mp_stream.state) { + case MPS_BEGIN: { + pd->mp_stream.state = MPS_WAITING_FOR_BOUNDARY; + break; + } + case MPS_WAITING_FOR_BOUNDARY: { + if (mg_http_multipart_wait_for_boundary(c) == 0) { + return; + } + break; + } + case MPS_GOT_BOUNDARY: { + if (mg_http_multipart_process_boundary(c) == 0) { + return; + } + break; + } + case MPS_WAITING_FOR_CHUNK: { + if (mg_http_multipart_continue_wait_for_chunk(c) == 0) { + return; + } + break; + } + case MPS_GOT_CHUNK: { + if (mg_http_multipart_got_chunk(c) == 0) { + return; + } + break; + } + case MPS_FINALIZE: { + if (mg_http_multipart_finalize(c) == 0) { + return; + } + break; + } + case MPS_FINISHED: { + mbuf_remove(&c->recv_mbuf, c->recv_mbuf.len); + return; + } + } + } +} + +struct file_upload_state { + char *lfn; + size_t num_recd; + FILE *fp; +}; + +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ + +void mg_set_protocol_http_websocket(struct mg_connection *nc) { + nc->proto_handler = mg_http_handler; +} + +const char *mg_status_message(int status_code) { + switch (status_code) { + case 206: + return "Partial Content"; + case 301: + return "Moved"; + case 302: + return "Found"; + case 400: + return "Bad Request"; + case 401: + return "Unauthorized"; + case 403: + return "Forbidden"; + case 404: + return "Not Found"; + case 416: + return "Requested Range Not Satisfiable"; + case 418: + return "I'm a teapot"; + case 500: + return "Internal Server Error"; + case 502: + return "Bad Gateway"; + case 503: + return "Service Unavailable"; + +#if MG_ENABLE_EXTRA_ERRORS_DESC + case 100: + return "Continue"; + case 101: + return "Switching Protocols"; + case 102: + return "Processing"; + case 200: + return "OK"; + case 201: + return "Created"; + case 202: + return "Accepted"; + case 203: + return "Non-Authoritative Information"; + case 204: + return "No Content"; + case 205: + return "Reset Content"; + case 207: + return "Multi-Status"; + case 208: + return "Already Reported"; + case 226: + return "IM Used"; + case 300: + return "Multiple Choices"; + case 303: + return "See Other"; + case 304: + return "Not Modified"; + case 305: + return "Use Proxy"; + case 306: + return "Switch Proxy"; + case 307: + return "Temporary Redirect"; + case 308: + return "Permanent Redirect"; + case 402: + return "Payment Required"; + case 405: + return "Method Not Allowed"; + case 406: + return "Not Acceptable"; + case 407: + return "Proxy Authentication Required"; + case 408: + return "Request Timeout"; + case 409: + return "Conflict"; + case 410: + return "Gone"; + case 411: + return "Length Required"; + case 412: + return "Precondition Failed"; + case 413: + return "Payload Too Large"; + case 414: + return "URI Too Long"; + case 415: + return "Unsupported Media Type"; + case 417: + return "Expectation Failed"; + case 422: + return "Unprocessable Entity"; + case 423: + return "Locked"; + case 424: + return "Failed Dependency"; + case 426: + return "Upgrade Required"; + case 428: + return "Precondition Required"; + case 429: + return "Too Many Requests"; + case 431: + return "Request Header Fields Too Large"; + case 451: + return "Unavailable For Legal Reasons"; + case 501: + return "Not Implemented"; + case 504: + return "Gateway Timeout"; + case 505: + return "HTTP Version Not Supported"; + case 506: + return "Variant Also Negotiates"; + case 507: + return "Insufficient Storage"; + case 508: + return "Loop Detected"; + case 510: + return "Not Extended"; + case 511: + return "Network Authentication Required"; +#endif /* MG_ENABLE_EXTRA_ERRORS_DESC */ + + default: + return "OK"; + } +} + +void mg_send_response_line_s(struct mg_connection *nc, int status_code, + const struct mg_str extra_headers) { + mg_printf(nc, "HTTP/1.1 %d %s\r\nServer: %s\r\n", status_code, + mg_status_message(status_code), mg_version_header); + if (extra_headers.len > 0) { + mg_printf(nc, "%.*s\r\n", (int) extra_headers.len, extra_headers.p); + } +} + +void mg_send_response_line(struct mg_connection *nc, int status_code, + const char *extra_headers) { + mg_send_response_line_s(nc, status_code, mg_mk_str(extra_headers)); +} + +void mg_http_send_redirect(struct mg_connection *nc, int status_code, + const struct mg_str location, + const struct mg_str extra_headers) { + char bbody[100], *pbody = bbody; + int bl = mg_asprintf(&pbody, sizeof(bbody), + "

Moved here.\r\n", + (int) location.len, location.p); + char bhead[150], *phead = bhead; + mg_asprintf(&phead, sizeof(bhead), + "Location: %.*s\r\n" + "Content-Type: text/html\r\n" + "Content-Length: %d\r\n" + "Cache-Control: no-cache\r\n" + "%.*s%s", + (int) location.len, location.p, bl, (int) extra_headers.len, + extra_headers.p, (extra_headers.len > 0 ? "\r\n" : "")); + mg_send_response_line(nc, status_code, phead); + if (phead != bhead) MG_FREE(phead); + mg_send(nc, pbody, bl); + if (pbody != bbody) MG_FREE(pbody); +} + +void mg_send_head(struct mg_connection *c, int status_code, + int64_t content_length, const char *extra_headers) { + mg_send_response_line(c, status_code, extra_headers); + if (content_length < 0) { + mg_printf(c, "%s", "Transfer-Encoding: chunked\r\n"); + } else { + mg_printf(c, "Content-Length: %" INT64_FMT "\r\n", content_length); + } + mg_send(c, "\r\n", 2); +} + +void mg_http_send_error(struct mg_connection *nc, int code, + const char *reason) { + if (!reason) reason = mg_status_message(code); + LOG(LL_DEBUG, ("%p %d %s", nc, code, reason)); + mg_send_head(nc, code, strlen(reason), + "Content-Type: text/plain\r\nConnection: close"); + mg_send(nc, reason, strlen(reason)); + nc->flags |= MG_F_SEND_AND_CLOSE; +} + +#if MG_ENABLE_FILESYSTEM +static void mg_http_construct_etag(char *buf, size_t buf_len, + const cs_stat_t *st) { + snprintf(buf, buf_len, "\"%lx.%" INT64_FMT "\"", (unsigned long) st->st_mtime, + (int64_t) st->st_size); +} + +#ifndef WINCE +static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t) { + strftime(buf, buf_len, "%a, %d %b %Y %H:%M:%S GMT", gmtime(t)); +} +#else +/* Look wince_lib.c for WindowsCE implementation */ +static void mg_gmt_time_string(char *buf, size_t buf_len, time_t *t); +#endif + +static int mg_http_parse_range_header(const struct mg_str *header, int64_t *a, + int64_t *b) { + /* + * There is no snscanf. Headers are not guaranteed to be NUL-terminated, + * so we have this. Ugh. + */ + int result; + char *p = (char *) MG_MALLOC(header->len + 1); + if (p == NULL) return 0; + memcpy(p, header->p, header->len); + p[header->len] = '\0'; + result = sscanf(p, "bytes=%" INT64_FMT "-%" INT64_FMT, a, b); + MG_FREE(p); + return result; +} + +void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm, + const char *path, const struct mg_str mime_type, + const struct mg_str extra_headers) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + cs_stat_t st; + LOG(LL_DEBUG, ("%p [%s] %.*s", nc, path, (int) mime_type.len, mime_type.p)); + if (mg_stat(path, &st) != 0 || (pd->file.fp = mg_fopen(path, "rb")) == NULL) { + int code, err = mg_get_errno(); + switch (err) { + case EACCES: + code = 403; + break; + case ENOENT: + code = 404; + break; + default: + code = 500; + }; + mg_http_send_error(nc, code, "Open failed"); + } else { + char etag[50], current_time[50], last_modified[50], range[70]; + time_t t = (time_t) mg_time(); + int64_t r1 = 0, r2 = 0, cl = st.st_size; + struct mg_str *range_hdr = mg_get_http_header(hm, "Range"); + int n, status_code = 200; + + /* Handle Range header */ + range[0] = '\0'; + if (range_hdr != NULL && + (n = mg_http_parse_range_header(range_hdr, &r1, &r2)) > 0 && r1 >= 0 && + r2 >= 0) { + /* If range is specified like "400-", set second limit to content len */ + if (n == 1) { + r2 = cl - 1; + } + if (r1 > r2 || r2 >= cl) { + status_code = 416; + cl = 0; + snprintf(range, sizeof(range), + "Content-Range: bytes */%" INT64_FMT "\r\n", + (int64_t) st.st_size); + } else { + status_code = 206; + cl = r2 - r1 + 1; + snprintf(range, sizeof(range), "Content-Range: bytes %" INT64_FMT + "-%" INT64_FMT "/%" INT64_FMT "\r\n", + r1, r1 + cl - 1, (int64_t) st.st_size); +#if _FILE_OFFSET_BITS == 64 || _POSIX_C_SOURCE >= 200112L || \ + _XOPEN_SOURCE >= 600 + fseeko(pd->file.fp, r1, SEEK_SET); +#else + fseek(pd->file.fp, (long) r1, SEEK_SET); +#endif + } + } + +#if !MG_DISABLE_HTTP_KEEP_ALIVE + { + struct mg_str *conn_hdr = mg_get_http_header(hm, "Connection"); + if (conn_hdr != NULL) { + pd->file.keepalive = (mg_vcasecmp(conn_hdr, "keep-alive") == 0); + } else { + pd->file.keepalive = (mg_vcmp(&hm->proto, "HTTP/1.1") == 0); + } + } +#endif + + mg_http_construct_etag(etag, sizeof(etag), &st); + mg_gmt_time_string(current_time, sizeof(current_time), &t); + mg_gmt_time_string(last_modified, sizeof(last_modified), &st.st_mtime); + /* + * Content length casted to size_t because: + * 1) that's the maximum buffer size anyway + * 2) ESP8266 RTOS SDK newlib vprintf cannot contain a 64bit arg at non-last + * position + * TODO(mkm): fix ESP8266 RTOS SDK + */ + mg_send_response_line_s(nc, status_code, extra_headers); + mg_printf(nc, + "Date: %s\r\n" + "Last-Modified: %s\r\n" + "Accept-Ranges: bytes\r\n" + "Content-Type: %.*s\r\n" + "Connection: %s\r\n" + "Content-Length: %" SIZE_T_FMT + "\r\n" + "%sEtag: %s\r\n\r\n", + current_time, last_modified, (int) mime_type.len, mime_type.p, + (pd->file.keepalive ? "keep-alive" : "close"), (size_t) cl, range, + etag); + + pd->file.cl = cl; + pd->file.type = DATA_FILE; + mg_http_transfer_file_data(nc); + } +} + +static void mg_http_serve_file2(struct mg_connection *nc, const char *path, + struct http_message *hm, + struct mg_serve_http_opts *opts) { +#if MG_ENABLE_HTTP_SSI + if (mg_match_prefix(opts->ssi_pattern, strlen(opts->ssi_pattern), path) > 0) { + mg_handle_ssi_request(nc, hm, path, opts); + return; + } +#endif + mg_http_serve_file(nc, hm, path, mg_get_mime_type(path, "text/plain", opts), + mg_mk_str(opts->extra_headers)); +} + +#endif + +int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, + int is_form_url_encoded) { + int i, j, a, b; +#define HEXTOI(x) (isdigit(x) ? x - '0' : x - 'W') + + for (i = j = 0; i < src_len && j < dst_len - 1; i++, j++) { + if (src[i] == '%') { + if (i < src_len - 2 && isxdigit(*(const unsigned char *) (src + i + 1)) && + isxdigit(*(const unsigned char *) (src + i + 2))) { + a = tolower(*(const unsigned char *) (src + i + 1)); + b = tolower(*(const unsigned char *) (src + i + 2)); + dst[j] = (char) ((HEXTOI(a) << 4) | HEXTOI(b)); + i += 2; + } else { + return -1; + } + } else if (is_form_url_encoded && src[i] == '+') { + dst[j] = ' '; + } else { + dst[j] = src[i]; + } + } + + dst[j] = '\0'; /* Null-terminate the destination */ + + return i >= src_len ? j : -1; +} + +int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, + size_t dst_len) { + const char *p, *e, *s; + size_t name_len; + int len; + + /* + * According to the documentation function returns negative + * value in case of error. For debug purposes it returns: + * -1 - src is wrong (NUUL) + * -2 - dst is wrong (NULL) + * -3 - failed to decode url or dst is to small + * -4 - name does not exist + */ + if (dst == NULL || dst_len == 0) { + len = -2; + } else if (buf->p == NULL || name == NULL || buf->len == 0) { + len = -1; + dst[0] = '\0'; + } else { + name_len = strlen(name); + e = buf->p + buf->len; + len = -4; + dst[0] = '\0'; + + for (p = buf->p; p + name_len < e; p++) { + if ((p == buf->p || p[-1] == '&') && p[name_len] == '=' && + !mg_ncasecmp(name, p, name_len)) { + p += name_len + 1; + s = (const char *) memchr(p, '&', (size_t)(e - p)); + if (s == NULL) { + s = e; + } + len = mg_url_decode(p, (size_t)(s - p), dst, dst_len, 1); + /* -1 means: failed to decode or dst is too small */ + if (len == -1) { + len = -3; + } + break; + } + } + } + + return len; +} + +void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len) { + char chunk_size[50]; + int n; + + n = snprintf(chunk_size, sizeof(chunk_size), "%lX\r\n", (unsigned long) len); + mg_send(nc, chunk_size, n); + mg_send(nc, buf, len); + mg_send(nc, "\r\n", 2); +} + +void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...) { + char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem; + int len; + va_list ap; + + va_start(ap, fmt); + len = mg_avprintf(&buf, sizeof(mem), fmt, ap); + va_end(ap); + + if (len >= 0) { + mg_send_http_chunk(nc, buf, len); + } + + /* LCOV_EXCL_START */ + if (buf != mem && buf != NULL) { + MG_FREE(buf); + } + /* LCOV_EXCL_STOP */ +} + +void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...) { + char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem; + int i, j, len; + va_list ap; + + va_start(ap, fmt); + len = mg_avprintf(&buf, sizeof(mem), fmt, ap); + va_end(ap); + + if (len >= 0) { + for (i = j = 0; i < len; i++) { + if (buf[i] == '<' || buf[i] == '>') { + mg_send(nc, buf + j, i - j); + mg_send(nc, buf[i] == '<' ? "<" : ">", 4); + j = i + 1; + } + } + mg_send(nc, buf + j, i - j); + } + + /* LCOV_EXCL_START */ + if (buf != mem && buf != NULL) { + MG_FREE(buf); + } + /* LCOV_EXCL_STOP */ +} + +int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, + size_t buf_size) { + int ch = ' ', ch1 = ',', len = 0, n = strlen(var_name); + const char *p, *end = hdr ? hdr->p + hdr->len : NULL, *s = NULL; + + if (buf != NULL && buf_size > 0) buf[0] = '\0'; + if (hdr == NULL) return 0; + + /* Find where variable starts */ + for (s = hdr->p; s != NULL && s + n < end; s++) { + if ((s == hdr->p || s[-1] == ch || s[-1] == ch1 || s[-1] == ';') && + s[n] == '=' && !strncmp(s, var_name, n)) + break; + } + + if (s != NULL && &s[n + 1] < end) { + s += n + 1; + if (*s == '"' || *s == '\'') { + ch = ch1 = *s++; + } + p = s; + while (p < end && p[0] != ch && p[0] != ch1 && len < (int) buf_size) { + if (ch != ' ' && p[0] == '\\' && p[1] == ch) p++; + buf[len++] = *p++; + } + if (len >= (int) buf_size || (ch != ' ' && *p != ch)) { + len = 0; + } else { + if (len > 0 && s[len - 1] == ',') len--; + if (len > 0 && s[len - 1] == ';') len--; + buf[len] = '\0'; + } + } + + return len; +} + +int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len, + char *pass, size_t pass_len) { + struct mg_str *hdr = mg_get_http_header(hm, "Authorization"); + if (hdr == NULL) return -1; + return mg_parse_http_basic_auth(hdr, user, user_len, pass, pass_len); +} + +int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len, + char *pass, size_t pass_len) { + char *buf = NULL; + char fmt[64]; + int res = 0; + + if (mg_strncmp(*hdr, mg_mk_str("Basic "), 6) != 0) return -1; + + buf = (char *) MG_MALLOC(hdr->len); + cs_base64_decode((unsigned char *) hdr->p + 6, hdr->len, buf, NULL); + + /* e.g. "%123[^:]:%321[^\n]" */ + snprintf(fmt, sizeof(fmt), "%%%" SIZE_T_FMT "[^:]:%%%" SIZE_T_FMT "[^\n]", + user_len - 1, pass_len - 1); + if (sscanf(buf, fmt, user, pass) == 0) { + res = -1; + } + + MG_FREE(buf); + return res; +} + +#if MG_ENABLE_FILESYSTEM +static int mg_is_file_hidden(const char *path, + const struct mg_serve_http_opts *opts, + int exclude_specials) { + const char *p1 = opts->per_directory_auth_file; + const char *p2 = opts->hidden_file_pattern; + + /* Strip directory path from the file name */ + const char *pdir = strrchr(path, DIRSEP); + if (pdir != NULL) { + path = pdir + 1; + } + + return (exclude_specials && (!strcmp(path, ".") || !strcmp(path, ".."))) || + (p1 != NULL && mg_match_prefix(p1, strlen(p1), path) == strlen(p1)) || + (p2 != NULL && mg_match_prefix(p2, strlen(p2), path) > 0); +} + +#if !MG_DISABLE_HTTP_DIGEST_AUTH + +#ifndef MG_EXT_MD5 +void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest) { + size_t i; + cs_md5_ctx md5_ctx; + cs_md5_init(&md5_ctx); + for (i = 0; i < num_msgs; i++) { + cs_md5_update(&md5_ctx, msgs[i], msg_lens[i]); + } + cs_md5_final(digest, &md5_ctx); +} +#else +extern void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest); +#endif + +void cs_md5(char buf[33], ...) { + unsigned char hash[16]; + const uint8_t *msgs[20], *p; + size_t msg_lens[20]; + size_t num_msgs = 0; + va_list ap; + + va_start(ap, buf); + while ((p = va_arg(ap, const unsigned char *) ) != NULL) { + msgs[num_msgs] = p; + msg_lens[num_msgs] = va_arg(ap, size_t); + num_msgs++; + } + va_end(ap); + + mg_hash_md5_v(num_msgs, msgs, msg_lens, hash); + cs_to_hex(buf, hash, sizeof(hash)); +} + +static void mg_mkmd5resp(const char *method, size_t method_len, const char *uri, + size_t uri_len, const char *ha1, size_t ha1_len, + const char *nonce, size_t nonce_len, const char *nc, + size_t nc_len, const char *cnonce, size_t cnonce_len, + const char *qop, size_t qop_len, char *resp) { + static const char colon[] = ":"; + static const size_t one = 1; + char ha2[33]; + cs_md5(ha2, method, method_len, colon, one, uri, uri_len, NULL); + cs_md5(resp, ha1, ha1_len, colon, one, nonce, nonce_len, colon, one, nc, + nc_len, colon, one, cnonce, cnonce_len, colon, one, qop, qop_len, + colon, one, ha2, sizeof(ha2) - 1, NULL); +} + +int mg_http_create_digest_auth_header(char *buf, size_t buf_len, + const char *method, const char *uri, + const char *auth_domain, const char *user, + const char *passwd, const char *nonce) { + static const char colon[] = ":", qop[] = "auth"; + static const size_t one = 1; + char ha1[33], resp[33], cnonce[40]; + + snprintf(cnonce, sizeof(cnonce), "%lx", (unsigned long) mg_time()); + cs_md5(ha1, user, (size_t) strlen(user), colon, one, auth_domain, + (size_t) strlen(auth_domain), colon, one, passwd, + (size_t) strlen(passwd), NULL); + mg_mkmd5resp(method, strlen(method), uri, strlen(uri), ha1, sizeof(ha1) - 1, + nonce, strlen(nonce), "1", one, cnonce, strlen(cnonce), qop, + sizeof(qop) - 1, resp); + return snprintf(buf, buf_len, + "Authorization: Digest username=\"%s\"," + "realm=\"%s\",uri=\"%s\",qop=%s,nc=1,cnonce=%s," + "nonce=%s,response=%s\r\n", + user, auth_domain, uri, qop, cnonce, nonce, resp); +} + +/* + * Check for authentication timeout. + * Clients send time stamp encoded in nonce. Make sure it is not too old, + * to prevent replay attacks. + * Assumption: nonce is a hexadecimal number of seconds since 1970. + */ +static int mg_check_nonce(const char *nonce) { + unsigned long now = (unsigned long) mg_time(); + unsigned long val = (unsigned long) strtoul(nonce, NULL, 16); + return (now >= val) && (now - val < 60 * 60); +} + +int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain, + FILE *fp) { + struct mg_str *hdr; + char username[50], cnonce[64], response[40], uri[200], qop[20], nc[20], + nonce[30]; + + /* Parse "Authorization:" header, fail fast on parse error */ + if (hm == NULL || fp == NULL || + (hdr = mg_get_http_header(hm, "Authorization")) == NULL || + mg_http_parse_header(hdr, "username", username, sizeof(username)) == 0 || + mg_http_parse_header(hdr, "cnonce", cnonce, sizeof(cnonce)) == 0 || + mg_http_parse_header(hdr, "response", response, sizeof(response)) == 0 || + mg_http_parse_header(hdr, "uri", uri, sizeof(uri)) == 0 || + mg_http_parse_header(hdr, "qop", qop, sizeof(qop)) == 0 || + mg_http_parse_header(hdr, "nc", nc, sizeof(nc)) == 0 || + mg_http_parse_header(hdr, "nonce", nonce, sizeof(nonce)) == 0 || + mg_check_nonce(nonce) == 0) { + return 0; + } + + /* NOTE(lsm): due to a bug in MSIE, we do not compare URIs */ + + return mg_check_digest_auth( + hm->method, + mg_mk_str_n( + hm->uri.p, + hm->uri.len + (hm->query_string.len ? hm->query_string.len + 1 : 0)), + mg_mk_str(username), mg_mk_str(cnonce), mg_mk_str(response), + mg_mk_str(qop), mg_mk_str(nc), mg_mk_str(nonce), mg_mk_str(auth_domain), + fp); +} + +int mg_check_digest_auth(struct mg_str method, struct mg_str uri, + struct mg_str username, struct mg_str cnonce, + struct mg_str response, struct mg_str qop, + struct mg_str nc, struct mg_str nonce, + struct mg_str auth_domain, FILE *fp) { + char buf[128], f_user[sizeof(buf)], f_ha1[sizeof(buf)], f_domain[sizeof(buf)]; + char expected_response[33]; + + /* + * Read passwords file line by line. If should have htdigest format, + * i.e. each line should be a colon-separated sequence: + * USER_NAME:DOMAIN_NAME:HA1_HASH_OF_USER_DOMAIN_AND_PASSWORD + */ + while (fgets(buf, sizeof(buf), fp) != NULL) { + if (sscanf(buf, "%[^:]:%[^:]:%s", f_user, f_domain, f_ha1) == 3 && + mg_vcmp(&username, f_user) == 0 && + mg_vcmp(&auth_domain, f_domain) == 0) { + /* Username and domain matched, check the password */ + mg_mkmd5resp(method.p, method.len, uri.p, uri.len, f_ha1, strlen(f_ha1), + nonce.p, nonce.len, nc.p, nc.len, cnonce.p, cnonce.len, + qop.p, qop.len, expected_response); + LOG(LL_DEBUG, + ("%.*s %s %.*s %s", (int) username.len, username.p, f_domain, + (int) response.len, response.p, expected_response)); + return mg_ncasecmp(response.p, expected_response, response.len) == 0; + } + } + + /* None of the entries in the passwords file matched - return failure */ + return 0; +} + +int mg_http_is_authorized(struct http_message *hm, struct mg_str path, + const char *domain, const char *passwords_file, + int flags) { + char buf[MG_MAX_PATH]; + const char *p; + FILE *fp; + int authorized = 1; + + if (domain != NULL && passwords_file != NULL) { + if (flags & MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE) { + fp = mg_fopen(passwords_file, "r"); + } else if (flags & MG_AUTH_FLAG_IS_DIRECTORY) { + snprintf(buf, sizeof(buf), "%.*s%c%s", (int) path.len, path.p, DIRSEP, + passwords_file); + fp = mg_fopen(buf, "r"); + } else { + p = strrchr(path.p, DIRSEP); + if (p == NULL) p = path.p; + snprintf(buf, sizeof(buf), "%.*s%c%s", (int) (p - path.p), path.p, DIRSEP, + passwords_file); + fp = mg_fopen(buf, "r"); + } + + if (fp != NULL) { + authorized = mg_http_check_digest_auth(hm, domain, fp); + fclose(fp); + } else if (!(flags & MG_AUTH_FLAG_ALLOW_MISSING_FILE)) { + authorized = 0; + } + } + + LOG(LL_DEBUG, ("%.*s %s %x %d", (int) path.len, path.p, + passwords_file ? passwords_file : "", flags, authorized)); + return authorized; +} +#else +int mg_http_is_authorized(struct http_message *hm, const struct mg_str path, + const char *domain, const char *passwords_file, + int flags) { + (void) hm; + (void) path; + (void) domain; + (void) passwords_file; + (void) flags; + return 1; +} +#endif + +#if MG_ENABLE_DIRECTORY_LISTING +static void mg_escape(const char *src, char *dst, size_t dst_len) { + size_t n = 0; + while (*src != '\0' && n + 5 < dst_len) { + unsigned char ch = *(unsigned char *) src++; + if (ch == '<') { + n += snprintf(dst + n, dst_len - n, "%s", "<"); + } else { + dst[n++] = ch; + } + } + dst[n] = '\0'; +} + +static void mg_print_dir_entry(struct mg_connection *nc, const char *file_name, + cs_stat_t *stp) { + char size[64], mod[64], path[MG_MAX_PATH]; + int64_t fsize = stp->st_size; + int is_dir = S_ISDIR(stp->st_mode); + const char *slash = is_dir ? "/" : ""; + struct mg_str href; + + if (is_dir) { + snprintf(size, sizeof(size), "%s", "[DIRECTORY]"); + } else { + /* + * We use (double) cast below because MSVC 6 compiler cannot + * convert unsigned __int64 to double. + */ + if (fsize < 1024) { + snprintf(size, sizeof(size), "%d", (int) fsize); + } else if (fsize < 0x100000) { + snprintf(size, sizeof(size), "%.1fk", (double) fsize / 1024.0); + } else if (fsize < 0x40000000) { + snprintf(size, sizeof(size), "%.1fM", (double) fsize / 1048576); + } else { + snprintf(size, sizeof(size), "%.1fG", (double) fsize / 1073741824); + } + } + strftime(mod, sizeof(mod), "%d-%b-%Y %H:%M", localtime(&stp->st_mtime)); + mg_escape(file_name, path, sizeof(path)); + href = mg_url_encode(mg_mk_str(file_name)); + mg_printf_http_chunk(nc, + "%s%s" + "%s%s\n", + href.p, slash, path, slash, mod, is_dir ? -1 : fsize, + size); + free((void *) href.p); +} + +static void mg_scan_directory(struct mg_connection *nc, const char *dir, + const struct mg_serve_http_opts *opts, + void (*func)(struct mg_connection *, const char *, + cs_stat_t *)) { + char path[MG_MAX_PATH]; + cs_stat_t st; + struct dirent *dp; + DIR *dirp; + + LOG(LL_DEBUG, ("%p [%s]", nc, dir)); + if ((dirp = (opendir(dir))) != NULL) { + while ((dp = readdir(dirp)) != NULL) { + /* Do not show current dir and hidden files */ + if (mg_is_file_hidden((const char *) dp->d_name, opts, 1)) { + continue; + } + snprintf(path, sizeof(path), "%s/%s", dir, dp->d_name); + if (mg_stat(path, &st) == 0) { + func(nc, (const char *) dp->d_name, &st); + } + } + closedir(dirp); + } else { + LOG(LL_DEBUG, ("%p opendir(%s) -> %d", nc, dir, mg_get_errno())); + } +} + +static void mg_send_directory_listing(struct mg_connection *nc, const char *dir, + struct http_message *hm, + struct mg_serve_http_opts *opts) { + static const char *sort_js_code = + ""; + + mg_send_response_line(nc, 200, opts->extra_headers); + mg_printf(nc, "%s: %s\r\n%s: %s\r\n\r\n", "Transfer-Encoding", "chunked", + "Content-Type", "text/html; charset=utf-8"); + + mg_printf_http_chunk( + nc, + "Index of %.*s%s%s" + "\n" + "

Index of %.*s

\n" + "" + "\n" + "\n" + "", + (int) hm->uri.len, hm->uri.p, sort_js_code, sort_js_code2, + (int) hm->uri.len, hm->uri.p); + mg_scan_directory(nc, dir, opts, mg_print_dir_entry); + mg_printf_http_chunk(nc, + "\n" + "
Name" + "Modified" + "Size


\n" + "
%s
\n" + "", + mg_version_header); + mg_send_http_chunk(nc, "", 0); + /* TODO(rojer): Remove when cesanta/dev/issues/197 is fixed. */ + nc->flags |= MG_F_SEND_AND_CLOSE; +} +#endif /* MG_ENABLE_DIRECTORY_LISTING */ + +/* + * Given a directory path, find one of the files specified in the + * comma-separated list of index files `list`. + * First found index file wins. If an index file is found, then gets + * appended to the `path`, stat-ed, and result of `stat()` passed to `stp`. + * If index file is not found, then `path` and `stp` remain unchanged. + */ +MG_INTERNAL void mg_find_index_file(const char *path, const char *list, + char **index_file, cs_stat_t *stp) { + struct mg_str vec; + size_t path_len = strlen(path); + int found = 0; + *index_file = NULL; + + /* Traverse index files list. For each entry, append it to the given */ + /* path and see if the file exists. If it exists, break the loop */ + while ((list = mg_next_comma_list_entry(list, &vec, NULL)) != NULL) { + cs_stat_t st; + size_t len = path_len + 1 + vec.len + 1; + *index_file = (char *) MG_REALLOC(*index_file, len); + if (*index_file == NULL) break; + snprintf(*index_file, len, "%s%c%.*s", path, DIRSEP, (int) vec.len, vec.p); + + /* Does it exist? Is it a file? */ + if (mg_stat(*index_file, &st) == 0 && S_ISREG(st.st_mode)) { + /* Yes it does, break the loop */ + *stp = st; + found = 1; + break; + } + } + if (!found) { + MG_FREE(*index_file); + *index_file = NULL; + } + LOG(LL_DEBUG, ("[%s] [%s]", path, (*index_file ? *index_file : ""))); +} + +#if MG_ENABLE_HTTP_URL_REWRITES +static int mg_http_send_port_based_redirect( + struct mg_connection *c, struct http_message *hm, + const struct mg_serve_http_opts *opts) { + const char *rewrites = opts->url_rewrites; + struct mg_str a, b; + char local_port[20] = {'%'}; + + mg_conn_addr_to_str(c, local_port + 1, sizeof(local_port) - 1, + MG_SOCK_STRINGIFY_PORT); + + while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) { + if (mg_vcmp(&a, local_port) == 0) { + mg_send_response_line(c, 301, NULL); + mg_printf(c, "Content-Length: 0\r\nLocation: %.*s%.*s\r\n\r\n", + (int) b.len, b.p, (int) (hm->proto.p - hm->uri.p - 1), + hm->uri.p); + return 1; + } + } + + return 0; +} + +static void mg_reverse_proxy_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct http_message *hm = (struct http_message *) ev_data; + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + + if (pd == NULL || pd->reverse_proxy_data.linked_conn == NULL) { + DBG(("%p: upstream closed", nc)); + return; + } + + switch (ev) { + case MG_EV_CONNECT: + if (*(int *) ev_data != 0) { + mg_http_send_error(pd->reverse_proxy_data.linked_conn, 502, NULL); + } + break; + /* TODO(mkm): handle streaming */ + case MG_EV_HTTP_REPLY: + mg_send(pd->reverse_proxy_data.linked_conn, hm->message.p, + hm->message.len); + pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE; + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + case MG_EV_CLOSE: + pd->reverse_proxy_data.linked_conn->flags |= MG_F_SEND_AND_CLOSE; + break; + } + +#if MG_ENABLE_CALLBACK_USERDATA + (void) user_data; +#endif +} + +void mg_http_reverse_proxy(struct mg_connection *nc, + const struct http_message *hm, struct mg_str mount, + struct mg_str upstream) { + struct mg_connection *be; + char burl[256], *purl = burl; + int i; + const char *error; + struct mg_connect_opts opts; + struct mg_str path = MG_NULL_STR, user_info = MG_NULL_STR, host = MG_NULL_STR; + memset(&opts, 0, sizeof(opts)); + opts.error_string = &error; + + mg_asprintf(&purl, sizeof(burl), "%.*s%.*s", (int) upstream.len, upstream.p, + (int) (hm->uri.len - mount.len), hm->uri.p + mount.len); + + be = mg_connect_http_base(nc->mgr, MG_CB(mg_reverse_proxy_handler, NULL), + opts, "http", NULL, "https", NULL, purl, &path, + &user_info, &host); + LOG(LL_DEBUG, ("Proxying %.*s to %s (rule: %.*s)", (int) hm->uri.len, + hm->uri.p, purl, (int) mount.len, mount.p)); + + if (be == NULL) { + LOG(LL_ERROR, ("Error connecting to %s: %s", purl, error)); + mg_http_send_error(nc, 502, NULL); + goto cleanup; + } + + /* link connections to each other, they must live and die together */ + mg_http_get_proto_data(be)->reverse_proxy_data.linked_conn = nc; + mg_http_get_proto_data(nc)->reverse_proxy_data.linked_conn = be; + + /* send request upstream */ + mg_printf(be, "%.*s %.*s HTTP/1.1\r\n", (int) hm->method.len, hm->method.p, + (int) path.len, path.p); + + mg_printf(be, "Host: %.*s\r\n", (int) host.len, host.p); + for (i = 0; i < MG_MAX_HTTP_HEADERS && hm->header_names[i].len > 0; i++) { + struct mg_str hn = hm->header_names[i]; + struct mg_str hv = hm->header_values[i]; + + /* we rewrite the host header */ + if (mg_vcasecmp(&hn, "Host") == 0) continue; + /* + * Don't pass chunked transfer encoding to the client because hm->body is + * already dechunked when we arrive here. + */ + if (mg_vcasecmp(&hn, "Transfer-encoding") == 0 && + mg_vcasecmp(&hv, "chunked") == 0) { + mg_printf(be, "Content-Length: %" SIZE_T_FMT "\r\n", hm->body.len); + continue; + } + /* We don't support proxying Expect: 100-continue. */ + if (mg_vcasecmp(&hn, "Expect") == 0 && + mg_vcasecmp(&hv, "100-continue") == 0) { + continue; + } + + mg_printf(be, "%.*s: %.*s\r\n", (int) hn.len, hn.p, (int) hv.len, hv.p); + } + + mg_send(be, "\r\n", 2); + mg_send(be, hm->body.p, hm->body.len); + +cleanup: + if (purl != burl) MG_FREE(purl); +} + +static int mg_http_handle_forwarding(struct mg_connection *nc, + struct http_message *hm, + const struct mg_serve_http_opts *opts) { + const char *rewrites = opts->url_rewrites; + struct mg_str a, b; + struct mg_str p1 = MG_MK_STR("http://"), p2 = MG_MK_STR("https://"); + + while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) { + if (mg_strncmp(a, hm->uri, a.len) == 0) { + if (mg_strncmp(b, p1, p1.len) == 0 || mg_strncmp(b, p2, p2.len) == 0) { + mg_http_reverse_proxy(nc, hm, a, b); + return 1; + } + } + } + + return 0; +} +#endif /* MG_ENABLE_FILESYSTEM */ + +MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm, + const struct mg_serve_http_opts *opts, + char **local_path, + struct mg_str *remainder) { + int ok = 1; + const char *cp = hm->uri.p, *cp_end = hm->uri.p + hm->uri.len; + struct mg_str root = {NULL, 0}; + const char *file_uri_start = cp; + *local_path = NULL; + remainder->p = NULL; + remainder->len = 0; + + { /* 1. Determine which root to use. */ + +#if MG_ENABLE_HTTP_URL_REWRITES + const char *rewrites = opts->url_rewrites; +#else + const char *rewrites = ""; +#endif + struct mg_str *hh = mg_get_http_header(hm, "Host"); + struct mg_str a, b; + /* Check rewrites first. */ + while ((rewrites = mg_next_comma_list_entry(rewrites, &a, &b)) != NULL) { + if (a.len > 1 && a.p[0] == '@') { + /* Host rewrite. */ + if (hh != NULL && hh->len == a.len - 1 && + mg_ncasecmp(a.p + 1, hh->p, a.len - 1) == 0) { + root = b; + break; + } + } else { + /* Regular rewrite, URI=directory */ + size_t match_len = mg_match_prefix_n(a, hm->uri); + if (match_len > 0) { + file_uri_start = hm->uri.p + match_len; + if (*file_uri_start == '/' || file_uri_start == cp_end) { + /* Match ended at component boundary, ok. */ + } else if (*(file_uri_start - 1) == '/') { + /* Pattern ends with '/', backtrack. */ + file_uri_start--; + } else { + /* No match: must fall on the component boundary. */ + continue; + } + root = b; + break; + } + } + } + /* If no rewrite rules matched, use DAV or regular document root. */ + if (root.p == NULL) { +#if MG_ENABLE_HTTP_WEBDAV + if (opts->dav_document_root != NULL && mg_is_dav_request(&hm->method)) { + root.p = opts->dav_document_root; + root.len = strlen(opts->dav_document_root); + } else +#endif + { + root.p = opts->document_root; + root.len = strlen(opts->document_root); + } + } + assert(root.p != NULL && root.len > 0); + } + + { /* 2. Find where in the canonical URI path the local path ends. */ + const char *u = file_uri_start + 1; + char *lp = (char *) MG_MALLOC(root.len + hm->uri.len + 1); + char *lp_end = lp + root.len + hm->uri.len + 1; + char *p = lp, *ps; + int exists = 1; + if (lp == NULL) { + ok = 0; + goto out; + } + memcpy(p, root.p, root.len); + p += root.len; + if (*(p - 1) == DIRSEP) p--; + *p = '\0'; + ps = p; + + /* Chop off URI path components one by one and build local path. */ + while (u <= cp_end) { + const char *next = u; + struct mg_str component; + if (exists) { + cs_stat_t st; + exists = (mg_stat(lp, &st) == 0); + if (exists && S_ISREG(st.st_mode)) { + /* We found the terminal, the rest of the URI (if any) is path_info. + */ + if (*(u - 1) == '/') u--; + break; + } + } + if (u >= cp_end) break; + parse_uri_component((const char **) &next, cp_end, "/", &component); + if (component.len > 0) { + int len; + memmove(p + 1, component.p, component.len); + len = mg_url_decode(p + 1, component.len, p + 1, lp_end - p - 1, 0); + if (len <= 0) { + ok = 0; + break; + } + component.p = p + 1; + component.len = len; + if (mg_vcmp(&component, ".") == 0) { + /* Yum. */ + } else if (mg_vcmp(&component, "..") == 0) { + while (p > ps && *p != DIRSEP) p--; + *p = '\0'; + } else { + size_t i; +#ifdef _WIN32 + /* On Windows, make sure it's valid Unicode (no funny stuff). */ + wchar_t buf[MG_MAX_PATH * 2]; + if (to_wchar(component.p, buf, MG_MAX_PATH) == 0) { + DBG(("[%.*s] smells funny", (int) component.len, component.p)); + ok = 0; + break; + } +#endif + *p++ = DIRSEP; + /* No NULs and DIRSEPs in the component (percent-encoded). */ + for (i = 0; i < component.len; i++, p++) { + if (*p == '\0' || *p == DIRSEP +#ifdef _WIN32 + /* On Windows, "/" is also accepted, so check for that too. */ + || + *p == '/' +#endif + ) { + ok = 0; + break; + } + } + } + } + u = next; + } + if (ok) { + *local_path = lp; + if (u > cp_end) u = cp_end; + remainder->p = u; + remainder->len = cp_end - u; + } else { + MG_FREE(lp); + } + } + +out: + LOG(LL_DEBUG, + ("'%.*s' -> '%s' + '%.*s'", (int) hm->uri.len, hm->uri.p, + *local_path ? *local_path : "", (int) remainder->len, remainder->p)); + return ok; +} + +static int mg_get_month_index(const char *s) { + static const char *month_names[] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", + "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"}; + size_t i; + + for (i = 0; i < ARRAY_SIZE(month_names); i++) + if (!strcmp(s, month_names[i])) return (int) i; + + return -1; +} + +static int mg_num_leap_years(int year) { + return year / 4 - year / 100 + year / 400; +} + +/* Parse UTC date-time string, and return the corresponding time_t value. */ +MG_INTERNAL time_t mg_parse_date_string(const char *datetime) { + static const unsigned short days_before_month[] = { + 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334}; + char month_str[32]; + int second, minute, hour, day, month, year, leap_days, days; + time_t result = (time_t) 0; + + if (((sscanf(datetime, "%d/%3s/%d %d:%d:%d", &day, month_str, &year, &hour, + &minute, &second) == 6) || + (sscanf(datetime, "%d %3s %d %d:%d:%d", &day, month_str, &year, &hour, + &minute, &second) == 6) || + (sscanf(datetime, "%*3s, %d %3s %d %d:%d:%d", &day, month_str, &year, + &hour, &minute, &second) == 6) || + (sscanf(datetime, "%d-%3s-%d %d:%d:%d", &day, month_str, &year, &hour, + &minute, &second) == 6)) && + year > 1970 && (month = mg_get_month_index(month_str)) != -1) { + leap_days = mg_num_leap_years(year) - mg_num_leap_years(1970); + year -= 1970; + days = year * 365 + days_before_month[month] + (day - 1) + leap_days; + result = days * 24 * 3600 + hour * 3600 + minute * 60 + second; + } + + return result; +} + +MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st) { + struct mg_str *hdr; + if ((hdr = mg_get_http_header(hm, "If-None-Match")) != NULL) { + char etag[64]; + mg_http_construct_etag(etag, sizeof(etag), st); + return mg_vcasecmp(hdr, etag) == 0; + } else if ((hdr = mg_get_http_header(hm, "If-Modified-Since")) != NULL) { + return st->st_mtime <= mg_parse_date_string(hdr->p); + } else { + return 0; + } +} + +void mg_http_send_digest_auth_request(struct mg_connection *c, + const char *domain) { + mg_printf(c, + "HTTP/1.1 401 Unauthorized\r\n" + "WWW-Authenticate: Digest qop=\"auth\", " + "realm=\"%s\", nonce=\"%lx\"\r\n" + "Content-Length: 0\r\n\r\n", + domain, (unsigned long) mg_time()); +} + +static void mg_http_send_options(struct mg_connection *nc) { + mg_printf(nc, "%s", + "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS" +#if MG_ENABLE_HTTP_WEBDAV + ", MKCOL, PUT, DELETE, PROPFIND, MOVE\r\nDAV: 1,2" +#endif + "\r\n\r\n"); + nc->flags |= MG_F_SEND_AND_CLOSE; +} + +static int mg_is_creation_request(const struct http_message *hm) { + return mg_vcmp(&hm->method, "MKCOL") == 0 || mg_vcmp(&hm->method, "PUT") == 0; +} + +MG_INTERNAL void mg_send_http_file(struct mg_connection *nc, char *path, + const struct mg_str *path_info, + struct http_message *hm, + struct mg_serve_http_opts *opts) { + int exists, is_directory, is_cgi; +#if MG_ENABLE_HTTP_WEBDAV + int is_dav = mg_is_dav_request(&hm->method); +#else + int is_dav = 0; +#endif + char *index_file = NULL; + cs_stat_t st; + + exists = (mg_stat(path, &st) == 0); + is_directory = exists && S_ISDIR(st.st_mode); + + if (is_directory) + mg_find_index_file(path, opts->index_files, &index_file, &st); + + is_cgi = + (mg_match_prefix(opts->cgi_file_pattern, strlen(opts->cgi_file_pattern), + index_file ? index_file : path) > 0); + + LOG(LL_DEBUG, + ("%p %.*s [%s] exists=%d is_dir=%d is_dav=%d is_cgi=%d index=%s", nc, + (int) hm->method.len, hm->method.p, path, exists, is_directory, is_dav, + is_cgi, index_file ? index_file : "")); + + if (is_directory && hm->uri.p[hm->uri.len - 1] != '/' && !is_dav) { + mg_printf(nc, + "HTTP/1.1 301 Moved\r\nLocation: %.*s/\r\n" + "Content-Length: 0\r\n\r\n", + (int) hm->uri.len, hm->uri.p); + MG_FREE(index_file); + return; + } + + /* If we have path_info, the only way to handle it is CGI. */ + if (path_info->len > 0 && !is_cgi) { + mg_http_send_error(nc, 501, NULL); + MG_FREE(index_file); + return; + } + + if (is_dav && opts->dav_document_root == NULL) { + mg_http_send_error(nc, 501, NULL); + } else if (!mg_http_is_authorized( + hm, mg_mk_str(path), opts->auth_domain, opts->global_auth_file, + ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) | + MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE | + MG_AUTH_FLAG_ALLOW_MISSING_FILE)) || + !mg_http_is_authorized( + hm, mg_mk_str(path), opts->auth_domain, + opts->per_directory_auth_file, + ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) | + MG_AUTH_FLAG_ALLOW_MISSING_FILE))) { + mg_http_send_digest_auth_request(nc, opts->auth_domain); + } else if (is_cgi) { +#if MG_ENABLE_HTTP_CGI + mg_handle_cgi(nc, index_file ? index_file : path, path_info, hm, opts); +#else + mg_http_send_error(nc, 501, NULL); +#endif /* MG_ENABLE_HTTP_CGI */ + } else if ((!exists || + mg_is_file_hidden(path, opts, 0 /* specials are ok */)) && + !mg_is_creation_request(hm)) { + mg_http_send_error(nc, 404, NULL); +#if MG_ENABLE_HTTP_WEBDAV + } else if (!mg_vcmp(&hm->method, "PROPFIND")) { + mg_handle_propfind(nc, path, &st, hm, opts); +#if !MG_DISABLE_DAV_AUTH + } else if (is_dav && + (opts->dav_auth_file == NULL || + (strcmp(opts->dav_auth_file, "-") != 0 && + !mg_http_is_authorized( + hm, mg_mk_str(path), opts->auth_domain, opts->dav_auth_file, + ((is_directory ? MG_AUTH_FLAG_IS_DIRECTORY : 0) | + MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE | + MG_AUTH_FLAG_ALLOW_MISSING_FILE))))) { + mg_http_send_digest_auth_request(nc, opts->auth_domain); +#endif + } else if (!mg_vcmp(&hm->method, "MKCOL")) { + mg_handle_mkcol(nc, path, hm); + } else if (!mg_vcmp(&hm->method, "DELETE")) { + mg_handle_delete(nc, opts, path); + } else if (!mg_vcmp(&hm->method, "PUT")) { + mg_handle_put(nc, path, hm); + } else if (!mg_vcmp(&hm->method, "MOVE")) { + mg_handle_move(nc, opts, path, hm); +#if MG_ENABLE_FAKE_DAVLOCK + } else if (!mg_vcmp(&hm->method, "LOCK")) { + mg_handle_lock(nc, path); +#endif +#endif /* MG_ENABLE_HTTP_WEBDAV */ + } else if (!mg_vcmp(&hm->method, "OPTIONS")) { + mg_http_send_options(nc); + } else if (is_directory && index_file == NULL) { +#if MG_ENABLE_DIRECTORY_LISTING + if (strcmp(opts->enable_directory_listing, "yes") == 0) { + mg_send_directory_listing(nc, path, hm, opts); + } else { + mg_http_send_error(nc, 403, NULL); + } +#else + mg_http_send_error(nc, 501, NULL); +#endif + } else if (mg_is_not_modified(hm, &st)) { + mg_http_send_error(nc, 304, "Not Modified"); + } else { + mg_http_serve_file2(nc, index_file ? index_file : path, hm, opts); + } + MG_FREE(index_file); +} + +void mg_serve_http(struct mg_connection *nc, struct http_message *hm, + struct mg_serve_http_opts opts) { + char *path = NULL; + struct mg_str *hdr, path_info; + uint32_t remote_ip = ntohl(*(uint32_t *) &nc->sa.sin.sin_addr); + + if (mg_check_ip_acl(opts.ip_acl, remote_ip) != 1) { + /* Not allowed to connect */ + mg_http_send_error(nc, 403, NULL); + nc->flags |= MG_F_SEND_AND_CLOSE; + return; + } + +#if MG_ENABLE_HTTP_URL_REWRITES + if (mg_http_handle_forwarding(nc, hm, &opts)) { + return; + } + + if (mg_http_send_port_based_redirect(nc, hm, &opts)) { + return; + } +#endif + + if (opts.document_root == NULL) { + opts.document_root = "."; + } + if (opts.per_directory_auth_file == NULL) { + opts.per_directory_auth_file = ".htpasswd"; + } + if (opts.enable_directory_listing == NULL) { + opts.enable_directory_listing = "yes"; + } + if (opts.cgi_file_pattern == NULL) { + opts.cgi_file_pattern = "**.cgi$|**.php$"; + } + if (opts.ssi_pattern == NULL) { + opts.ssi_pattern = "**.shtml$|**.shtm$"; + } + if (opts.index_files == NULL) { + opts.index_files = "index.html,index.htm,index.shtml,index.cgi,index.php"; + } + /* Normalize path - resolve "." and ".." (in-place). */ + if (!mg_normalize_uri_path(&hm->uri, &hm->uri)) { + mg_http_send_error(nc, 400, NULL); + return; + } + if (mg_uri_to_local_path(hm, &opts, &path, &path_info) == 0) { + mg_http_send_error(nc, 404, NULL); + return; + } + mg_send_http_file(nc, path, &path_info, hm, &opts); + + MG_FREE(path); + path = NULL; + + /* Close connection for non-keep-alive requests */ + if (mg_vcmp(&hm->proto, "HTTP/1.1") != 0 || + ((hdr = mg_get_http_header(hm, "Connection")) != NULL && + mg_vcmp(hdr, "keep-alive") != 0)) { +#if 0 + nc->flags |= MG_F_SEND_AND_CLOSE; +#endif + } +} + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data, + mg_fu_fname_fn local_name_fn + MG_UD_ARG(void *user_data)) { + switch (ev) { + case MG_EV_HTTP_PART_BEGIN: { + struct mg_http_multipart_part *mp = + (struct mg_http_multipart_part *) ev_data; + struct file_upload_state *fus = + (struct file_upload_state *) MG_CALLOC(1, sizeof(*fus)); + struct mg_str lfn = local_name_fn(nc, mg_mk_str(mp->file_name)); + mp->user_data = NULL; + if (lfn.p == NULL || lfn.len == 0) { + LOG(LL_ERROR, ("%p Not allowed to upload %s", nc, mp->file_name)); + mg_printf(nc, + "HTTP/1.1 403 Not Allowed\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n" + "Not allowed to upload %s\r\n", + mp->file_name); + nc->flags |= MG_F_SEND_AND_CLOSE; + return; + } + fus->lfn = (char *) MG_MALLOC(lfn.len + 1); + memcpy(fus->lfn, lfn.p, lfn.len); + fus->lfn[lfn.len] = '\0'; + if (lfn.p != mp->file_name) MG_FREE((char *) lfn.p); + LOG(LL_DEBUG, + ("%p Receiving file %s -> %s", nc, mp->file_name, fus->lfn)); + fus->fp = mg_fopen(fus->lfn, "w"); + if (fus->fp == NULL) { + mg_printf(nc, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + LOG(LL_ERROR, ("Failed to open %s: %d\n", fus->lfn, mg_get_errno())); + mg_printf(nc, "Failed to open %s: %d\n", fus->lfn, mg_get_errno()); + /* Do not close the connection just yet, discard remainder of the data. + * This is because at the time of writing some browsers (Chrome) fail to + * render response before all the data is sent. */ + } + mp->user_data = (void *) fus; + break; + } + case MG_EV_HTTP_PART_DATA: { + struct mg_http_multipart_part *mp = + (struct mg_http_multipart_part *) ev_data; + struct file_upload_state *fus = + (struct file_upload_state *) mp->user_data; + if (fus == NULL || fus->fp == NULL) break; + if (mg_fwrite(mp->data.p, 1, mp->data.len, fus->fp) != mp->data.len) { + LOG(LL_ERROR, ("Failed to write to %s: %d, wrote %d", fus->lfn, + mg_get_errno(), (int) fus->num_recd)); + if (mg_get_errno() == ENOSPC +#ifdef SPIFFS_ERR_FULL + || mg_get_errno() == SPIFFS_ERR_FULL +#endif + ) { + mg_printf(nc, + "HTTP/1.1 413 Payload Too Large\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + mg_printf(nc, "Failed to write to %s: no space left; wrote %d\r\n", + fus->lfn, (int) fus->num_recd); + } else { + mg_printf(nc, + "HTTP/1.1 500 Internal Server Error\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n"); + mg_printf(nc, "Failed to write to %s: %d, wrote %d", mp->file_name, + mg_get_errno(), (int) fus->num_recd); + } + fclose(fus->fp); + remove(fus->lfn); + fus->fp = NULL; + /* Do not close the connection just yet, discard remainder of the data. + * This is because at the time of writing some browsers (Chrome) fail to + * render response before all the data is sent. */ + return; + } + fus->num_recd += mp->data.len; + LOG(LL_DEBUG, ("%p rec'd %d bytes, %d total", nc, (int) mp->data.len, + (int) fus->num_recd)); + break; + } + case MG_EV_HTTP_PART_END: { + struct mg_http_multipart_part *mp = + (struct mg_http_multipart_part *) ev_data; + struct file_upload_state *fus = + (struct file_upload_state *) mp->user_data; + if (fus == NULL) break; + if (mp->status >= 0 && fus->fp != NULL) { + LOG(LL_DEBUG, ("%p Uploaded %s (%s), %d bytes", nc, mp->file_name, + fus->lfn, (int) fus->num_recd)); + mg_printf(nc, + "HTTP/1.1 200 OK\r\n" + "Content-Type: text/plain\r\n" + "Connection: close\r\n\r\n" + "Ok, %s - %d bytes.\r\n", + mp->file_name, (int) fus->num_recd); + } else { + LOG(LL_ERROR, ("Failed to store %s (%s)", mp->file_name, fus->lfn)); + /* + * mp->status < 0 means connection was terminated, so no reason to send + * HTTP reply + */ + } + if (fus->fp != NULL) fclose(fus->fp); + MG_FREE(fus->lfn); + MG_FREE(fus); + mp->user_data = NULL; + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + } + } + +#if MG_ENABLE_CALLBACK_USERDATA + (void) user_data; +#endif +} + +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ +#endif /* MG_ENABLE_FILESYSTEM */ + +struct mg_connection *mg_connect_http_base( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *scheme1, const char *scheme2, + const char *scheme_ssl1, const char *scheme_ssl2, const char *url, + struct mg_str *path, struct mg_str *user_info, struct mg_str *host) { + struct mg_connection *nc = NULL; + unsigned int port_i = 0; + int use_ssl = 0; + struct mg_str scheme, query, fragment; + char conn_addr_buf[2]; + char *conn_addr = conn_addr_buf; + + if (mg_parse_uri(mg_mk_str(url), &scheme, user_info, host, &port_i, path, + &query, &fragment) != 0) { + MG_SET_PTRPTR(opts.error_string, "cannot parse url"); + goto out; + } + + /* If query is present, do not strip it. Pass to the caller. */ + if (query.len > 0) path->len += query.len + 1; + + if (scheme.len == 0 || mg_vcmp(&scheme, scheme1) == 0 || + (scheme2 != NULL && mg_vcmp(&scheme, scheme2) == 0)) { + use_ssl = 0; + if (port_i == 0) port_i = 80; + } else if (mg_vcmp(&scheme, scheme_ssl1) == 0 || + (scheme2 != NULL && mg_vcmp(&scheme, scheme_ssl2) == 0)) { + use_ssl = 1; + if (port_i == 0) port_i = 443; + } else { + goto out; + } + + mg_asprintf(&conn_addr, sizeof(conn_addr_buf), "tcp://%.*s:%u", + (int) host->len, host->p, port_i); + if (conn_addr == NULL) goto out; + + LOG(LL_DEBUG, ("%s use_ssl? %d %s", url, use_ssl, conn_addr)); + if (use_ssl) { +#if MG_ENABLE_SSL + /* + * Schema requires SSL, but no SSL parameters were provided in opts. + * In order to maintain backward compatibility, use a faux-SSL with no + * verification. + */ + if (opts.ssl_ca_cert == NULL) { + opts.ssl_ca_cert = "*"; + } +#else + MG_SET_PTRPTR(opts.error_string, "ssl is disabled"); + goto out; +#endif + } + + if ((nc = mg_connect_opt(mgr, conn_addr, MG_CB(ev_handler, user_data), + opts)) != NULL) { + mg_set_protocol_http_websocket(nc); + } + +out: + if (conn_addr != NULL && conn_addr != conn_addr_buf) MG_FREE(conn_addr); + return nc; +} + +struct mg_connection *mg_connect_http_opt( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *extra_headers, + const char *post_data) { + struct mg_str user = MG_NULL_STR, null_str = MG_NULL_STR; + struct mg_str host = MG_NULL_STR, path = MG_NULL_STR; + struct mbuf auth; + struct mg_connection *nc = + mg_connect_http_base(mgr, MG_CB(ev_handler, user_data), opts, "http", + NULL, "https", NULL, url, &path, &user, &host); + + if (nc == NULL) { + return NULL; + } + + mbuf_init(&auth, 0); + if (user.len > 0) { + mg_basic_auth_header(user, null_str, &auth); + } + + if (post_data == NULL) post_data = ""; + if (extra_headers == NULL) extra_headers = ""; + if (path.len == 0) path = mg_mk_str("/"); + if (host.len == 0) host = mg_mk_str(""); + + mg_printf(nc, "%s %.*s HTTP/1.1\r\nHost: %.*s\r\nContent-Length: %" SIZE_T_FMT + "\r\n%.*s%s\r\n%s", + (post_data[0] == '\0' ? "GET" : "POST"), (int) path.len, path.p, + (int) (path.p - host.p), host.p, strlen(post_data), (int) auth.len, + (auth.buf == NULL ? "" : auth.buf), extra_headers, post_data); + + mbuf_free(&auth); + return nc; +} + +struct mg_connection *mg_connect_http( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + const char *url, const char *extra_headers, const char *post_data) { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_connect_http_opt(mgr, MG_CB(ev_handler, user_data), opts, url, + extra_headers, post_data); +} + +size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, + size_t var_name_len, char *file_name, + size_t file_name_len, const char **data, + size_t *data_len) { + static const char cd[] = "Content-Disposition: "; + size_t hl, bl, n, ll, pos, cdl = sizeof(cd) - 1; + int shl; + + if (buf == NULL || buf_len <= 0) return 0; + if ((shl = mg_http_get_request_len(buf, buf_len)) <= 0) return 0; + hl = shl; + if (buf[0] != '-' || buf[1] != '-' || buf[2] == '\n') return 0; + + /* Get boundary length */ + bl = mg_get_line_len(buf, buf_len); + + /* Loop through headers, fetch variable name and file name */ + var_name[0] = file_name[0] = '\0'; + for (n = bl; (ll = mg_get_line_len(buf + n, hl - n)) > 0; n += ll) { + if (mg_ncasecmp(cd, buf + n, cdl) == 0) { + struct mg_str header; + header.p = buf + n + cdl; + header.len = ll - (cdl + 2); + mg_http_parse_header(&header, "name", var_name, var_name_len); + mg_http_parse_header(&header, "filename", file_name, file_name_len); + } + } + + /* Scan through the body, search for terminating boundary */ + for (pos = hl; pos + (bl - 2) < buf_len; pos++) { + if (buf[pos] == '-' && !strncmp(buf, &buf[pos], bl - 2)) { + if (data_len != NULL) *data_len = (pos - 2) - hl; + if (data != NULL) *data = buf + hl; + return pos; + } + } + + return 0; +} + +void mg_register_http_endpoint_opt(struct mg_connection *nc, + const char *uri_path, + mg_event_handler_t handler, + struct mg_http_endpoint_opts opts) { + struct mg_http_proto_data *pd = NULL; + struct mg_http_endpoint *new_ep = NULL; + + if (nc == NULL) return; + new_ep = (struct mg_http_endpoint *) MG_CALLOC(1, sizeof(*new_ep)); + if (new_ep == NULL) return; + + pd = mg_http_get_proto_data(nc); + new_ep->uri_pattern = mg_strdup(mg_mk_str(uri_path)); + if (opts.auth_domain != NULL && opts.auth_file != NULL) { + new_ep->auth_domain = strdup(opts.auth_domain); + new_ep->auth_file = strdup(opts.auth_file); + } + new_ep->handler = handler; +#if MG_ENABLE_CALLBACK_USERDATA + new_ep->user_data = opts.user_data; +#endif + new_ep->next = pd->endpoints; + pd->endpoints = new_ep; +} + +static void mg_http_call_endpoint_handler(struct mg_connection *nc, int ev, + struct http_message *hm) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + void *user_data = nc->user_data; + + if (ev == MG_EV_HTTP_REQUEST +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + || ev == MG_EV_HTTP_MULTIPART_REQUEST +#endif + ) { + struct mg_http_endpoint *ep = + mg_http_get_endpoint_handler(nc->listener, &hm->uri); + if (ep != NULL) { +#if MG_ENABLE_FILESYSTEM && !MG_DISABLE_HTTP_DIGEST_AUTH + if (!mg_http_is_authorized(hm, hm->uri, ep->auth_domain, ep->auth_file, + MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE)) { + mg_http_send_digest_auth_request(nc, ep->auth_domain); + return; + } +#endif + pd->endpoint_handler = ep->handler; +#if MG_ENABLE_CALLBACK_USERDATA + user_data = ep->user_data; +#endif + } + } + mg_call(nc, pd->endpoint_handler ? pd->endpoint_handler : nc->handler, + user_data, ev, hm); +} + +void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path, + MG_CB(mg_event_handler_t handler, + void *user_data)) { + struct mg_http_endpoint_opts opts; + memset(&opts, 0, sizeof(opts)); +#if MG_ENABLE_CALLBACK_USERDATA + opts.user_data = user_data; +#endif + mg_register_http_endpoint_opt(nc, uri_path, handler, opts); +} + +#endif /* MG_ENABLE_HTTP */ diff --git a/src/http.h b/src/http.h new file mode 100644 index 00000000..7c2c7e99 --- /dev/null +++ b/src/http.h @@ -0,0 +1,364 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === Common API reference + */ + +#ifndef CS_MONGOOSE_SRC_HTTP_H_ +#define CS_MONGOOSE_SRC_HTTP_H_ + +#if MG_ENABLE_HTTP + +#include "mongoose/src/net.h" +#include "common/mg_str.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef MG_MAX_HTTP_HEADERS +#define MG_MAX_HTTP_HEADERS 20 +#endif + +#ifndef MG_MAX_HTTP_REQUEST_SIZE +#define MG_MAX_HTTP_REQUEST_SIZE 1024 +#endif + +#ifndef MG_MAX_HTTP_SEND_MBUF +#define MG_MAX_HTTP_SEND_MBUF 1024 +#endif + +#ifndef MG_CGI_ENVIRONMENT_SIZE +#define MG_CGI_ENVIRONMENT_SIZE 8192 +#endif + +/* HTTP message */ +struct http_message { + struct mg_str message; /* Whole message: request line + headers + body */ + struct mg_str body; /* Message body. 0-length for requests with no body */ + + /* HTTP Request line (or HTTP response line) */ + struct mg_str method; /* "GET" */ + struct mg_str uri; /* "/my_file.html" */ + struct mg_str proto; /* "HTTP/1.1" -- for both request and response */ + + /* For responses, code and response status message are set */ + int resp_code; + struct mg_str resp_status_msg; + + /* + * Query-string part of the URI. For example, for HTTP request + * GET /foo/bar?param1=val1¶m2=val2 + * | uri | query_string | + * + * Note that question mark character doesn't belong neither to the uri, + * nor to the query_string + */ + struct mg_str query_string; + + /* Headers */ + struct mg_str header_names[MG_MAX_HTTP_HEADERS]; + struct mg_str header_values[MG_MAX_HTTP_HEADERS]; +}; + +#if MG_ENABLE_HTTP_WEBSOCKET +/* WebSocket message */ +struct websocket_message { + unsigned char *data; + size_t size; + unsigned char flags; +}; +#endif + +/* HTTP multipart part */ +struct mg_http_multipart_part { + const char *file_name; + const char *var_name; + struct mg_str data; + int status; /* <0 on error */ + void *user_data; +}; + +/* SSI call context */ +struct mg_ssi_call_ctx { + struct http_message *req; /* The request being processed. */ + struct mg_str file; /* Filesystem path of the file being processed. */ + struct mg_str arg; /* The argument passed to the tag: . */ +}; + +/* HTTP and websocket events. void *ev_data is described in a comment. */ +#define MG_EV_HTTP_REQUEST 100 /* struct http_message * */ +#define MG_EV_HTTP_REPLY 101 /* struct http_message * */ +#define MG_EV_HTTP_CHUNK 102 /* struct http_message * */ +#define MG_EV_SSI_CALL 105 /* char * */ +#define MG_EV_SSI_CALL_CTX 106 /* struct mg_ssi_call_ctx * */ + +#if MG_ENABLE_HTTP_WEBSOCKET +#define MG_EV_WEBSOCKET_HANDSHAKE_REQUEST 111 /* struct http_message * */ +#define MG_EV_WEBSOCKET_HANDSHAKE_DONE 112 /* NULL */ +#define MG_EV_WEBSOCKET_FRAME 113 /* struct websocket_message * */ +#define MG_EV_WEBSOCKET_CONTROL_FRAME 114 /* struct websocket_message * */ +#endif + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +#define MG_EV_HTTP_MULTIPART_REQUEST 121 /* struct http_message */ +#define MG_EV_HTTP_PART_BEGIN 122 /* struct mg_http_multipart_part */ +#define MG_EV_HTTP_PART_DATA 123 /* struct mg_http_multipart_part */ +#define MG_EV_HTTP_PART_END 124 /* struct mg_http_multipart_part */ +/* struct mg_http_multipart_part */ +#define MG_EV_HTTP_MULTIPART_REQUEST_END 125 +#endif + +/* + * Attaches a built-in HTTP event handler to the given connection. + * The user-defined event handler will receive following extra events: + * + * - MG_EV_HTTP_REQUEST: HTTP request has arrived. Parsed HTTP request + * is passed as + * `struct http_message` through the handler's `void *ev_data` pointer. + * - MG_EV_HTTP_REPLY: The HTTP reply has arrived. The parsed HTTP reply is + * passed as `struct http_message` through the handler's `void *ev_data` + * pointer. + * - MG_EV_HTTP_CHUNK: The HTTP chunked-encoding chunk has arrived. + * The parsed HTTP reply is passed as `struct http_message` through the + * handler's `void *ev_data` pointer. `http_message::body` would contain + * incomplete, reassembled HTTP body. + * It will grow with every new chunk that arrives, and it can + * potentially consume a lot of memory. An event handler may process + * the body as chunks are coming, and signal Mongoose to delete processed + * body by setting `MG_F_DELETE_CHUNK` in `mg_connection::flags`. When + * the last zero chunk is received, + * Mongoose sends `MG_EV_HTTP_REPLY` event with + * full reassembled body (if handler did not signal to delete chunks) or + * with empty body (if handler did signal to delete chunks). + * - MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: server has received the WebSocket + * handshake request. `ev_data` contains parsed HTTP request. + * - MG_EV_WEBSOCKET_HANDSHAKE_DONE: server has completed the WebSocket + * handshake. `ev_data` is `NULL`. + * - MG_EV_WEBSOCKET_FRAME: new WebSocket frame has arrived. `ev_data` is + * `struct websocket_message *` + * + * When compiled with MG_ENABLE_HTTP_STREAMING_MULTIPART, Mongoose parses + * multipart requests and splits them into separate events: + * - MG_EV_HTTP_MULTIPART_REQUEST: Start of the request. + * This event is sent before body is parsed. After this, the user + * should expect a sequence of PART_BEGIN/DATA/END requests. + * This is also the last time when headers and other request fields are + * accessible. + * - MG_EV_HTTP_PART_BEGIN: Start of a part of a multipart message. + * Argument: mg_http_multipart_part with var_name and file_name set + * (if present). No data is passed in this message. + * - MG_EV_HTTP_PART_DATA: new portion of data from the multipart message. + * Argument: mg_http_multipart_part. var_name and file_name are preserved, + * data is available in mg_http_multipart_part.data. + * - MG_EV_HTTP_PART_END: End of the current part. var_name, file_name are + * the same, no data in the message. If status is 0, then the part is + * properly terminated with a boundary, status < 0 means that connection + * was terminated. + * - MG_EV_HTTP_MULTIPART_REQUEST_END: End of the multipart request. + * Argument: mg_http_multipart_part, var_name and file_name are NULL, + * status = 0 means request was properly closed, < 0 means connection + * was terminated (note: in this case both PART_END and REQUEST_END are + * delivered). + */ +void mg_set_protocol_http_websocket(struct mg_connection *nc); + +#if MG_ENABLE_HTTP_WEBSOCKET +/* + * Send websocket handshake to the server. + * + * `nc` must be a valid connection, connected to a server. `uri` is an URI + * to fetch, extra_headers` is extra HTTP headers to send or `NULL`. + * + * This function is intended to be used by websocket client. + * + * Note that the Host header is mandatory in HTTP/1.1 and must be + * included in `extra_headers`. `mg_send_websocket_handshake2` offers + * a better API for that. + * + * Deprecated in favour of `mg_send_websocket_handshake2` + */ +void mg_send_websocket_handshake(struct mg_connection *nc, const char *uri, + const char *extra_headers); + +/* + * Send websocket handshake to the server. + * + * `nc` must be a valid connection, connected to a server. `uri` is an URI + * to fetch, `host` goes into the `Host` header, `protocol` goes into the + * `Sec-WebSocket-Proto` header (NULL to omit), extra_headers` is extra HTTP + * headers to send or `NULL`. + * + * This function is intended to be used by websocket client. + */ +void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path, + const char *host, const char *protocol, + const char *extra_headers); + +/* Like mg_send_websocket_handshake2 but also passes basic auth header */ +void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path, + const char *host, const char *protocol, + const char *extra_headers, const char *user, + const char *pass); + +/* Same as mg_send_websocket_handshake3 but with strings not necessarily + * NUL-temrinated */ +void mg_send_websocket_handshake3v(struct mg_connection *nc, + const struct mg_str path, + const struct mg_str host, + const struct mg_str protocol, + const struct mg_str extra_headers, + const struct mg_str user, + const struct mg_str pass); + +/* + * Helper function that creates an outbound WebSocket connection. + * + * `url` is a URL to connect to. It must be properly URL-encoded, e.g. have + * no spaces, etc. By default, `mg_connect_ws()` sends Connection and + * Host headers. `extra_headers` is an extra HTTP header to send, e.g. + * `"User-Agent: my-app\r\n"`. + * If `protocol` is not NULL, then a `Sec-WebSocket-Protocol` header is sent. + * + * Examples: + * + * ```c + * nc1 = mg_connect_ws(mgr, ev_handler_1, "ws://echo.websocket.org", NULL, + * NULL); + * nc2 = mg_connect_ws(mgr, ev_handler_1, "wss://echo.websocket.org", NULL, + * NULL); + * nc3 = mg_connect_ws(mgr, ev_handler_1, "ws://api.cesanta.com", + * "clubby.cesanta.com", NULL); + * ``` + */ +struct mg_connection *mg_connect_ws(struct mg_mgr *mgr, + MG_CB(mg_event_handler_t event_handler, + void *user_data), + const char *url, const char *protocol, + const char *extra_headers); + +/* + * Helper function that creates an outbound WebSocket connection + * + * Mostly identical to `mg_connect_ws`, but allows to provide extra parameters + * (for example, SSL parameters) + */ +struct mg_connection *mg_connect_ws_opt( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *protocol, + const char *extra_headers); + +/* + * Send WebSocket frame to the remote end. + * + * `op_and_flags` specifies the frame's type. It's one of: + * + * - WEBSOCKET_OP_CONTINUE + * - WEBSOCKET_OP_TEXT + * - WEBSOCKET_OP_BINARY + * - WEBSOCKET_OP_CLOSE + * - WEBSOCKET_OP_PING + * - WEBSOCKET_OP_PONG + * + * Orred with one of the flags: + * + * - WEBSOCKET_DONT_FIN: Don't set the FIN flag on the frame to be sent. + * + * `data` and `data_len` contain frame data. + */ +void mg_send_websocket_frame(struct mg_connection *nc, int op_and_flags, + const void *data, size_t data_len); + +/* + * Like `mg_send_websocket_frame()`, but composes a single frame from multiple + * buffers. + */ +void mg_send_websocket_framev(struct mg_connection *nc, int op_and_flags, + const struct mg_str *strings, int num_strings); + +/* + * Sends WebSocket frame to the remote end. + * + * Like `mg_send_websocket_frame()`, but allows to create formatted messages + * with `printf()`-like semantics. + */ +void mg_printf_websocket_frame(struct mg_connection *nc, int op_and_flags, + const char *fmt, ...); + +/* Websocket opcodes, from http://tools.ietf.org/html/rfc6455 */ +#define WEBSOCKET_OP_CONTINUE 0 +#define WEBSOCKET_OP_TEXT 1 +#define WEBSOCKET_OP_BINARY 2 +#define WEBSOCKET_OP_CLOSE 8 +#define WEBSOCKET_OP_PING 9 +#define WEBSOCKET_OP_PONG 10 + +/* + * If set causes the FIN flag to not be set on outbound + * frames. This enables sending multiple fragments of a single + * logical message. + * + * The WebSocket protocol mandates that if the FIN flag of a data + * frame is not set, the next frame must be a WEBSOCKET_OP_CONTINUE. + * The last frame must have the FIN bit set. + * + * Note that mongoose will automatically defragment incoming messages, + * so this flag is used only on outbound messages. + */ +#define WEBSOCKET_DONT_FIN 0x100 + +#endif /* MG_ENABLE_HTTP_WEBSOCKET */ + +/* + * Decodes a URL-encoded string. + * + * Source string is specified by (`src`, `src_len`), and destination is + * (`dst`, `dst_len`). If `is_form_url_encoded` is non-zero, then + * `+` character is decoded as a blank space character. This function + * guarantees to NUL-terminate the destination. If destination is too small, + * then the source string is partially decoded and `-1` is returned. + *Otherwise, + * a length of the decoded string is returned, not counting final NUL. + */ +int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, + int is_form_url_encoded); + +extern void mg_hash_md5_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest); +extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest); + +/* + * Flags for `mg_http_is_authorized()`. + */ +#define MG_AUTH_FLAG_IS_DIRECTORY (1 << 0) +#define MG_AUTH_FLAG_IS_GLOBAL_PASS_FILE (1 << 1) +#define MG_AUTH_FLAG_ALLOW_MISSING_FILE (1 << 2) + +/* + * Checks whether an http request is authorized. `domain` is the authentication + * realm, `passwords_file` is a htdigest file (can be created e.g. with + * `htdigest` utility). If either `domain` or `passwords_file` is NULL, this + * function always returns 1; otherwise checks the authentication in the + * http request and returns 1 only if there is a match; 0 otherwise. + */ +int mg_http_is_authorized(struct http_message *hm, struct mg_str path, + const char *domain, const char *passwords_file, + int flags); + +/* + * Sends 401 Unauthorized response. + */ +void mg_http_send_digest_auth_request(struct mg_connection *c, + const char *domain); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_HTTP */ + +#endif /* CS_MONGOOSE_SRC_HTTP_H_ */ diff --git a/src/http_cgi.c b/src/http_cgi.c new file mode 100644 index 00000000..437c93c1 --- /dev/null +++ b/src/http_cgi.c @@ -0,0 +1,514 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef _WIN32 +#include +#endif + +#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI + +#ifndef MG_MAX_CGI_ENVIR_VARS +#define MG_MAX_CGI_ENVIR_VARS 64 +#endif + +#ifndef MG_ENV_EXPORT_TO_CGI +#define MG_ENV_EXPORT_TO_CGI "MONGOOSE_CGI" +#endif + +#define MG_F_HTTP_CGI_PARSE_HEADERS MG_F_USER_1 + +/* + * This structure helps to create an environment for the spawned CGI program. + * Environment is an array of "VARIABLE=VALUE\0" ASCIIZ strings, + * last element must be NULL. + * However, on Windows there is a requirement that all these VARIABLE=VALUE\0 + * strings must reside in a contiguous buffer. The end of the buffer is + * marked by two '\0' characters. + * We satisfy both worlds: we create an envp array (which is vars), all + * entries are actually pointers inside buf. + */ +struct mg_cgi_env_block { + struct mg_connection *nc; + char buf[MG_CGI_ENVIRONMENT_SIZE]; /* Environment buffer */ + const char *vars[MG_MAX_CGI_ENVIR_VARS]; /* char *envp[] */ + int len; /* Space taken */ + int nvars; /* Number of variables in envp[] */ +}; + +#ifdef _WIN32 +struct mg_threadparam { + sock_t s; + HANDLE hPipe; +}; + +static int mg_wait_until_ready(sock_t sock, int for_read) { + fd_set set; + FD_ZERO(&set); + FD_SET(sock, &set); + return select(sock + 1, for_read ? &set : 0, for_read ? 0 : &set, 0, 0) == 1; +} + +static void *mg_push_to_stdin(void *arg) { + struct mg_threadparam *tp = (struct mg_threadparam *) arg; + int n, sent, stop = 0; + DWORD k; + char buf[BUFSIZ]; + + while (!stop && mg_wait_until_ready(tp->s, 1) && + (n = recv(tp->s, buf, sizeof(buf), 0)) > 0) { + if (n == -1 && GetLastError() == WSAEWOULDBLOCK) continue; + for (sent = 0; !stop && sent < n; sent += k) { + if (!WriteFile(tp->hPipe, buf + sent, n - sent, &k, 0)) stop = 1; + } + } + DBG(("%s", "FORWARED EVERYTHING TO CGI")); + CloseHandle(tp->hPipe); + MG_FREE(tp); + return NULL; +} + +static void *mg_pull_from_stdout(void *arg) { + struct mg_threadparam *tp = (struct mg_threadparam *) arg; + int k = 0, stop = 0; + DWORD n, sent; + char buf[BUFSIZ]; + + while (!stop && ReadFile(tp->hPipe, buf, sizeof(buf), &n, NULL)) { + for (sent = 0; !stop && sent < n; sent += k) { + if (mg_wait_until_ready(tp->s, 0) && + (k = send(tp->s, buf + sent, n - sent, 0)) <= 0) + stop = 1; + } + } + DBG(("%s", "EOF FROM CGI")); + CloseHandle(tp->hPipe); + shutdown(tp->s, 2); // Without this, IO thread may get truncated data + closesocket(tp->s); + MG_FREE(tp); + return NULL; +} + +static void mg_spawn_stdio_thread(sock_t sock, HANDLE hPipe, + void *(*func)(void *)) { + struct mg_threadparam *tp = (struct mg_threadparam *) MG_MALLOC(sizeof(*tp)); + if (tp != NULL) { + tp->s = sock; + tp->hPipe = hPipe; + mg_start_thread(func, tp); + } +} + +static void mg_abs_path(const char *utf8_path, char *abs_path, size_t len) { + wchar_t buf[MG_MAX_PATH], buf2[MG_MAX_PATH]; + to_wchar(utf8_path, buf, ARRAY_SIZE(buf)); + GetFullPathNameW(buf, ARRAY_SIZE(buf2), buf2, NULL); + WideCharToMultiByte(CP_UTF8, 0, buf2, wcslen(buf2) + 1, abs_path, len, 0, 0); +} + +static int mg_start_process(const char *interp, const char *cmd, + const char *env, const char *envp[], + const char *dir, sock_t sock) { + STARTUPINFOW si; + PROCESS_INFORMATION pi; + HANDLE a[2], b[2], me = GetCurrentProcess(); + wchar_t wcmd[MG_MAX_PATH], full_dir[MG_MAX_PATH]; + char buf[MG_MAX_PATH], buf2[MG_MAX_PATH], buf5[MG_MAX_PATH], + buf4[MG_MAX_PATH], cmdline[MG_MAX_PATH]; + DWORD flags = DUPLICATE_CLOSE_SOURCE | DUPLICATE_SAME_ACCESS; + FILE *fp; + + memset(&si, 0, sizeof(si)); + memset(&pi, 0, sizeof(pi)); + + si.cb = sizeof(si); + si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW; + si.wShowWindow = SW_HIDE; + si.hStdError = GetStdHandle(STD_ERROR_HANDLE); + + CreatePipe(&a[0], &a[1], NULL, 0); + CreatePipe(&b[0], &b[1], NULL, 0); + DuplicateHandle(me, a[0], me, &si.hStdInput, 0, TRUE, flags); + DuplicateHandle(me, b[1], me, &si.hStdOutput, 0, TRUE, flags); + + if (interp == NULL && (fp = mg_fopen(cmd, "r")) != NULL) { + buf[0] = buf[1] = '\0'; + fgets(buf, sizeof(buf), fp); + buf[sizeof(buf) - 1] = '\0'; + if (buf[0] == '#' && buf[1] == '!') { + interp = buf + 2; + /* Trim leading spaces: https://github.com/cesanta/mongoose/issues/489 */ + while (*interp != '\0' && isspace(*(unsigned char *) interp)) { + interp++; + } + } + fclose(fp); + } + + snprintf(buf, sizeof(buf), "%s/%s", dir, cmd); + mg_abs_path(buf, buf2, ARRAY_SIZE(buf2)); + + mg_abs_path(dir, buf5, ARRAY_SIZE(buf5)); + to_wchar(dir, full_dir, ARRAY_SIZE(full_dir)); + + if (interp != NULL) { + mg_abs_path(interp, buf4, ARRAY_SIZE(buf4)); + snprintf(cmdline, sizeof(cmdline), "%s \"%s\"", buf4, buf2); + } else { + snprintf(cmdline, sizeof(cmdline), "\"%s\"", buf2); + } + to_wchar(cmdline, wcmd, ARRAY_SIZE(wcmd)); + + if (CreateProcessW(NULL, wcmd, NULL, NULL, TRUE, CREATE_NEW_PROCESS_GROUP, + (void *) env, full_dir, &si, &pi) != 0) { + mg_spawn_stdio_thread(sock, a[1], mg_push_to_stdin); + mg_spawn_stdio_thread(sock, b[0], mg_pull_from_stdout); + + CloseHandle(si.hStdOutput); + CloseHandle(si.hStdInput); + + CloseHandle(pi.hThread); + CloseHandle(pi.hProcess); + } else { + CloseHandle(a[1]); + CloseHandle(b[0]); + closesocket(sock); + } + DBG(("CGI command: [%ls] -> %p", wcmd, pi.hProcess)); + + /* Not closing a[0] and b[1] because we've used DUPLICATE_CLOSE_SOURCE */ + (void) envp; + return (pi.hProcess != NULL); +} +#else +static int mg_start_process(const char *interp, const char *cmd, + const char *env, const char *envp[], + const char *dir, sock_t sock) { + char buf[500]; + pid_t pid = fork(); + (void) env; + + if (pid == 0) { + /* + * In Linux `chdir` declared with `warn_unused_result` attribute + * To shutup compiler we have yo use result in some way + */ + int tmp = chdir(dir); + (void) tmp; + (void) dup2(sock, 0); + (void) dup2(sock, 1); + closesocket(sock); + + /* + * After exec, all signal handlers are restored to their default values, + * with one exception of SIGCHLD. According to POSIX.1-2001 and Linux's + * implementation, SIGCHLD's handler will leave unchanged after exec + * if it was set to be ignored. Restore it to default action. + */ + signal(SIGCHLD, SIG_DFL); + + if (interp == NULL) { + execle(cmd, cmd, (char *) 0, envp); /* (char *) 0 to squash warning */ + } else { + execle(interp, interp, cmd, (char *) 0, envp); + } + snprintf(buf, sizeof(buf), + "Status: 500\r\n\r\n" + "500 Server Error: %s%s%s: %s", + interp == NULL ? "" : interp, interp == NULL ? "" : " ", cmd, + strerror(errno)); + send(1, buf, strlen(buf), 0); + _exit(EXIT_FAILURE); /* exec call failed */ + } + + return (pid != 0); +} +#endif /* _WIN32 */ + +/* + * Append VARIABLE=VALUE\0 string to the buffer, and add a respective + * pointer into the vars array. + */ +static char *mg_addenv(struct mg_cgi_env_block *block, const char *fmt, ...) { + int n, space; + char *added = block->buf + block->len; + va_list ap; + + /* Calculate how much space is left in the buffer */ + space = sizeof(block->buf) - (block->len + 2); + if (space > 0) { + /* Copy VARIABLE=VALUE\0 string into the free space */ + va_start(ap, fmt); + n = vsnprintf(added, (size_t) space, fmt, ap); + va_end(ap); + + /* Make sure we do not overflow buffer and the envp array */ + if (n > 0 && n + 1 < space && + block->nvars < (int) ARRAY_SIZE(block->vars) - 2) { + /* Append a pointer to the added string into the envp array */ + block->vars[block->nvars++] = added; + /* Bump up used length counter. Include \0 terminator */ + block->len += n + 1; + } + } + + return added; +} + +static void mg_addenv2(struct mg_cgi_env_block *blk, const char *name) { + const char *s; + if ((s = getenv(name)) != NULL) mg_addenv(blk, "%s=%s", name, s); +} + +static void mg_prepare_cgi_environment(struct mg_connection *nc, + const char *prog, + const struct mg_str *path_info, + const struct http_message *hm, + const struct mg_serve_http_opts *opts, + struct mg_cgi_env_block *blk) { + const char *s; + struct mg_str *h; + char *p; + size_t i; + char buf[100]; + size_t path_info_len = path_info != NULL ? path_info->len : 0; + + blk->len = blk->nvars = 0; + blk->nc = nc; + + if ((s = getenv("SERVER_NAME")) != NULL) { + mg_addenv(blk, "SERVER_NAME=%s", s); + } else { + mg_sock_to_str(nc->sock, buf, sizeof(buf), 3); + mg_addenv(blk, "SERVER_NAME=%s", buf); + } + mg_addenv(blk, "SERVER_ROOT=%s", opts->document_root); + mg_addenv(blk, "DOCUMENT_ROOT=%s", opts->document_root); + mg_addenv(blk, "SERVER_SOFTWARE=%s/%s", "Mongoose", MG_VERSION); + + /* Prepare the environment block */ + mg_addenv(blk, "%s", "GATEWAY_INTERFACE=CGI/1.1"); + mg_addenv(blk, "%s", "SERVER_PROTOCOL=HTTP/1.1"); + mg_addenv(blk, "%s", "REDIRECT_STATUS=200"); /* For PHP */ + + mg_addenv(blk, "REQUEST_METHOD=%.*s", (int) hm->method.len, hm->method.p); + + mg_addenv(blk, "REQUEST_URI=%.*s%s%.*s", (int) hm->uri.len, hm->uri.p, + hm->query_string.len == 0 ? "" : "?", (int) hm->query_string.len, + hm->query_string.p); + + mg_conn_addr_to_str(nc, buf, sizeof(buf), + MG_SOCK_STRINGIFY_REMOTE | MG_SOCK_STRINGIFY_IP); + mg_addenv(blk, "REMOTE_ADDR=%s", buf); + mg_conn_addr_to_str(nc, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT); + mg_addenv(blk, "SERVER_PORT=%s", buf); + + s = hm->uri.p + hm->uri.len - path_info_len - 1; + if (*s == '/') { + const char *base_name = strrchr(prog, DIRSEP); + mg_addenv(blk, "SCRIPT_NAME=%.*s/%s", (int) (s - hm->uri.p), hm->uri.p, + (base_name != NULL ? base_name + 1 : prog)); + } else { + mg_addenv(blk, "SCRIPT_NAME=%.*s", (int) (s - hm->uri.p + 1), hm->uri.p); + } + mg_addenv(blk, "SCRIPT_FILENAME=%s", prog); + + if (path_info != NULL && path_info->len > 0) { + mg_addenv(blk, "PATH_INFO=%.*s", (int) path_info->len, path_info->p); + /* Not really translated... */ + mg_addenv(blk, "PATH_TRANSLATED=%.*s", (int) path_info->len, path_info->p); + } + +#if MG_ENABLE_SSL + mg_addenv(blk, "HTTPS=%s", (nc->flags & MG_F_SSL ? "on" : "off")); +#else + mg_addenv(blk, "HTTPS=off"); +#endif + + if ((h = mg_get_http_header((struct http_message *) hm, "Content-Type")) != + NULL) { + mg_addenv(blk, "CONTENT_TYPE=%.*s", (int) h->len, h->p); + } + + if (hm->query_string.len > 0) { + mg_addenv(blk, "QUERY_STRING=%.*s", (int) hm->query_string.len, + hm->query_string.p); + } + + if ((h = mg_get_http_header((struct http_message *) hm, "Content-Length")) != + NULL) { + mg_addenv(blk, "CONTENT_LENGTH=%.*s", (int) h->len, h->p); + } + + mg_addenv2(blk, "PATH"); + mg_addenv2(blk, "TMP"); + mg_addenv2(blk, "TEMP"); + mg_addenv2(blk, "TMPDIR"); + mg_addenv2(blk, "PERLLIB"); + mg_addenv2(blk, MG_ENV_EXPORT_TO_CGI); + +#ifdef _WIN32 + mg_addenv2(blk, "COMSPEC"); + mg_addenv2(blk, "SYSTEMROOT"); + mg_addenv2(blk, "SystemDrive"); + mg_addenv2(blk, "ProgramFiles"); + mg_addenv2(blk, "ProgramFiles(x86)"); + mg_addenv2(blk, "CommonProgramFiles(x86)"); +#else + mg_addenv2(blk, "LD_LIBRARY_PATH"); +#endif /* _WIN32 */ + + /* Add all headers as HTTP_* variables */ + for (i = 0; hm->header_names[i].len > 0; i++) { + p = mg_addenv(blk, "HTTP_%.*s=%.*s", (int) hm->header_names[i].len, + hm->header_names[i].p, (int) hm->header_values[i].len, + hm->header_values[i].p); + + /* Convert variable name into uppercase, and change - to _ */ + for (; *p != '=' && *p != '\0'; p++) { + if (*p == '-') *p = '_'; + *p = (char) toupper(*(unsigned char *) p); + } + } + + blk->vars[blk->nvars++] = NULL; + blk->buf[blk->len++] = '\0'; +} + +static void mg_cgi_ev_handler(struct mg_connection *cgi_nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { +#if !MG_ENABLE_CALLBACK_USERDATA + void *user_data = cgi_nc->user_data; +#endif + struct mg_connection *nc = (struct mg_connection *) user_data; + (void) ev_data; + + if (nc == NULL) { + /* The corresponding network connection was closed. */ + cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return; + } + + switch (ev) { + case MG_EV_RECV: + /* + * CGI script does not output reply line, like "HTTP/1.1 CODE XXXXX\n" + * It outputs headers, then body. Headers might include "Status" + * header, which changes CODE, and it might include "Location" header + * which changes CODE to 302. + * + * Therefore we do not send the output from the CGI script to the user + * until all CGI headers are received. + * + * Here we parse the output from the CGI script, and if all headers has + * been received, send appropriate reply line, and forward all + * received headers to the client. + */ + if (nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS) { + struct mbuf *io = &cgi_nc->recv_mbuf; + int len = mg_http_get_request_len(io->buf, io->len); + + if (len == 0) break; + if (len < 0 || io->len > MG_MAX_HTTP_REQUEST_SIZE) { + cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY; + mg_http_send_error(nc, 500, "Bad headers"); + } else { + struct http_message hm; + struct mg_str *h; + mg_http_parse_headers(io->buf, io->buf + io->len, io->len, &hm); + if (mg_get_http_header(&hm, "Location") != NULL) { + mg_printf(nc, "%s", "HTTP/1.1 302 Moved\r\n"); + } else if ((h = mg_get_http_header(&hm, "Status")) != NULL) { + mg_printf(nc, "HTTP/1.1 %.*s\r\n", (int) h->len, h->p); + } else { + mg_printf(nc, "%s", "HTTP/1.1 200 OK\r\n"); + } + } + nc->flags &= ~MG_F_HTTP_CGI_PARSE_HEADERS; + } + if (!(nc->flags & MG_F_HTTP_CGI_PARSE_HEADERS)) { + mg_forward(cgi_nc, nc); + } + break; + case MG_EV_CLOSE: + DBG(("%p CLOSE", cgi_nc)); + mg_http_free_proto_data_cgi(&mg_http_get_proto_data(nc)->cgi); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + } +} + +MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog, + const struct mg_str *path_info, + const struct http_message *hm, + const struct mg_serve_http_opts *opts) { + struct mg_cgi_env_block blk; + char dir[MG_MAX_PATH]; + const char *p; + sock_t fds[2]; + + DBG(("%p [%s]", nc, prog)); + mg_prepare_cgi_environment(nc, prog, path_info, hm, opts, &blk); + /* + * CGI must be executed in its own directory. 'dir' must point to the + * directory containing executable program, 'p' must point to the + * executable program name relative to 'dir'. + */ + if ((p = strrchr(prog, DIRSEP)) == NULL) { + snprintf(dir, sizeof(dir), "%s", "."); + } else { + snprintf(dir, sizeof(dir), "%.*s", (int) (p - prog), prog); + prog = p + 1; + } + + if (!mg_socketpair(fds, SOCK_STREAM)) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return; + } + +#ifndef _WIN32 + struct sigaction sa; + + sigemptyset(&sa.sa_mask); + sa.sa_handler = SIG_IGN; + sa.sa_flags = 0; + sigaction(SIGCHLD, &sa, NULL); +#endif + + if (mg_start_process(opts->cgi_interpreter, prog, blk.buf, blk.vars, dir, + fds[1]) != 0) { + size_t n = nc->recv_mbuf.len - (hm->message.len - hm->body.len); + struct mg_connection *cgi_nc = + mg_add_sock(nc->mgr, fds[0], mg_cgi_ev_handler MG_UD_ARG(nc)); + struct mg_http_proto_data *cgi_pd = mg_http_get_proto_data(nc); + cgi_pd->cgi.cgi_nc = cgi_nc; +#if !MG_ENABLE_CALLBACK_USERDATA + cgi_pd->cgi.cgi_nc->user_data = nc; +#endif + nc->flags |= MG_F_HTTP_CGI_PARSE_HEADERS; + /* Push POST data to the CGI */ + if (n > 0 && n < nc->recv_mbuf.len) { + mg_send(cgi_pd->cgi.cgi_nc, hm->body.p, n); + } + mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len); + } else { + closesocket(fds[0]); + mg_http_send_error(nc, 500, "CGI failure"); + } + +#ifndef _WIN32 + closesocket(fds[1]); /* On Windows, CGI stdio thread closes that socket */ +#endif +} + +MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d) { + if (d == NULL) return; + if (d->cgi_nc != NULL) { + d->cgi_nc->flags |= MG_F_CLOSE_IMMEDIATELY; + d->cgi_nc->user_data = NULL; + } + memset(d, 0, sizeof(*d)); +} + +#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_CGI */ diff --git a/src/http_client.h b/src/http_client.h new file mode 100644 index 00000000..9597ec58 --- /dev/null +++ b/src/http_client.h @@ -0,0 +1,62 @@ +/* + * === Client API reference + */ + +#ifndef CS_MONGOOSE_SRC_HTTP_CLIENT_H_ +#define CS_MONGOOSE_SRC_HTTP_CLIENT_H_ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * Helper function that creates an outbound HTTP connection. + * + * `url` is the URL to fetch. It must be properly URL-encoded, e.g. have + * no spaces, etc. By default, `mg_connect_http()` sends the Connection and + * Host headers. `extra_headers` is an extra HTTP header to send, e.g. + * `"User-Agent: my-app\r\n"`. + * If `post_data` is NULL, then a GET request is created. Otherwise, a POST + * request is created with the specified POST data. Note that if the data being + * posted is a form submission, the `Content-Type` header should be set + * accordingly (see example below). + * + * Examples: + * + * ```c + * nc1 = mg_connect_http(mgr, ev_handler_1, "http://www.google.com", NULL, + * NULL); + * nc2 = mg_connect_http(mgr, ev_handler_1, "https://github.com", NULL, NULL); + * nc3 = mg_connect_http( + * mgr, ev_handler_1, "my_server:8000/form_submit/", + * "Content-Type: application/x-www-form-urlencoded\r\n", + * "var_1=value_1&var_2=value_2"); + * ``` + */ +struct mg_connection *mg_connect_http( + struct mg_mgr *mgr, + MG_CB(mg_event_handler_t event_handler, void *user_data), const char *url, + const char *extra_headers, const char *post_data); + +/* + * Helper function that creates an outbound HTTP connection. + * + * Mostly identical to mg_connect_http, but allows you to provide extra + *parameters + * (for example, SSL parameters) + */ +struct mg_connection *mg_connect_http_opt( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *extra_headers, + const char *post_data); + +/* Creates digest authentication header for a client request. */ +int mg_http_create_digest_auth_header(char *buf, size_t buf_len, + const char *method, const char *uri, + const char *auth_domain, const char *user, + const char *passwd, const char *nonce); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CS_MONGOOSE_SRC_HTTP_CLIENT_H_ */ diff --git a/src/http_server.h b/src/http_server.h new file mode 100644 index 00000000..62f298f8 --- /dev/null +++ b/src/http_server.h @@ -0,0 +1,540 @@ +/* + * === Server API reference + */ + +#ifndef CS_MONGOOSE_SRC_HTTP_SERVER_H_ +#define CS_MONGOOSE_SRC_HTTP_SERVER_H_ + +#if MG_ENABLE_HTTP + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * Parses a HTTP message. + * + * `is_req` should be set to 1 if parsing a request, 0 if reply. + * + * Returns the number of bytes parsed. If HTTP message is + * incomplete `0` is returned. On parse error, a negative number is returned. + */ +int mg_parse_http(const char *s, int n, struct http_message *hm, int is_req); + +/* + * Searches and returns the header `name` in parsed HTTP message `hm`. + * If header is not found, NULL is returned. Example: + * + * struct mg_str *host_hdr = mg_get_http_header(hm, "Host"); + */ +struct mg_str *mg_get_http_header(struct http_message *hm, const char *name); + +/* + * Parses the HTTP header `hdr`. Finds variable `var_name` and stores its value + * in the buffer `buf`, `buf_size`. Returns 0 if variable not found, non-zero + * otherwise. + * + * This function is supposed to parse cookies, authentication headers, etc. + * Example (error handling omitted): + * + * char user[20]; + * struct mg_str *hdr = mg_get_http_header(hm, "Authorization"); + * mg_http_parse_header(hdr, "username", user, sizeof(user)); + * + * Returns the length of the variable's value. If buffer is not large enough, + * or variable not found, 0 is returned. + */ +int mg_http_parse_header(struct mg_str *hdr, const char *var_name, char *buf, + size_t buf_size); + +/* + * Gets and parses the Authorization: Basic header + * Returns -1 if no Authorization header is found, or if + * mg_parse_http_basic_auth + * fails parsing the resulting header. + */ +int mg_get_http_basic_auth(struct http_message *hm, char *user, size_t user_len, + char *pass, size_t pass_len); + +/* + * Parses the Authorization: Basic header + * Returns -1 iif the authorization type is not "Basic" or any other error such + * as incorrectly encoded base64 user password pair. + */ +int mg_parse_http_basic_auth(struct mg_str *hdr, char *user, size_t user_len, + char *pass, size_t pass_len); + +/* + * Parses the buffer `buf`, `buf_len` that contains multipart form data chunks. + * Stores the chunk name in a `var_name`, `var_name_len` buffer. + * If a chunk is an uploaded file, then `file_name`, `file_name_len` is + * filled with an uploaded file name. `chunk`, `chunk_len` + * points to the chunk data. + * + * Return: number of bytes to skip to the next chunk or 0 if there are + * no more chunks. + * + * Usage example: + * + * ```c + * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + * switch(ev) { + * case MG_EV_HTTP_REQUEST: { + * struct http_message *hm = (struct http_message *) ev_data; + * char var_name[100], file_name[100]; + * const char *chunk; + * size_t chunk_len, n1, n2; + * + * n1 = n2 = 0; + * while ((n2 = mg_parse_multipart(hm->body.p + n1, + * hm->body.len - n1, + * var_name, sizeof(var_name), + * file_name, sizeof(file_name), + * &chunk, &chunk_len)) > 0) { + * printf("var: %s, file_name: %s, size: %d, chunk: [%.*s]\n", + * var_name, file_name, (int) chunk_len, + * (int) chunk_len, chunk); + * n1 += n2; + * } + * } + * break; + * ``` + */ +size_t mg_parse_multipart(const char *buf, size_t buf_len, char *var_name, + size_t var_name_len, char *file_name, + size_t file_name_len, const char **chunk, + size_t *chunk_len); + +/* + * Fetches a HTTP form variable. + * + * Fetches a variable `name` from a `buf` into a buffer specified by `dst`, + * `dst_len`. The destination is always zero-terminated. Returns the length of + * a fetched variable. If not found, 0 is returned. `buf` must be valid + * url-encoded buffer. If destination is too small or an error occured, + * negative number is returned. + */ +int mg_get_http_var(const struct mg_str *buf, const char *name, char *dst, + size_t dst_len); + +#if MG_ENABLE_FILESYSTEM +/* + * This structure defines how `mg_serve_http()` works. + * Best practice is to set only required settings, and leave the rest as NULL. + */ +struct mg_serve_http_opts { + /* Path to web root directory */ + const char *document_root; + + /* List of index files. Default is "" */ + const char *index_files; + + /* + * Leave as NULL to disable authentication. + * To enable directory protection with authentication, set this to ".htpasswd" + * Then, creating ".htpasswd" file in any directory automatically protects + * it with digest authentication. + * Use `mongoose` web server binary, or `htdigest` Apache utility to + * create/manipulate passwords file. + * Make sure `auth_domain` is set to a valid domain name. + */ + const char *per_directory_auth_file; + + /* Authorization domain (domain name of this web server) */ + const char *auth_domain; + + /* + * Leave as NULL to disable authentication. + * Normally, only selected directories in the document root are protected. + * If absolutely every access to the web server needs to be authenticated, + * regardless of the URI, set this option to the path to the passwords file. + * Format of that file is the same as ".htpasswd" file. Make sure that file + * is located outside document root to prevent people fetching it. + */ + const char *global_auth_file; + + /* Set to "no" to disable directory listing. Enabled by default. */ + const char *enable_directory_listing; + + /* + * SSI files pattern. If not set, "**.shtml$|**.shtm$" is used. + * + * All files that match ssi_pattern are treated as SSI. + * + * Server Side Includes (SSI) is a simple interpreted server-side scripting + * language which is most commonly used to include the contents of a file + * into a web page. It can be useful when it is desirable to include a common + * piece of code throughout a website, for example, headers and footers. + * + * In order for a webpage to recognize an SSI-enabled HTML file, the + * filename should end with a special extension, by default the extension + * should be either .shtml or .shtm + * + * Unknown SSI directives are silently ignored by Mongoose. Currently, + * the following SSI directives are supported: + * <!--#include FILE_TO_INCLUDE --> + * <!--#exec "COMMAND_TO_EXECUTE" --> + * <!--#call COMMAND --> + * + * Note that <!--#include ...> directive supports three path + *specifications: + * + * <!--#include virtual="path" --> Path is relative to web server root + * <!--#include abspath="path" --> Path is absolute or relative to the + * web server working dir + * <!--#include file="path" -->, Path is relative to current document + * <!--#include "path" --> + * + * The include directive may be used to include the contents of a file or + * the result of running a CGI script. + * + * The exec directive is used to execute + * a command on a server, and show command's output. Example: + * + * <!--#exec "ls -l" --> + * + * The call directive is a way to invoke a C handler from the HTML page. + * On each occurence of <!--#call COMMAND OPTIONAL_PARAMS> directive, + * Mongoose calls a registered event handler with MG_EV_SSI_CALL event, + * and event parameter will point to the COMMAND OPTIONAL_PARAMS string. + * An event handler can output any text, for example by calling + * `mg_printf()`. This is a flexible way of generating a web page on + * server side by calling a C event handler. Example: + * + * <!--#call foo --> ... <!--#call bar --> + * + * In the event handler: + * case MG_EV_SSI_CALL: { + * const char *param = (const char *) ev_data; + * if (strcmp(param, "foo") == 0) { + * mg_printf(c, "hello from foo"); + * } else if (strcmp(param, "bar") == 0) { + * mg_printf(c, "hello from bar"); + * } + * break; + * } + */ + const char *ssi_pattern; + + /* IP ACL. By default, NULL, meaning all IPs are allowed to connect */ + const char *ip_acl; + +#if MG_ENABLE_HTTP_URL_REWRITES + /* URL rewrites. + * + * Comma-separated list of `uri_pattern=url_file_or_directory_path` rewrites. + * When HTTP request is received, Mongoose constructs a file name from the + * requested URI by combining `document_root` and the URI. However, if the + * rewrite option is used and `uri_pattern` matches requested URI, then + * `document_root` is ignored. Instead, `url_file_or_directory_path` is used, + * which should be a full path name or a path relative to the web server's + * current working directory. It can also be an URI (http:// or https://) + * in which case mongoose will behave as a reverse proxy for that destination. + * + * Note that `uri_pattern`, as all Mongoose patterns, is a prefix pattern. + * + * If uri_pattern starts with `@` symbol, then Mongoose compares it with the + * HOST header of the request. If they are equal, Mongoose sets document root + * to `file_or_directory_path`, implementing virtual hosts support. + * Example: `@foo.com=/document/root/for/foo.com` + * + * If `uri_pattern` starts with `%` symbol, then Mongoose compares it with + * the listening port. If they match, then Mongoose issues a 301 redirect. + * For example, to redirect all HTTP requests to the + * HTTPS port, do `%80=https://my.site.com`. Note that the request URI is + * automatically appended to the redirect location. + */ + const char *url_rewrites; +#endif + + /* DAV document root. If NULL, DAV requests are going to fail. */ + const char *dav_document_root; + + /* + * DAV passwords file. If NULL, DAV requests are going to fail. + * If passwords file is set to "-", then DAV auth is disabled. + */ + const char *dav_auth_file; + + /* Glob pattern for the files to hide. */ + const char *hidden_file_pattern; + + /* Set to non-NULL to enable CGI, e.g. **.cgi$|**.php$" */ + const char *cgi_file_pattern; + + /* If not NULL, ignore CGI script hashbang and use this interpreter */ + const char *cgi_interpreter; + + /* + * Comma-separated list of Content-Type overrides for path suffixes, e.g. + * ".txt=text/plain; charset=utf-8,.c=text/plain" + */ + const char *custom_mime_types; + + /* + * Extra HTTP headers to add to each server response. + * Example: to enable CORS, set this to "Access-Control-Allow-Origin: *". + */ + const char *extra_headers; +}; + +/* + * Serves given HTTP request according to the `options`. + * + * Example code snippet: + * + * ```c + * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + * struct http_message *hm = (struct http_message *) ev_data; + * struct mg_serve_http_opts opts = { .document_root = "/var/www" }; // C99 + * + * switch (ev) { + * case MG_EV_HTTP_REQUEST: + * mg_serve_http(nc, hm, opts); + * break; + * default: + * break; + * } + * } + * ``` + */ +void mg_serve_http(struct mg_connection *nc, struct http_message *hm, + struct mg_serve_http_opts opts); + +/* + * Serves a specific file with a given MIME type and optional extra headers. + * + * Example code snippet: + * + * ```c + * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + * switch (ev) { + * case MG_EV_HTTP_REQUEST: { + * struct http_message *hm = (struct http_message *) ev_data; + * mg_http_serve_file(nc, hm, "file.txt", + * mg_mk_str("text/plain"), mg_mk_str("")); + * break; + * } + * ... + * } + * } + * ``` + */ +void mg_http_serve_file(struct mg_connection *nc, struct http_message *hm, + const char *path, const struct mg_str mime_type, + const struct mg_str extra_headers); + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + +/* Callback prototype for `mg_file_upload_handler()`. */ +typedef struct mg_str (*mg_fu_fname_fn)(struct mg_connection *nc, + struct mg_str fname); + +/* + * File upload handler. + * This handler can be used to implement file uploads with minimum code. + * This handler will process MG_EV_HTTP_PART_* events and store file data into + * a local file. + * `local_name_fn` will be invoked with whatever name was provided by the client + * and will expect the name of the local file to open. A return value of NULL + * will abort file upload (client will get a "403 Forbidden" response). If + * non-null, the returned string must be heap-allocated and will be freed by + * the caller. + * Exception: it is ok to return the same string verbatim. + * + * Example: + * + * ```c + * struct mg_str upload_fname(struct mg_connection *nc, struct mg_str fname) { + * // Just return the same filename. Do not actually do this except in test! + * // fname is user-controlled and needs to be sanitized. + * return fname; + * } + * void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + * switch (ev) { + * ... + * case MG_EV_HTTP_PART_BEGIN: + * case MG_EV_HTTP_PART_DATA: + * case MG_EV_HTTP_PART_END: + * mg_file_upload_handler(nc, ev, ev_data, upload_fname); + * break; + * } + * } + * ``` + */ +void mg_file_upload_handler(struct mg_connection *nc, int ev, void *ev_data, + mg_fu_fname_fn local_name_fn + MG_UD_ARG(void *user_data)); +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ +#endif /* MG_ENABLE_FILESYSTEM */ + +/* + * Registers a callback for a specified http endpoint + * Note: if callback is registered it is called instead of the + * callback provided in mg_bind + * + * Example code snippet: + * + * ```c + * static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) { + * (void) ev; (void) ev_data; + * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]"); + * nc->flags |= MG_F_SEND_AND_CLOSE; + * } + * + * static void handle_hello2(struct mg_connection *nc, int ev, void *ev_data) { + * (void) ev; (void) ev_data; + * mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello2]"); + * nc->flags |= MG_F_SEND_AND_CLOSE; + * } + * + * void init() { + * nc = mg_bind(&mgr, local_addr, cb1); + * mg_register_http_endpoint(nc, "/hello1", handle_hello1); + * mg_register_http_endpoint(nc, "/hello1/hello2", handle_hello2); + * } + * ``` + */ +void mg_register_http_endpoint(struct mg_connection *nc, const char *uri_path, + MG_CB(mg_event_handler_t handler, + void *user_data)); + +struct mg_http_endpoint_opts { + void *user_data; + /* Authorization domain (realm) */ + const char *auth_domain; + const char *auth_file; +}; + +void mg_register_http_endpoint_opt(struct mg_connection *nc, + const char *uri_path, + mg_event_handler_t handler, + struct mg_http_endpoint_opts opts); + +/* + * Authenticates a HTTP request against an opened password file. + * Returns 1 if authenticated, 0 otherwise. + */ +int mg_http_check_digest_auth(struct http_message *hm, const char *auth_domain, + FILE *fp); + +/* + * Authenticates given response params against an opened password file. + * Returns 1 if authenticated, 0 otherwise. + * + * It's used by mg_http_check_digest_auth(). + */ +int mg_check_digest_auth(struct mg_str method, struct mg_str uri, + struct mg_str username, struct mg_str cnonce, + struct mg_str response, struct mg_str qop, + struct mg_str nc, struct mg_str nonce, + struct mg_str auth_domain, FILE *fp); + +/* + * Sends buffer `buf` of size `len` to the client using chunked HTTP encoding. + * This function sends the buffer size as hex number + newline first, then + * the buffer itself, then the newline. For example, + * `mg_send_http_chunk(nc, "foo", 3)` will append the `3\r\nfoo\r\n` string + * to the `nc->send_mbuf` output IO buffer. + * + * NOTE: The HTTP header "Transfer-Encoding: chunked" should be sent prior to + * using this function. + * + * NOTE: do not forget to send an empty chunk at the end of the response, + * to tell the client that everything was sent. Example: + * + * ``` + * mg_printf_http_chunk(nc, "%s", "my response!"); + * mg_send_http_chunk(nc, "", 0); // Tell the client we're finished + * ``` + */ +void mg_send_http_chunk(struct mg_connection *nc, const char *buf, size_t len); + +/* + * Sends a printf-formatted HTTP chunk. + * Functionality is similar to `mg_send_http_chunk()`. + */ +void mg_printf_http_chunk(struct mg_connection *nc, const char *fmt, ...); + +/* + * Sends the response status line. + * If `extra_headers` is not NULL, then `extra_headers` are also sent + * after the response line. `extra_headers` must NOT end end with new line. + * Example: + * + * mg_send_response_line(nc, 200, "Access-Control-Allow-Origin: *"); + * + * Will result in: + * + * HTTP/1.1 200 OK\r\n + * Access-Control-Allow-Origin: *\r\n + */ +void mg_send_response_line(struct mg_connection *nc, int status_code, + const char *extra_headers); + +/* + * Sends an error response. If reason is NULL, the message will be inferred + * from the error code (if supported). + */ +void mg_http_send_error(struct mg_connection *nc, int code, const char *reason); + +/* + * Sends a redirect response. + * `status_code` should be either 301 or 302 and `location` point to the + * new location. + * If `extra_headers` is not empty, then `extra_headers` are also sent + * after the response line. `extra_headers` must NOT end end with new line. + * + * Example: + * + * mg_http_send_redirect(nc, 302, mg_mk_str("/login"), mg_mk_str(NULL)); + */ +void mg_http_send_redirect(struct mg_connection *nc, int status_code, + const struct mg_str location, + const struct mg_str extra_headers); + +/* + * Sends the response line and headers. + * This function sends the response line with the `status_code`, and + * automatically + * sends one header: either "Content-Length" or "Transfer-Encoding". + * If `content_length` is negative, then "Transfer-Encoding: chunked" header + * is sent, otherwise, "Content-Length" header is sent. + * + * NOTE: If `Transfer-Encoding` is `chunked`, then message body must be sent + * using `mg_send_http_chunk()` or `mg_printf_http_chunk()` functions. + * Otherwise, `mg_send()` or `mg_printf()` must be used. + * Extra headers could be set through `extra_headers`. Note `extra_headers` + * must NOT be terminated by a new line. + */ +void mg_send_head(struct mg_connection *n, int status_code, + int64_t content_length, const char *extra_headers); + +/* + * Sends a printf-formatted HTTP chunk, escaping HTML tags. + */ +void mg_printf_html_escape(struct mg_connection *nc, const char *fmt, ...); + +#if MG_ENABLE_HTTP_URL_REWRITES +/* + * Proxies a given request to a given upstream http server. The path prefix + * in `mount` will be stripped of the path requested to the upstream server, + * e.g. if mount is /api and upstream is http://localhost:8001/foo + * then an incoming request to /api/bar will cause a request to + * http://localhost:8001/foo/bar + * + * EXPERIMENTAL API. Please use http_serve_http + url_rewrites if a static + * mapping is good enough. + */ +void mg_http_reverse_proxy(struct mg_connection *nc, + const struct http_message *hm, struct mg_str mount, + struct mg_str upstream); +#endif + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_HTTP */ + +#endif /* CS_MONGOOSE_SRC_HTTP_SERVER_H_ */ diff --git a/src/http_ssi.c b/src/http_ssi.c new file mode 100644 index 00000000..6d384db7 --- /dev/null +++ b/src/http_ssi.c @@ -0,0 +1,193 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_SSI && MG_ENABLE_FILESYSTEM + +static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm, + const char *path, FILE *fp, int include_level, + const struct mg_serve_http_opts *opts); + +static void mg_send_file_data(struct mg_connection *nc, FILE *fp) { + char buf[BUFSIZ]; + size_t n; + while ((n = mg_fread(buf, 1, sizeof(buf), fp)) > 0) { + mg_send(nc, buf, n); + } +} + +static void mg_do_ssi_include(struct mg_connection *nc, struct http_message *hm, + const char *ssi, char *tag, int include_level, + const struct mg_serve_http_opts *opts) { + char file_name[MG_MAX_PATH], path[MG_MAX_PATH], *p; + FILE *fp; + + /* + * sscanf() is safe here, since send_ssi_file() also uses buffer + * of size MG_BUF_LEN to get the tag. So strlen(tag) is always < MG_BUF_LEN. + */ + if (sscanf(tag, " virtual=\"%[^\"]\"", file_name) == 1) { + /* File name is relative to the webserver root */ + snprintf(path, sizeof(path), "%s/%s", opts->document_root, file_name); + } else if (sscanf(tag, " abspath=\"%[^\"]\"", file_name) == 1) { + /* + * File name is relative to the webserver working directory + * or it is absolute system path + */ + snprintf(path, sizeof(path), "%s", file_name); + } else if (sscanf(tag, " file=\"%[^\"]\"", file_name) == 1 || + sscanf(tag, " \"%[^\"]\"", file_name) == 1) { + /* File name is relative to the currect document */ + snprintf(path, sizeof(path), "%s", ssi); + if ((p = strrchr(path, DIRSEP)) != NULL) { + p[1] = '\0'; + } + snprintf(path + strlen(path), sizeof(path) - strlen(path), "%s", file_name); + } else { + mg_printf(nc, "Bad SSI #include: [%s]", tag); + return; + } + + if ((fp = mg_fopen(path, "rb")) == NULL) { + mg_printf(nc, "SSI include error: mg_fopen(%s): %s", path, + strerror(mg_get_errno())); + } else { + mg_set_close_on_exec((sock_t) fileno(fp)); + if (mg_match_prefix(opts->ssi_pattern, strlen(opts->ssi_pattern), path) > + 0) { + mg_send_ssi_file(nc, hm, path, fp, include_level + 1, opts); + } else { + mg_send_file_data(nc, fp); + } + fclose(fp); + } +} + +#if MG_ENABLE_HTTP_SSI_EXEC +static void do_ssi_exec(struct mg_connection *nc, char *tag) { + char cmd[BUFSIZ]; + FILE *fp; + + if (sscanf(tag, " \"%[^\"]\"", cmd) != 1) { + mg_printf(nc, "Bad SSI #exec: [%s]", tag); + } else if ((fp = popen(cmd, "r")) == NULL) { + mg_printf(nc, "Cannot SSI #exec: [%s]: %s", cmd, strerror(mg_get_errno())); + } else { + mg_send_file_data(nc, fp); + pclose(fp); + } +} +#endif /* MG_ENABLE_HTTP_SSI_EXEC */ + +/* + * SSI directive has the following format: + * + */ +static void mg_send_ssi_file(struct mg_connection *nc, struct http_message *hm, + const char *path, FILE *fp, int include_level, + const struct mg_serve_http_opts *opts) { + static const struct mg_str btag = MG_MK_STR(" */ + buf[i--] = '\0'; + while (i > 0 && buf[i] == ' ') { + buf[i--] = '\0'; + } + + /* Handle known SSI directives */ + if (strncmp(p, d_include.p, d_include.len) == 0) { + mg_do_ssi_include(nc, hm, path, p + d_include.len + 1, include_level, + opts); + } else if (strncmp(p, d_call.p, d_call.len) == 0) { + struct mg_ssi_call_ctx cctx; + memset(&cctx, 0, sizeof(cctx)); + cctx.req = hm; + cctx.file = mg_mk_str(path); + cctx.arg = mg_mk_str(p + d_call.len + 1); + mg_call(nc, NULL, nc->user_data, MG_EV_SSI_CALL, + (void *) cctx.arg.p); /* NUL added above */ + mg_call(nc, NULL, nc->user_data, MG_EV_SSI_CALL_CTX, &cctx); +#if MG_ENABLE_HTTP_SSI_EXEC + } else if (strncmp(p, d_exec.p, d_exec.len) == 0) { + do_ssi_exec(nc, p + d_exec.len + 1); +#endif + } else { + /* Silently ignore unknown SSI directive. */ + } + len = 0; + } else if (ch == '<') { + in_ssi_tag = 1; + if (len > 0) { + mg_send(nc, buf, (size_t) len); + } + len = 0; + buf[len++] = ch & 0xff; + } else if (in_ssi_tag) { + if (len == (int) btag.len && strncmp(buf, btag.p, btag.len) != 0) { + /* Not an SSI tag */ + in_ssi_tag = 0; + } else if (len == (int) sizeof(buf) - 2) { + mg_printf(nc, "%s: SSI tag is too large", path); + len = 0; + } + buf[len++] = ch & 0xff; + } else { + buf[len++] = ch & 0xff; + if (len == (int) sizeof(buf)) { + mg_send(nc, buf, (size_t) len); + len = 0; + } + } + } + + /* Send the rest of buffered data */ + if (len > 0) { + mg_send(nc, buf, (size_t) len); + } +} + +MG_INTERNAL void mg_handle_ssi_request(struct mg_connection *nc, + struct http_message *hm, + const char *path, + const struct mg_serve_http_opts *opts) { + FILE *fp; + struct mg_str mime_type; + DBG(("%p %s", nc, path)); + + if ((fp = mg_fopen(path, "rb")) == NULL) { + mg_http_send_error(nc, 404, NULL); + } else { + mg_set_close_on_exec((sock_t) fileno(fp)); + + mime_type = mg_get_mime_type(path, "text/plain", opts); + mg_send_response_line(nc, 200, opts->extra_headers); + mg_printf(nc, + "Content-Type: %.*s\r\n" + "Connection: close\r\n\r\n", + (int) mime_type.len, mime_type.p); + mg_send_ssi_file(nc, hm, path, fp, 0, opts); + fclose(fp); + nc->flags |= MG_F_SEND_AND_CLOSE; + } +} + +#endif /* MG_ENABLE_HTTP_SSI && MG_ENABLE_HTTP && MG_ENABLE_FILESYSTEM */ diff --git a/src/http_webdav.c b/src/http_webdav.c new file mode 100644 index 00000000..6efb928c --- /dev/null +++ b/src/http_webdav.c @@ -0,0 +1,269 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV + +MG_INTERNAL int mg_is_dav_request(const struct mg_str *s) { + static const char *methods[] = { + "PUT", + "DELETE", + "MKCOL", + "PROPFIND", + "MOVE" +#if MG_ENABLE_FAKE_DAVLOCK + , + "LOCK", + "UNLOCK" +#endif + }; + size_t i; + + for (i = 0; i < ARRAY_SIZE(methods); i++) { + if (mg_vcmp(s, methods[i]) == 0) { + return 1; + } + } + + return 0; +} + +static int mg_mkdir(const char *path, uint32_t mode) { +#ifndef _WIN32 + return mkdir(path, mode); +#else + (void) mode; + return _mkdir(path); +#endif +} + +static void mg_print_props(struct mg_connection *nc, const char *name, + cs_stat_t *stp) { + char mtime[64]; + time_t t = stp->st_mtime; /* store in local variable for NDK compile */ + struct mg_str name_esc = mg_url_encode(mg_mk_str(name)); + mg_gmt_time_string(mtime, sizeof(mtime), &t); + mg_printf(nc, + "" + "%s" + "" + "" + "%s" + "%" INT64_FMT + "" + "%s" + "" + "HTTP/1.1 200 OK" + "" + "\n", + name_esc.p, S_ISDIR(stp->st_mode) ? "" : "", + (int64_t) stp->st_size, mtime); + free((void *) name_esc.p); +} + +MG_INTERNAL void mg_handle_propfind(struct mg_connection *nc, const char *path, + cs_stat_t *stp, struct http_message *hm, + struct mg_serve_http_opts *opts) { + static const char header[] = + "HTTP/1.1 207 Multi-Status\r\n" + "Connection: close\r\n" + "Content-Type: text/xml; charset=utf-8\r\n\r\n" + "" + "\n"; + static const char footer[] = "\n"; + const struct mg_str *depth = mg_get_http_header(hm, "Depth"); + + /* Print properties for the requested resource itself */ + if (S_ISDIR(stp->st_mode) && + strcmp(opts->enable_directory_listing, "yes") != 0) { + mg_printf(nc, "%s", "HTTP/1.1 403 Directory Listing Denied\r\n\r\n"); + } else { + char uri[MG_MAX_PATH]; + mg_send(nc, header, sizeof(header) - 1); + snprintf(uri, sizeof(uri), "%.*s", (int) hm->uri.len, hm->uri.p); + mg_print_props(nc, uri, stp); + if (S_ISDIR(stp->st_mode) && (depth == NULL || mg_vcmp(depth, "0") != 0)) { + mg_scan_directory(nc, path, opts, mg_print_props); + } + mg_send(nc, footer, sizeof(footer) - 1); + nc->flags |= MG_F_SEND_AND_CLOSE; + } +} + +#if MG_ENABLE_FAKE_DAVLOCK +/* + * Windows explorer (probably there are another WebDav clients like it) + * requires LOCK support in webdav. W/out this, it still works, but fails + * to save file: shows error message and offers "Save As". + * "Save as" works, but this message is very annoying. + * This is fake lock, which doesn't lock something, just returns LOCK token, + * UNLOCK always answers "OK". + * With this fake LOCK Windows Explorer looks happy and saves file. + * NOTE: that is not DAV LOCK imlementation, it is just a way to shut up + * Windows native DAV client. This is why FAKE LOCK is not enabed by default + */ +MG_INTERNAL void mg_handle_lock(struct mg_connection *nc, const char *path) { + static const char *reply = + "HTTP/1.1 207 Multi-Status\r\n" + "Connection: close\r\n" + "Content-Type: text/xml; charset=utf-8\r\n\r\n" + "" + "\n" + "\n" + "\n" + "\n" + "\n" + "opaquelocktoken:%s%u" + "" + "" + "\n" + "" + "\n"; + mg_printf(nc, reply, path, (unsigned int) mg_time()); + nc->flags |= MG_F_SEND_AND_CLOSE; +} +#endif + +MG_INTERNAL void mg_handle_mkcol(struct mg_connection *nc, const char *path, + struct http_message *hm) { + int status_code = 500; + if (hm->body.len != (size_t) ~0 && hm->body.len > 0) { + status_code = 415; + } else if (!mg_mkdir(path, 0755)) { + status_code = 201; + } else if (errno == EEXIST) { + status_code = 405; + } else if (errno == EACCES) { + status_code = 403; + } else if (errno == ENOENT) { + status_code = 409; + } else { + status_code = 500; + } + mg_http_send_error(nc, status_code, NULL); +} + +static int mg_remove_directory(const struct mg_serve_http_opts *opts, + const char *dir) { + char path[MG_MAX_PATH]; + struct dirent *dp; + cs_stat_t st; + DIR *dirp; + + if ((dirp = opendir(dir)) == NULL) return 0; + + while ((dp = readdir(dirp)) != NULL) { + if (mg_is_file_hidden((const char *) dp->d_name, opts, 1)) { + continue; + } + snprintf(path, sizeof(path), "%s%c%s", dir, '/', dp->d_name); + mg_stat(path, &st); + if (S_ISDIR(st.st_mode)) { + mg_remove_directory(opts, path); + } else { + remove(path); + } + } + closedir(dirp); + rmdir(dir); + + return 1; +} + +MG_INTERNAL void mg_handle_move(struct mg_connection *c, + const struct mg_serve_http_opts *opts, + const char *path, struct http_message *hm) { + const struct mg_str *dest = mg_get_http_header(hm, "Destination"); + if (dest == NULL) { + mg_http_send_error(c, 411, NULL); + } else { + const char *p = (char *) memchr(dest->p, '/', dest->len); + if (p != NULL && p[1] == '/' && + (p = (char *) memchr(p + 2, '/', dest->p + dest->len - p)) != NULL) { + char buf[MG_MAX_PATH]; + snprintf(buf, sizeof(buf), "%s%.*s", opts->dav_document_root, + (int) (dest->p + dest->len - p), p); + if (rename(path, buf) == 0) { + mg_http_send_error(c, 200, NULL); + } else { + mg_http_send_error(c, 418, NULL); + } + } else { + mg_http_send_error(c, 500, NULL); + } + } +} + +MG_INTERNAL void mg_handle_delete(struct mg_connection *nc, + const struct mg_serve_http_opts *opts, + const char *path) { + cs_stat_t st; + if (mg_stat(path, &st) != 0) { + mg_http_send_error(nc, 404, NULL); + } else if (S_ISDIR(st.st_mode)) { + mg_remove_directory(opts, path); + mg_http_send_error(nc, 204, NULL); + } else if (remove(path) == 0) { + mg_http_send_error(nc, 204, NULL); + } else { + mg_http_send_error(nc, 423, NULL); + } +} + +/* Return -1 on error, 1 on success. */ +static int mg_create_itermediate_directories(const char *path) { + const char *s; + + /* Create intermediate directories if they do not exist */ + for (s = path + 1; *s != '\0'; s++) { + if (*s == '/') { + char buf[MG_MAX_PATH]; + cs_stat_t st; + snprintf(buf, sizeof(buf), "%.*s", (int) (s - path), path); + buf[sizeof(buf) - 1] = '\0'; + if (mg_stat(buf, &st) != 0 && mg_mkdir(buf, 0755) != 0) { + return -1; + } + } + } + + return 1; +} + +MG_INTERNAL void mg_handle_put(struct mg_connection *nc, const char *path, + struct http_message *hm) { + struct mg_http_proto_data *pd = mg_http_get_proto_data(nc); + cs_stat_t st; + const struct mg_str *cl_hdr = mg_get_http_header(hm, "Content-Length"); + int rc, status_code = mg_stat(path, &st) == 0 ? 200 : 201; + + mg_http_free_proto_data_file(&pd->file); + if ((rc = mg_create_itermediate_directories(path)) == 0) { + mg_printf(nc, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code); + } else if (rc == -1) { + mg_http_send_error(nc, 500, NULL); + } else if (cl_hdr == NULL) { + mg_http_send_error(nc, 411, NULL); + } else if ((pd->file.fp = mg_fopen(path, "w+b")) == NULL) { + mg_http_send_error(nc, 500, NULL); + } else { + const struct mg_str *range_hdr = mg_get_http_header(hm, "Content-Range"); + int64_t r1 = 0, r2 = 0; + pd->file.type = DATA_PUT; + mg_set_close_on_exec((sock_t) fileno(pd->file.fp)); + pd->file.cl = to64(cl_hdr->p); + if (range_hdr != NULL && + mg_http_parse_range_header(range_hdr, &r1, &r2) > 0) { + status_code = 206; + fseeko(pd->file.fp, r1, SEEK_SET); + pd->file.cl = r2 > r1 ? r2 - r1 + 1 : pd->file.cl - r1; + } + mg_printf(nc, "HTTP/1.1 %d OK\r\nContent-Length: 0\r\n\r\n", status_code); + /* Remove HTTP request from the mbuf, leave only payload */ + mbuf_remove(&nc->recv_mbuf, hm->message.len - hm->body.len); + mg_http_transfer_file_data(nc); + } +} + +#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBDAV */ diff --git a/src/http_websocket.c b/src/http_websocket.c new file mode 100644 index 00000000..fcffd495 --- /dev/null +++ b/src/http_websocket.c @@ -0,0 +1,517 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET + +#include "common/sha1.h" + +#ifndef MG_WEBSOCKET_PING_INTERVAL_SECONDS +#define MG_WEBSOCKET_PING_INTERVAL_SECONDS 5 +#endif + +#define FLAGS_MASK_FIN (1 << 7) +#define FLAGS_MASK_OP 0x0f + +static int mg_is_ws_fragment(unsigned char flags) { + return (flags & FLAGS_MASK_FIN) == 0 || + (flags & FLAGS_MASK_OP) == WEBSOCKET_OP_CONTINUE; +} + +static int mg_is_ws_first_fragment(unsigned char flags) { + return (flags & FLAGS_MASK_FIN) == 0 && + (flags & FLAGS_MASK_OP) != WEBSOCKET_OP_CONTINUE; +} + +static int mg_is_ws_control_frame(unsigned char flags) { + unsigned char op = (flags & FLAGS_MASK_OP); + return op == WEBSOCKET_OP_CLOSE || op == WEBSOCKET_OP_PING || + op == WEBSOCKET_OP_PONG; +} + +static void mg_handle_incoming_websocket_frame(struct mg_connection *nc, + struct websocket_message *wsm) { + if (wsm->flags & 0x8) { + mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_CONTROL_FRAME, wsm); + } else { + mg_call(nc, nc->handler, nc->user_data, MG_EV_WEBSOCKET_FRAME, wsm); + } +} + +static struct mg_ws_proto_data *mg_ws_get_proto_data(struct mg_connection *nc) { + struct mg_http_proto_data *htd = mg_http_get_proto_data(nc); + return (htd != NULL ? &htd->ws_data : NULL); +} + +/* + * Sends a Close websocket frame with the given data, and closes the underlying + * connection. If `len` is ~0, strlen(data) is used. + */ +static void mg_ws_close(struct mg_connection *nc, const void *data, + size_t len) { + if ((int) len == ~0) { + len = strlen((const char *) data); + } + mg_send_websocket_frame(nc, WEBSOCKET_OP_CLOSE, data, len); + nc->flags |= MG_F_SEND_AND_CLOSE; +} + +static int mg_deliver_websocket_data(struct mg_connection *nc) { + /* Using unsigned char *, cause of integer arithmetic below */ + uint64_t i, data_len = 0, frame_len = 0, new_data_len = nc->recv_mbuf.len, + len, mask_len = 0, header_len = 0; + struct mg_ws_proto_data *wsd = mg_ws_get_proto_data(nc); + unsigned char *new_data = (unsigned char *) nc->recv_mbuf.buf, + *e = (unsigned char *) nc->recv_mbuf.buf + nc->recv_mbuf.len; + uint8_t flags; + int ok, reass; + + if (wsd->reass_len > 0) { + /* + * We already have some previously received data which we need to + * reassemble and deliver to the client code when we get the final + * fragment. + * + * NOTE: it doesn't mean that the current message must be a continuation: + * it might be a control frame (Close, Ping or Pong), which should be + * handled without breaking the fragmented message. + */ + + size_t existing_len = wsd->reass_len; + assert(new_data_len >= existing_len); + + new_data += existing_len; + new_data_len -= existing_len; + } + + flags = new_data[0]; + + reass = new_data_len > 0 && mg_is_ws_fragment(flags) && + !(nc->flags & MG_F_WEBSOCKET_NO_DEFRAG); + + if (reass && mg_is_ws_control_frame(flags)) { + /* + * Control frames can't be fragmented, so if we encounter fragmented + * control frame, close connection immediately. + */ + mg_ws_close(nc, "fragmented control frames are illegal", ~0); + return 0; + } else if (new_data_len > 0 && !reass && !mg_is_ws_control_frame(flags) && + wsd->reass_len > 0) { + /* + * When in the middle of a fragmented message, only the continuations + * and control frames are allowed. + */ + mg_ws_close(nc, "non-continuation in the middle of a fragmented message", + ~0); + return 0; + } + + if (new_data_len >= 2) { + len = new_data[1] & 0x7f; + mask_len = new_data[1] & FLAGS_MASK_FIN ? 4 : 0; + if (len < 126 && new_data_len >= mask_len) { + data_len = len; + header_len = 2 + mask_len; + } else if (len == 126 && new_data_len >= 4 + mask_len) { + header_len = 4 + mask_len; + data_len = ntohs(*(uint16_t *) &new_data[2]); + } else if (new_data_len >= 10 + mask_len) { + header_len = 10 + mask_len; + data_len = (((uint64_t) ntohl(*(uint32_t *) &new_data[2])) << 32) + + ntohl(*(uint32_t *) &new_data[6]); + } + } + + frame_len = header_len + data_len; + ok = (frame_len > 0 && frame_len <= new_data_len); + + /* Check for overflow */ + if (frame_len < header_len || frame_len < data_len) { + ok = 0; + mg_ws_close(nc, "overflowed message", ~0); + } + + if (ok) { + size_t cleanup_len = 0; + struct websocket_message wsm; + + wsm.size = (size_t) data_len; + wsm.data = new_data + header_len; + wsm.flags = flags; + + /* Apply mask if necessary */ + if (mask_len > 0) { + for (i = 0; i < data_len; i++) { + new_data[i + header_len] ^= (new_data + header_len - mask_len)[i % 4]; + } + } + + if (reass) { + /* This is a message fragment */ + + if (mg_is_ws_first_fragment(flags)) { + /* + * On the first fragmented frame, skip the first byte (op) and also + * reset size to 1 (op), it'll be incremented with the data len below. + */ + new_data += 1; + wsd->reass_len = 1 /* op */; + } + + /* Append this frame to the reassembled buffer */ + memmove(new_data, wsm.data, e - wsm.data); + wsd->reass_len += wsm.size; + nc->recv_mbuf.len -= wsm.data - new_data; + + if (flags & FLAGS_MASK_FIN) { + /* On last fragmented frame - call user handler and remove data */ + wsm.flags = FLAGS_MASK_FIN | nc->recv_mbuf.buf[0]; + wsm.data = (unsigned char *) nc->recv_mbuf.buf + 1 /* op */; + wsm.size = wsd->reass_len - 1 /* op */; + cleanup_len = wsd->reass_len; + wsd->reass_len = 0; + + /* Pass reassembled message to the client code. */ + mg_handle_incoming_websocket_frame(nc, &wsm); + mbuf_remove(&nc->recv_mbuf, cleanup_len); /* Cleanup frame */ + } + } else { + /* + * This is a complete message, not a fragment. It might happen in between + * of a fragmented message (in this case, WebSocket protocol requires + * current message to be a control frame). + */ + cleanup_len = (size_t) frame_len; + + /* First of all, check if we need to react on a control frame. */ + switch (flags & FLAGS_MASK_OP) { + case WEBSOCKET_OP_PING: + mg_send_websocket_frame(nc, WEBSOCKET_OP_PONG, wsm.data, wsm.size); + break; + + case WEBSOCKET_OP_CLOSE: + mg_ws_close(nc, wsm.data, wsm.size); + break; + } + + /* Pass received message to the client code. */ + mg_handle_incoming_websocket_frame(nc, &wsm); + + /* Cleanup frame */ + memmove(nc->recv_mbuf.buf + wsd->reass_len, + nc->recv_mbuf.buf + wsd->reass_len + cleanup_len, + nc->recv_mbuf.len - wsd->reass_len - cleanup_len); + nc->recv_mbuf.len -= cleanup_len; + } + } + + return ok; +} + +struct ws_mask_ctx { + size_t pos; /* zero means unmasked */ + uint32_t mask; +}; + +static uint32_t mg_ws_random_mask(void) { + uint32_t mask; +/* + * The spec requires WS client to generate hard to + * guess mask keys. From RFC6455, Section 5.3: + * + * The unpredictability of the masking key is essential to prevent + * authors of malicious applications from selecting the bytes that appear on + * the wire. + * + * Hence this feature is essential when the actual end user of this API + * is untrusted code that wouldn't have access to a lower level net API + * anyway (e.g. web browsers). Hence this feature is low prio for most + * mongoose use cases and thus can be disabled, e.g. when porting to a platform + * that lacks rand(). + */ +#if MG_DISABLE_WS_RANDOM_MASK + mask = 0xefbeadde; /* generated with a random number generator, I swear */ +#else + if (sizeof(long) >= 4) { + mask = (uint32_t) rand(); + } else if (sizeof(long) == 2) { + mask = (uint32_t) rand() << 16 | (uint32_t) rand(); + } +#endif + return mask; +} + +static void mg_send_ws_header(struct mg_connection *nc, int op, size_t len, + struct ws_mask_ctx *ctx) { + int header_len; + unsigned char header[10]; + + header[0] = + (op & WEBSOCKET_DONT_FIN ? 0x0 : FLAGS_MASK_FIN) | (op & FLAGS_MASK_OP); + if (len < 126) { + header[1] = (unsigned char) len; + header_len = 2; + } else if (len < 65535) { + uint16_t tmp = htons((uint16_t) len); + header[1] = 126; + memcpy(&header[2], &tmp, sizeof(tmp)); + header_len = 4; + } else { + uint32_t tmp; + header[1] = 127; + tmp = htonl((uint32_t)((uint64_t) len >> 32)); + memcpy(&header[2], &tmp, sizeof(tmp)); + tmp = htonl((uint32_t)(len & 0xffffffff)); + memcpy(&header[6], &tmp, sizeof(tmp)); + header_len = 10; + } + + /* client connections enable masking */ + if (nc->listener == NULL) { + header[1] |= 1 << 7; /* set masking flag */ + mg_send(nc, header, header_len); + ctx->mask = mg_ws_random_mask(); + mg_send(nc, &ctx->mask, sizeof(ctx->mask)); + ctx->pos = nc->send_mbuf.len; + } else { + mg_send(nc, header, header_len); + ctx->pos = 0; + } +} + +static void mg_ws_mask_frame(struct mbuf *mbuf, struct ws_mask_ctx *ctx) { + size_t i; + if (ctx->pos == 0) return; + for (i = 0; i < (mbuf->len - ctx->pos); i++) { + mbuf->buf[ctx->pos + i] ^= ((char *) &ctx->mask)[i % 4]; + } +} + +void mg_send_websocket_frame(struct mg_connection *nc, int op, const void *data, + size_t len) { + struct ws_mask_ctx ctx; + DBG(("%p %d %d", nc, op, (int) len)); + mg_send_ws_header(nc, op, len, &ctx); + mg_send(nc, data, len); + + mg_ws_mask_frame(&nc->send_mbuf, &ctx); + + if (op == WEBSOCKET_OP_CLOSE) { + nc->flags |= MG_F_SEND_AND_CLOSE; + } +} + +void mg_send_websocket_framev(struct mg_connection *nc, int op, + const struct mg_str *strv, int strvcnt) { + struct ws_mask_ctx ctx; + int i; + int len = 0; + for (i = 0; i < strvcnt; i++) { + len += strv[i].len; + } + + mg_send_ws_header(nc, op, len, &ctx); + + for (i = 0; i < strvcnt; i++) { + mg_send(nc, strv[i].p, strv[i].len); + } + + mg_ws_mask_frame(&nc->send_mbuf, &ctx); + + if (op == WEBSOCKET_OP_CLOSE) { + nc->flags |= MG_F_SEND_AND_CLOSE; + } +} + +void mg_printf_websocket_frame(struct mg_connection *nc, int op, + const char *fmt, ...) { + char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem; + va_list ap; + int len; + + va_start(ap, fmt); + if ((len = mg_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) { + mg_send_websocket_frame(nc, op, buf, len); + } + va_end(ap); + + if (buf != mem && buf != NULL) { + MG_FREE(buf); + } +} + +MG_INTERNAL void mg_ws_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + mg_call(nc, nc->handler, nc->user_data, ev, ev_data); + + switch (ev) { + case MG_EV_RECV: + do { + } while (mg_deliver_websocket_data(nc)); + break; + case MG_EV_POLL: + /* Ping idle websocket connections */ + { + time_t now = *(time_t *) ev_data; + if (nc->flags & MG_F_IS_WEBSOCKET && + now > nc->last_io_time + MG_WEBSOCKET_PING_INTERVAL_SECONDS) { + mg_send_websocket_frame(nc, WEBSOCKET_OP_PING, "", 0); + } + } + break; + default: + break; + } +#if MG_ENABLE_CALLBACK_USERDATA + (void) user_data; +#endif +} + +#ifndef MG_EXT_SHA1 +void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest) { + size_t i; + cs_sha1_ctx sha_ctx; + cs_sha1_init(&sha_ctx); + for (i = 0; i < num_msgs; i++) { + cs_sha1_update(&sha_ctx, msgs[i], msg_lens[i]); + } + cs_sha1_final(digest, &sha_ctx); +} +#else +extern void mg_hash_sha1_v(size_t num_msgs, const uint8_t *msgs[], + const size_t *msg_lens, uint8_t *digest); +#endif + +MG_INTERNAL void mg_ws_handshake(struct mg_connection *nc, + const struct mg_str *key, + struct http_message *hm) { + static const char *magic = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; + const uint8_t *msgs[2] = {(const uint8_t *) key->p, (const uint8_t *) magic}; + const size_t msg_lens[2] = {key->len, 36}; + unsigned char sha[20]; + char b64_sha[30]; + struct mg_str *s; + + mg_hash_sha1_v(2, msgs, msg_lens, sha); + mg_base64_encode(sha, sizeof(sha), b64_sha); + mg_printf(nc, "%s", + "HTTP/1.1 101 Switching Protocols\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n"); + + s = mg_get_http_header(hm, "Sec-WebSocket-Protocol"); + if (s != NULL) { + mg_printf(nc, "Sec-WebSocket-Protocol: %.*s\r\n", (int) s->len, s->p); + } + mg_printf(nc, "Sec-WebSocket-Accept: %s%s", b64_sha, "\r\n\r\n"); + + DBG(("%p %.*s %s", nc, (int) key->len, key->p, b64_sha)); +} + +void mg_send_websocket_handshake2(struct mg_connection *nc, const char *path, + const char *host, const char *protocol, + const char *extra_headers) { + mg_send_websocket_handshake3(nc, path, host, protocol, extra_headers, NULL, + NULL); +} + +void mg_send_websocket_handshake3(struct mg_connection *nc, const char *path, + const char *host, const char *protocol, + const char *extra_headers, const char *user, + const char *pass) { + mg_send_websocket_handshake3v(nc, mg_mk_str(path), mg_mk_str(host), + mg_mk_str(protocol), mg_mk_str(extra_headers), + mg_mk_str(user), mg_mk_str(pass)); +} + +void mg_send_websocket_handshake3v(struct mg_connection *nc, + const struct mg_str path, + const struct mg_str host, + const struct mg_str protocol, + const struct mg_str extra_headers, + const struct mg_str user, + const struct mg_str pass) { + struct mbuf auth; + char key[25]; + uint32_t nonce[4]; + nonce[0] = mg_ws_random_mask(); + nonce[1] = mg_ws_random_mask(); + nonce[2] = mg_ws_random_mask(); + nonce[3] = mg_ws_random_mask(); + mg_base64_encode((unsigned char *) &nonce, sizeof(nonce), key); + + mbuf_init(&auth, 0); + if (user.len > 0) { + mg_basic_auth_header(user, pass, &auth); + } + + /* + * NOTE: the (auth.buf == NULL ? "" : auth.buf) is because cc3200 libc is + * broken: it doesn't like zero length to be passed to %.*s + * i.e. sprintf("f%.*so", (int)0, NULL), yields `f\0o`. + * because it handles NULL specially (and incorrectly). + */ + mg_printf(nc, + "GET %.*s HTTP/1.1\r\n" + "Upgrade: websocket\r\n" + "Connection: Upgrade\r\n" + "%.*s" + "Sec-WebSocket-Version: 13\r\n" + "Sec-WebSocket-Key: %s\r\n", + (int) path.len, path.p, (int) auth.len, + (auth.buf == NULL ? "" : auth.buf), key); + + /* TODO(mkm): take default hostname from http proto data if host == NULL */ + if (host.len > 0) { + int host_len = (int) (path.p - host.p); /* Account for possible :PORT */ + mg_printf(nc, "Host: %.*s\r\n", host_len, host.p); + } + if (protocol.len > 0) { + mg_printf(nc, "Sec-WebSocket-Protocol: %.*s\r\n", (int) protocol.len, + protocol.p); + } + if (extra_headers.len > 0) { + mg_printf(nc, "%.*s", (int) extra_headers.len, extra_headers.p); + } + mg_printf(nc, "\r\n"); + + mbuf_free(&auth); +} + +void mg_send_websocket_handshake(struct mg_connection *nc, const char *path, + const char *extra_headers) { + struct mg_str null_str = MG_NULL_STR; + mg_send_websocket_handshake3v( + nc, mg_mk_str(path), null_str /* host */, null_str /* protocol */, + mg_mk_str(extra_headers), null_str /* user */, null_str /* pass */); +} + +struct mg_connection *mg_connect_ws_opt( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + struct mg_connect_opts opts, const char *url, const char *protocol, + const char *extra_headers) { + struct mg_str null_str = MG_NULL_STR; + struct mg_str host = MG_NULL_STR, path = MG_NULL_STR, user_info = MG_NULL_STR; + struct mg_connection *nc = + mg_connect_http_base(mgr, MG_CB(ev_handler, user_data), opts, "http", + "ws", "https", "wss", url, &path, &user_info, &host); + if (nc != NULL) { + mg_send_websocket_handshake3v(nc, path, host, mg_mk_str(protocol), + mg_mk_str(extra_headers), user_info, + null_str); + } + return nc; +} + +struct mg_connection *mg_connect_ws( + struct mg_mgr *mgr, MG_CB(mg_event_handler_t ev_handler, void *user_data), + const char *url, const char *protocol, const char *extra_headers) { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_connect_ws_opt(mgr, MG_CB(ev_handler, user_data), opts, url, + protocol, extra_headers); +} +#endif /* MG_ENABLE_HTTP && MG_ENABLE_HTTP_WEBSOCKET */ diff --git a/src/internal.h b/src/internal.h new file mode 100644 index 00000000..d7a2625c --- /dev/null +++ b/src/internal.h @@ -0,0 +1,157 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_INTERNAL_H_ +#define CS_MONGOOSE_SRC_INTERNAL_H_ + +#include "common/mg_mem.h" + +#ifndef MBUF_REALLOC +#define MBUF_REALLOC MG_REALLOC +#endif + +#ifndef MBUF_FREE +#define MBUF_FREE MG_FREE +#endif + +#define MG_SET_PTRPTR(_ptr, _v) \ + do { \ + if (_ptr) *(_ptr) = _v; \ + } while (0) + +#ifndef MG_INTERNAL +#define MG_INTERNAL static +#endif + +#ifdef PICOTCP +#define NO_LIBC +#define MG_DISABLE_PFS +#endif + +#include "common/cs_dbg.h" +#include "mongoose/src/http.h" +#include "mongoose/src/net.h" + +#define MG_CTL_MSG_MESSAGE_SIZE 8192 + +/* internals that need to be accessible in unit tests */ +MG_INTERNAL struct mg_connection *mg_do_connect(struct mg_connection *nc, + int proto, + union socket_address *sa); + +MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, + int *proto, char *host, size_t host_len); +MG_INTERNAL void mg_call(struct mg_connection *nc, + mg_event_handler_t ev_handler, void *user_data, int ev, + void *ev_data); +void mg_forward(struct mg_connection *from, struct mg_connection *to); +MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c); +MG_INTERNAL void mg_remove_conn(struct mg_connection *c); +MG_INTERNAL struct mg_connection *mg_create_connection( + struct mg_mgr *mgr, mg_event_handler_t callback, + struct mg_add_sock_opts opts); +#ifdef _WIN32 +/* Retur value is the same as for MultiByteToWideChar. */ +int to_wchar(const char *path, wchar_t *wbuf, size_t wbuf_len); +#endif + +struct ctl_msg { + mg_event_handler_t callback; + char message[MG_CTL_MSG_MESSAGE_SIZE]; +}; + +#if MG_ENABLE_MQTT +struct mg_mqtt_message; + +#define MG_MQTT_ERROR_INCOMPLETE_MSG -1 +#define MG_MQTT_ERROR_MALFORMED_MSG -2 + +MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm); +#endif + +/* Forward declarations for testing. */ +extern void *(*test_malloc)(size_t size); +extern void *(*test_calloc)(size_t count, size_t size); + +#ifndef MIN +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#endif + +#if MG_ENABLE_HTTP +struct mg_serve_http_opts; + +/* + * Reassemble the content of the buffer (buf, blen) which should be + * in the HTTP chunked encoding, by collapsing data chunks to the + * beginning of the buffer. + * + * If chunks get reassembled, modify hm->body to point to the reassembled + * body and fire MG_EV_HTTP_CHUNK event. If handler sets MG_F_DELETE_CHUNK + * in nc->flags, delete reassembled body from the mbuf. + * + * Return reassembled body size. + */ +MG_INTERNAL size_t mg_handle_chunked(struct mg_connection *nc, + struct http_message *hm, char *buf, + size_t blen); + +#if MG_ENABLE_FILESYSTEM +MG_INTERNAL int mg_uri_to_local_path(struct http_message *hm, + const struct mg_serve_http_opts *opts, + char **local_path, + struct mg_str *remainder); +MG_INTERNAL time_t mg_parse_date_string(const char *datetime); +MG_INTERNAL int mg_is_not_modified(struct http_message *hm, cs_stat_t *st); +#endif +#if MG_ENABLE_HTTP_CGI +MG_INTERNAL void mg_handle_cgi(struct mg_connection *nc, const char *prog, + const struct mg_str *path_info, + const struct http_message *hm, + const struct mg_serve_http_opts *opts); +struct mg_http_proto_data_cgi; +MG_INTERNAL void mg_http_free_proto_data_cgi(struct mg_http_proto_data_cgi *d); +#endif +#if MG_ENABLE_HTTP_SSI +MG_INTERNAL void mg_handle_ssi_request(struct mg_connection *nc, + struct http_message *hm, + const char *path, + const struct mg_serve_http_opts *opts); +#endif +#if MG_ENABLE_HTTP_WEBDAV +MG_INTERNAL int mg_is_dav_request(const struct mg_str *s); +MG_INTERNAL void mg_handle_propfind(struct mg_connection *nc, const char *path, + cs_stat_t *stp, struct http_message *hm, + struct mg_serve_http_opts *opts); +MG_INTERNAL void mg_handle_lock(struct mg_connection *nc, const char *path); +MG_INTERNAL void mg_handle_mkcol(struct mg_connection *nc, const char *path, + struct http_message *hm); +MG_INTERNAL void mg_handle_move(struct mg_connection *c, + const struct mg_serve_http_opts *opts, + const char *path, struct http_message *hm); +MG_INTERNAL void mg_handle_delete(struct mg_connection *nc, + const struct mg_serve_http_opts *opts, + const char *path); +MG_INTERNAL void mg_handle_put(struct mg_connection *nc, const char *path, + struct http_message *hm); +#endif +#if MG_ENABLE_HTTP_WEBSOCKET +MG_INTERNAL void mg_ws_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)); +MG_INTERNAL void mg_ws_handshake(struct mg_connection *nc, + const struct mg_str *key, + struct http_message *); +#endif +#endif /* MG_ENABLE_HTTP */ + +MG_INTERNAL int mg_get_errno(void); + +MG_INTERNAL void mg_close_conn(struct mg_connection *conn); + +#if MG_ENABLE_SNTP +MG_INTERNAL int mg_sntp_parse_reply(const char *buf, int len, + struct mg_sntp_message *msg); +#endif + +#endif /* CS_MONGOOSE_SRC_INTERNAL_H_ */ diff --git a/src/modules.mk b/src/modules.mk new file mode 100644 index 00000000..f68f4adb --- /dev/null +++ b/src/modules.mk @@ -0,0 +1,107 @@ +COMMON = ../../common + +HEADERS = common.h \ + $(COMMON)/platform.h \ + $(COMMON)/platforms/platform_windows.h \ + $(COMMON)/platforms/platform_unix.h \ + $(COMMON)/platforms/platform_esp32.h \ + $(COMMON)/platforms/platform_esp8266.h \ + $(COMMON)/platforms/platform_cc3100.h \ + $(COMMON)/platforms/platform_cc3200.h \ + $(COMMON)/platforms/platform_msp432.h \ + $(COMMON)/platforms/platform_tm4c129.h \ + $(COMMON)/platforms/platform_mbed.h \ + $(COMMON)/platforms/platform_nrf51.h \ + $(COMMON)/platforms/platform_nrf52.h \ + $(COMMON)/platforms/simplelink/cs_simplelink.h \ + $(COMMON)/platforms/platform_wince.h \ + $(COMMON)/platforms/platform_nxp_lpc.h \ + $(COMMON)/platforms/platform_nxp_kinetis.h \ + $(COMMON)/platforms/platform_pic32.h \ + $(COMMON)/platforms/platform_stm32.h \ + $(COMMON)/platforms/lwip/mg_lwip.h \ + $(COMMON)/cs_md5.h \ + $(COMMON)/cs_sha1.h \ + $(COMMON)/cs_time.h \ + $(COMMON)/mg_str.h \ + $(COMMON)/mbuf.h \ + $(COMMON)/cs_base64.h \ + $(COMMON)/str_util.h \ + $(COMMON)/queue.h \ + features.h \ + net_if.h \ + ssl_if.h \ + net.h \ + uri.h \ + util.h \ + http.h \ + http_server.h \ + http_client.h \ + mqtt.h \ + mqtt_server.h \ + dns.h \ + dns_server.h \ + resolv.h \ + coap.h \ + sntp.h \ + socks.h + +SOURCES = $(COMMON)/mg_mem.h \ + $(COMMON)/cs_base64.c \ + $(COMMON)/cs_dbg.h \ + $(COMMON)/cs_dbg.c \ + $(COMMON)/cs_dirent.h \ + $(COMMON)/cs_dirent.c \ + $(COMMON)/cs_time.c \ + $(COMMON)/cs_endian.h \ + $(COMMON)/cs_md5.c \ + $(COMMON)/cs_sha1.c \ + $(COMMON)/mbuf.c \ + $(COMMON)/mg_str.c \ + $(COMMON)/str_util.c \ + tun.h \ + net.c \ + net_if_socket.h \ + net_if_tun.h \ + net_if_socks.h \ + net_if.c \ + net_if_socket.c \ + net_if_socks.c \ + net_if_tun.c \ + ssl_if_openssl.c \ + ssl_if_mbedtls.c \ + uri.c \ + http.c \ + http_cgi.c \ + http_ssi.c \ + http_webdav.c \ + http_websocket.c \ + util.c \ + mqtt.c \ + mqtt_server.c \ + dns.c \ + dns_server.c \ + resolv.c \ + coap.c \ + tun.c \ + sntp.c \ + socks.c \ + $(COMMON)/platforms/cc3200/cc3200_libc.c \ + $(COMMON)/platforms/msp432/msp432_libc.c \ + $(COMMON)/platforms/nrf5/nrf5_libc.c \ + $(COMMON)/platforms/simplelink/sl_fs_slfs.h \ + $(COMMON)/platforms/simplelink/sl_fs_slfs.c \ + $(COMMON)/platforms/simplelink/sl_fs.c \ + $(COMMON)/platforms/simplelink/sl_socket.c \ + $(COMMON)/platforms/simplelink/sl_mg_task.c \ + $(COMMON)/platforms/simplelink/sl_net_if.h \ + $(COMMON)/platforms/simplelink/sl_net_if.c \ + $(COMMON)/platforms/simplelink/sl_ssl_if.c \ + $(COMMON)/platforms/lwip/mg_lwip_net_if.h \ + $(COMMON)/platforms/lwip/mg_lwip_net_if.c \ + $(COMMON)/platforms/lwip/mg_lwip_ev_mgr.c \ + $(COMMON)/platforms/lwip/mg_lwip_ssl_if.c \ + $(COMMON)/platforms/wince/wince_libc.c \ + $(COMMON)/platforms/pic32/pic32_net_if.h \ + $(COMMON)/platforms/pic32/pic32_net_if.c \ + $(COMMON)/platforms/windows/windows_direct.c diff --git a/src/mqtt.c b/src/mqtt.c new file mode 100644 index 00000000..216c00bf --- /dev/null +++ b/src/mqtt.c @@ -0,0 +1,466 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_MQTT + +#include + +#include "mongoose/src/internal.h" +#include "mongoose/src/mqtt.h" + +static uint16_t getu16(const char *p) { + const uint8_t *up = (const uint8_t *) p; + return (up[0] << 8) + up[1]; +} + +static const char *scanto(const char *p, struct mg_str *s) { + s->len = getu16(p); + s->p = p + 2; + return s->p + s->len; +} + +MG_INTERNAL int parse_mqtt(struct mbuf *io, struct mg_mqtt_message *mm) { + uint8_t header; + size_t len = 0, len_len = 0; + const char *p, *end; + unsigned char lc = 0; + int cmd; + + if (io->len < 2) return MG_MQTT_ERROR_INCOMPLETE_MSG; + header = io->buf[0]; + cmd = header >> 4; + + /* decode mqtt variable length */ + len = len_len = 0; + p = io->buf + 1; + while ((size_t)(p - io->buf) < io->len) { + lc = *((const unsigned char *) p++); + len += (lc & 0x7f) << 7 * len_len; + len_len++; + if (!(lc & 0x80)) break; + if (len_len > 4) return MG_MQTT_ERROR_MALFORMED_MSG; + } + + end = p + len; + if (lc & 0x80 || len > (io->len - (p - io->buf))) { + return MG_MQTT_ERROR_INCOMPLETE_MSG; + } + + mm->cmd = cmd; + mm->qos = MG_MQTT_GET_QOS(header); + + switch (cmd) { + case MG_MQTT_CMD_CONNECT: { + p = scanto(p, &mm->protocol_name); + if (p > end - 4) return MG_MQTT_ERROR_MALFORMED_MSG; + mm->protocol_version = *(uint8_t *) p++; + mm->connect_flags = *(uint8_t *) p++; + mm->keep_alive_timer = getu16(p); + p += 2; + if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG; + p = scanto(p, &mm->client_id); + if (p > end) return MG_MQTT_ERROR_MALFORMED_MSG; + if (mm->connect_flags & MG_MQTT_HAS_WILL) { + if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG; + p = scanto(p, &mm->will_topic); + } + if (mm->connect_flags & MG_MQTT_HAS_WILL) { + if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG; + p = scanto(p, &mm->will_message); + } + if (mm->connect_flags & MG_MQTT_HAS_USER_NAME) { + if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG; + p = scanto(p, &mm->user_name); + } + if (mm->connect_flags & MG_MQTT_HAS_PASSWORD) { + if (p >= end) return MG_MQTT_ERROR_MALFORMED_MSG; + p = scanto(p, &mm->password); + } + if (p != end) return MG_MQTT_ERROR_MALFORMED_MSG; + + LOG(LL_DEBUG, + ("%d %2x %d proto [%.*s] client_id [%.*s] will_topic [%.*s] " + "will_msg [%.*s] user_name [%.*s] password [%.*s]", + (int) len, (int) mm->connect_flags, (int) mm->keep_alive_timer, + (int) mm->protocol_name.len, mm->protocol_name.p, + (int) mm->client_id.len, mm->client_id.p, (int) mm->will_topic.len, + mm->will_topic.p, (int) mm->will_message.len, mm->will_message.p, + (int) mm->user_name.len, mm->user_name.p, (int) mm->password.len, + mm->password.p)); + break; + } + case MG_MQTT_CMD_CONNACK: + if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG; + mm->connack_ret_code = p[1]; + break; + case MG_MQTT_CMD_PUBACK: + case MG_MQTT_CMD_PUBREC: + case MG_MQTT_CMD_PUBREL: + case MG_MQTT_CMD_PUBCOMP: + case MG_MQTT_CMD_SUBACK: + mm->message_id = getu16(p); + break; + case MG_MQTT_CMD_PUBLISH: { + p = scanto(p, &mm->topic); + if (p > end) return MG_MQTT_ERROR_MALFORMED_MSG; + if (mm->qos > 0) { + if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG; + mm->message_id = getu16(p); + p += 2; + } + mm->payload.p = p; + mm->payload.len = end - p; + break; + } + case MG_MQTT_CMD_SUBSCRIBE: + if (end - p < 2) return MG_MQTT_ERROR_MALFORMED_MSG; + mm->message_id = getu16(p); + p += 2; + /* + * topic expressions are left in the payload and can be parsed with + * `mg_mqtt_next_subscribe_topic` + */ + mm->payload.p = p; + mm->payload.len = end - p; + break; + default: + /* Unhandled command */ + break; + } + + mm->len = end - io->buf; + return mm->len; +} + +static void mqtt_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct mbuf *io = &nc->recv_mbuf; + struct mg_mqtt_message mm; + memset(&mm, 0, sizeof(mm)); + + nc->handler(nc, ev, ev_data MG_UD_ARG(user_data)); + + switch (ev) { + case MG_EV_ACCEPT: + if (nc->proto_data == NULL) mg_set_protocol_mqtt(nc); + break; + case MG_EV_RECV: { + /* There can be multiple messages in the buffer, process them all. */ + while (1) { + int len = parse_mqtt(io, &mm); + if (len < 0) { + if (len == MG_MQTT_ERROR_MALFORMED_MSG) { + /* Protocol error. */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else if (len == MG_MQTT_ERROR_INCOMPLETE_MSG) { + /* Not fully buffered, let's check if we have a chance to get more + * data later */ + if (nc->recv_mbuf_limit > 0 && + nc->recv_mbuf.len >= nc->recv_mbuf_limit) { + LOG(LL_ERROR, ("%p recv buffer (%lu bytes) exceeds the limit " + "%lu bytes, and not drained, closing", + nc, (unsigned long) nc->recv_mbuf.len, + (unsigned long) nc->recv_mbuf_limit)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + } else { + /* Should never be here */ + LOG(LL_ERROR, ("%p invalid len: %d, closing", nc, len)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + } + + nc->handler(nc, MG_MQTT_EVENT_BASE + mm.cmd, &mm MG_UD_ARG(user_data)); + mbuf_remove(io, len); + } + break; + } + case MG_EV_POLL: { + struct mg_mqtt_proto_data *pd = + (struct mg_mqtt_proto_data *) nc->proto_data; + double now = mg_time(); + if (pd->keep_alive > 0 && pd->last_control_time > 0 && + (now - pd->last_control_time) > pd->keep_alive) { + LOG(LL_DEBUG, ("Send PINGREQ")); + mg_mqtt_ping(nc); + } + break; + } + } +} + +static void mg_mqtt_proto_data_destructor(void *proto_data) { + MG_FREE(proto_data); +} + +int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic) { + /* TODO(mkm): implement real matching */ + if (memchr(exp.p, '#', exp.len)) { + /* exp `foo/#` will become `foo/` */ + exp.len -= 1; + /* + * topic should be longer than the expression: e.g. topic `foo/bar` does + * match `foo/#`, but neither `foo` nor `foo/` do. + */ + if (topic.len <= exp.len) { + return 0; + } + + /* Truncate topic so that it'll pass the next length check */ + topic.len = exp.len; + } + if (topic.len != exp.len) { + return 0; + } + return strncmp(topic.p, exp.p, exp.len) == 0; +} + +int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic) { + return mg_mqtt_match_topic_expression(mg_mk_str(exp), topic); +} + +void mg_set_protocol_mqtt(struct mg_connection *nc) { + nc->proto_handler = mqtt_handler; + nc->proto_data = MG_CALLOC(1, sizeof(struct mg_mqtt_proto_data)); + nc->proto_data_destructor = mg_mqtt_proto_data_destructor; +} + +static void mg_mqtt_prepend_header(struct mg_connection *nc, uint8_t cmd, + uint8_t flags, size_t len) { + struct mg_mqtt_proto_data *pd = (struct mg_mqtt_proto_data *) nc->proto_data; + size_t off = nc->send_mbuf.len - len; + uint8_t header = cmd << 4 | (uint8_t) flags; + + uint8_t buf[1 + sizeof(size_t)]; + uint8_t *vlen = &buf[1]; + + assert(nc->send_mbuf.len >= len); + + buf[0] = header; + + /* mqtt variable length encoding */ + do { + *vlen = len % 0x80; + len /= 0x80; + if (len > 0) *vlen |= 0x80; + vlen++; + } while (len > 0); + + mbuf_insert(&nc->send_mbuf, off, buf, vlen - buf); + pd->last_control_time = mg_time(); +} + +void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id) { + static struct mg_send_mqtt_handshake_opts opts; + mg_send_mqtt_handshake_opt(nc, client_id, opts); +} + +void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id, + struct mg_send_mqtt_handshake_opts opts) { + uint16_t hlen, nlen, rem_len = 0; + struct mg_mqtt_proto_data *pd = (struct mg_mqtt_proto_data *) nc->proto_data; + + mg_send(nc, "\00\04MQTT\04", 7); + rem_len += 7; + + if (opts.user_name != NULL) { + opts.flags |= MG_MQTT_HAS_USER_NAME; + } + if (opts.password != NULL) { + opts.flags |= MG_MQTT_HAS_PASSWORD; + } + if (opts.will_topic != NULL && opts.will_message != NULL) { + opts.flags |= MG_MQTT_HAS_WILL; + } + if (opts.keep_alive == 0) { + opts.keep_alive = 60; + } + + mg_send(nc, &opts.flags, 1); + rem_len += 1; + + nlen = htons(opts.keep_alive); + mg_send(nc, &nlen, 2); + rem_len += 2; + + hlen = strlen(client_id); + nlen = htons((uint16_t) hlen); + mg_send(nc, &nlen, 2); + mg_send(nc, client_id, hlen); + rem_len += 2 + hlen; + + if (opts.flags & MG_MQTT_HAS_WILL) { + hlen = strlen(opts.will_topic); + nlen = htons((uint16_t) hlen); + mg_send(nc, &nlen, 2); + mg_send(nc, opts.will_topic, hlen); + rem_len += 2 + hlen; + + hlen = strlen(opts.will_message); + nlen = htons((uint16_t) hlen); + mg_send(nc, &nlen, 2); + mg_send(nc, opts.will_message, hlen); + rem_len += 2 + hlen; + } + + if (opts.flags & MG_MQTT_HAS_USER_NAME) { + hlen = strlen(opts.user_name); + nlen = htons((uint16_t) hlen); + mg_send(nc, &nlen, 2); + mg_send(nc, opts.user_name, hlen); + rem_len += 2 + hlen; + } + if (opts.flags & MG_MQTT_HAS_PASSWORD) { + hlen = strlen(opts.password); + nlen = htons((uint16_t) hlen); + mg_send(nc, &nlen, 2); + mg_send(nc, opts.password, hlen); + rem_len += 2 + hlen; + } + + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_CONNECT, 0, rem_len); + + if (pd != NULL) { + pd->keep_alive = opts.keep_alive; + } +} + +void mg_mqtt_publish(struct mg_connection *nc, const char *topic, + uint16_t message_id, int flags, const void *data, + size_t len) { + size_t old_len = nc->send_mbuf.len; + + uint16_t topic_len = htons((uint16_t) strlen(topic)); + uint16_t message_id_net = htons(message_id); + + mg_send(nc, &topic_len, 2); + mg_send(nc, topic, strlen(topic)); + if (MG_MQTT_GET_QOS(flags) > 0) { + mg_send(nc, &message_id_net, 2); + } + mg_send(nc, data, len); + + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PUBLISH, flags, + nc->send_mbuf.len - old_len); +} + +void mg_mqtt_subscribe(struct mg_connection *nc, + const struct mg_mqtt_topic_expression *topics, + size_t topics_len, uint16_t message_id) { + size_t old_len = nc->send_mbuf.len; + + uint16_t message_id_n = htons(message_id); + size_t i; + + mg_send(nc, (char *) &message_id_n, 2); + for (i = 0; i < topics_len; i++) { + uint16_t topic_len_n = htons((uint16_t) strlen(topics[i].topic)); + mg_send(nc, &topic_len_n, 2); + mg_send(nc, topics[i].topic, strlen(topics[i].topic)); + mg_send(nc, &topics[i].qos, 1); + } + + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_SUBSCRIBE, MG_MQTT_QOS(1), + nc->send_mbuf.len - old_len); +} + +int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg, + struct mg_str *topic, uint8_t *qos, int pos) { + unsigned char *buf = (unsigned char *) msg->payload.p + pos; + int new_pos; + + if ((size_t) pos >= msg->payload.len) return -1; + + topic->len = buf[0] << 8 | buf[1]; + topic->p = (char *) buf + 2; + new_pos = pos + 2 + topic->len + 1; + if ((size_t) new_pos > msg->payload.len) return -1; + *qos = buf[2 + topic->len]; + return new_pos; +} + +void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics, + size_t topics_len, uint16_t message_id) { + size_t old_len = nc->send_mbuf.len; + + uint16_t message_id_n = htons(message_id); + size_t i; + + mg_send(nc, (char *) &message_id_n, 2); + for (i = 0; i < topics_len; i++) { + uint16_t topic_len_n = htons((uint16_t) strlen(topics[i])); + mg_send(nc, &topic_len_n, 2); + mg_send(nc, topics[i], strlen(topics[i])); + } + + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_UNSUBSCRIBE, MG_MQTT_QOS(1), + nc->send_mbuf.len - old_len); +} + +void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code) { + uint8_t unused = 0; + mg_send(nc, &unused, 1); + mg_send(nc, &return_code, 1); + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_CONNACK, 0, 2); +} + +/* + * Sends a command which contains only a `message_id` and a QoS level of 1. + * + * Helper function. + */ +static void mg_send_mqtt_short_command(struct mg_connection *nc, uint8_t cmd, + uint16_t message_id) { + uint16_t message_id_net = htons(message_id); + uint8_t flags = (cmd == MG_MQTT_CMD_PUBREL ? 2 : 0); + mg_send(nc, &message_id_net, 2); + mg_mqtt_prepend_header(nc, cmd, flags, 2 /* len */); +} + +void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id) { + mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBACK, message_id); +} + +void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id) { + mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBREC, message_id); +} + +void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id) { + mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBREL, message_id); +} + +void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id) { + mg_send_mqtt_short_command(nc, MG_MQTT_CMD_PUBCOMP, message_id); +} + +void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len, + uint16_t message_id) { + size_t i; + uint16_t message_id_net = htons(message_id); + mg_send(nc, &message_id_net, 2); + for (i = 0; i < qoss_len; i++) { + mg_send(nc, &qoss[i], 1); + } + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_SUBACK, MG_MQTT_QOS(1), 2 + qoss_len); +} + +void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id) { + mg_send_mqtt_short_command(nc, MG_MQTT_CMD_UNSUBACK, message_id); +} + +void mg_mqtt_ping(struct mg_connection *nc) { + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PINGREQ, 0, 0); +} + +void mg_mqtt_pong(struct mg_connection *nc) { + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_PINGRESP, 0, 0); +} + +void mg_mqtt_disconnect(struct mg_connection *nc) { + mg_mqtt_prepend_header(nc, MG_MQTT_CMD_DISCONNECT, 0, 0); +} + +#endif /* MG_ENABLE_MQTT */ diff --git a/src/mqtt.h b/src/mqtt.h new file mode 100644 index 00000000..af161f79 --- /dev/null +++ b/src/mqtt.h @@ -0,0 +1,228 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +/* + * === MQTT API reference + */ + +#ifndef CS_MONGOOSE_SRC_MQTT_H_ +#define CS_MONGOOSE_SRC_MQTT_H_ + +#include "mongoose/src/net.h" + +struct mg_mqtt_message { + int cmd; + int qos; + int len; /* message length in the IO buffer */ + struct mg_str topic; + struct mg_str payload; + + uint8_t connack_ret_code; /* connack */ + uint16_t message_id; /* puback */ + + /* connect */ + uint8_t protocol_version; + uint8_t connect_flags; + uint16_t keep_alive_timer; + struct mg_str protocol_name; + struct mg_str client_id; + struct mg_str will_topic; + struct mg_str will_message; + struct mg_str user_name; + struct mg_str password; +}; + +struct mg_mqtt_topic_expression { + const char *topic; + uint8_t qos; +}; + +struct mg_send_mqtt_handshake_opts { + unsigned char flags; /* connection flags */ + uint16_t keep_alive; + const char *will_topic; + const char *will_message; + const char *user_name; + const char *password; +}; + +/* mg_mqtt_proto_data should be in header to allow external access to it */ +struct mg_mqtt_proto_data { + uint16_t keep_alive; + double last_control_time; +}; + +/* Message types */ +#define MG_MQTT_CMD_CONNECT 1 +#define MG_MQTT_CMD_CONNACK 2 +#define MG_MQTT_CMD_PUBLISH 3 +#define MG_MQTT_CMD_PUBACK 4 +#define MG_MQTT_CMD_PUBREC 5 +#define MG_MQTT_CMD_PUBREL 6 +#define MG_MQTT_CMD_PUBCOMP 7 +#define MG_MQTT_CMD_SUBSCRIBE 8 +#define MG_MQTT_CMD_SUBACK 9 +#define MG_MQTT_CMD_UNSUBSCRIBE 10 +#define MG_MQTT_CMD_UNSUBACK 11 +#define MG_MQTT_CMD_PINGREQ 12 +#define MG_MQTT_CMD_PINGRESP 13 +#define MG_MQTT_CMD_DISCONNECT 14 + +/* MQTT event types */ +#define MG_MQTT_EVENT_BASE 200 +#define MG_EV_MQTT_CONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNECT) +#define MG_EV_MQTT_CONNACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_CONNACK) +#define MG_EV_MQTT_PUBLISH (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBLISH) +#define MG_EV_MQTT_PUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBACK) +#define MG_EV_MQTT_PUBREC (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREC) +#define MG_EV_MQTT_PUBREL (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBREL) +#define MG_EV_MQTT_PUBCOMP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PUBCOMP) +#define MG_EV_MQTT_SUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBSCRIBE) +#define MG_EV_MQTT_SUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_SUBACK) +#define MG_EV_MQTT_UNSUBSCRIBE (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBSCRIBE) +#define MG_EV_MQTT_UNSUBACK (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_UNSUBACK) +#define MG_EV_MQTT_PINGREQ (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGREQ) +#define MG_EV_MQTT_PINGRESP (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_PINGRESP) +#define MG_EV_MQTT_DISCONNECT (MG_MQTT_EVENT_BASE + MG_MQTT_CMD_DISCONNECT) + +/* Message flags */ +#define MG_MQTT_RETAIN 0x1 +#define MG_MQTT_DUP 0x4 +#define MG_MQTT_QOS(qos) ((qos) << 1) +#define MG_MQTT_GET_QOS(flags) (((flags) &0x6) >> 1) +#define MG_MQTT_SET_QOS(flags, qos) (flags) = ((flags) & ~0x6) | ((qos) << 1) + +/* Connection flags */ +#define MG_MQTT_CLEAN_SESSION 0x02 +#define MG_MQTT_HAS_WILL 0x04 +#define MG_MQTT_WILL_RETAIN 0x20 +#define MG_MQTT_HAS_PASSWORD 0x40 +#define MG_MQTT_HAS_USER_NAME 0x80 +#define MG_MQTT_GET_WILL_QOS(flags) (((flags) &0x18) >> 3) +#define MG_MQTT_SET_WILL_QOS(flags, qos) \ + (flags) = ((flags) & ~0x18) | ((qos) << 3) + +/* CONNACK return codes */ +#define MG_EV_MQTT_CONNACK_ACCEPTED 0 +#define MG_EV_MQTT_CONNACK_UNACCEPTABLE_VERSION 1 +#define MG_EV_MQTT_CONNACK_IDENTIFIER_REJECTED 2 +#define MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE 3 +#define MG_EV_MQTT_CONNACK_BAD_AUTH 4 +#define MG_EV_MQTT_CONNACK_NOT_AUTHORIZED 5 + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * Attaches a built-in MQTT event handler to the given connection. + * + * The user-defined event handler will receive following extra events: + * + * - MG_EV_MQTT_CONNACK + * - MG_EV_MQTT_PUBLISH + * - MG_EV_MQTT_PUBACK + * - MG_EV_MQTT_PUBREC + * - MG_EV_MQTT_PUBREL + * - MG_EV_MQTT_PUBCOMP + * - MG_EV_MQTT_SUBACK + */ +void mg_set_protocol_mqtt(struct mg_connection *nc); + +/* Sends an MQTT handshake. */ +void mg_send_mqtt_handshake(struct mg_connection *nc, const char *client_id); + +/* Sends an MQTT handshake with optional parameters. */ +void mg_send_mqtt_handshake_opt(struct mg_connection *nc, const char *client_id, + struct mg_send_mqtt_handshake_opts); + +/* Publishes a message to a given topic. */ +void mg_mqtt_publish(struct mg_connection *nc, const char *topic, + uint16_t message_id, int flags, const void *data, + size_t len); + +/* Subscribes to a bunch of topics. */ +void mg_mqtt_subscribe(struct mg_connection *nc, + const struct mg_mqtt_topic_expression *topics, + size_t topics_len, uint16_t message_id); + +/* Unsubscribes from a bunch of topics. */ +void mg_mqtt_unsubscribe(struct mg_connection *nc, char **topics, + size_t topics_len, uint16_t message_id); + +/* Sends a DISCONNECT command. */ +void mg_mqtt_disconnect(struct mg_connection *nc); + +/* Sends a CONNACK command with a given `return_code`. */ +void mg_mqtt_connack(struct mg_connection *nc, uint8_t return_code); + +/* Sends a PUBACK command with a given `message_id`. */ +void mg_mqtt_puback(struct mg_connection *nc, uint16_t message_id); + +/* Sends a PUBREC command with a given `message_id`. */ +void mg_mqtt_pubrec(struct mg_connection *nc, uint16_t message_id); + +/* Sends a PUBREL command with a given `message_id`. */ +void mg_mqtt_pubrel(struct mg_connection *nc, uint16_t message_id); + +/* Sends a PUBCOMP command with a given `message_id`. */ +void mg_mqtt_pubcomp(struct mg_connection *nc, uint16_t message_id); + +/* + * Sends a SUBACK command with a given `message_id` + * and a sequence of granted QoSs. + */ +void mg_mqtt_suback(struct mg_connection *nc, uint8_t *qoss, size_t qoss_len, + uint16_t message_id); + +/* Sends a UNSUBACK command with a given `message_id`. */ +void mg_mqtt_unsuback(struct mg_connection *nc, uint16_t message_id); + +/* Sends a PINGREQ command. */ +void mg_mqtt_ping(struct mg_connection *nc); + +/* Sends a PINGRESP command. */ +void mg_mqtt_pong(struct mg_connection *nc); + +/* + * Extracts the next topic expression from a SUBSCRIBE command payload. + * + * The topic expression name will point to a string in the payload buffer. + * Returns the pos of the next topic expression or -1 when the list + * of topics is exhausted. + */ +int mg_mqtt_next_subscribe_topic(struct mg_mqtt_message *msg, + struct mg_str *topic, uint8_t *qos, int pos); + +/* + * Matches a topic against a topic expression + * + * Returns 1 if it matches; 0 otherwise. + */ +int mg_mqtt_match_topic_expression(struct mg_str exp, struct mg_str topic); + +/* + * Same as `mg_mqtt_match_topic_expression()`, but takes `exp` as a + * NULL-terminated string. + */ +int mg_mqtt_vmatch_topic_expression(const char *exp, struct mg_str topic); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CS_MONGOOSE_SRC_MQTT_H_ */ diff --git a/src/mqtt_client.h b/src/mqtt_client.h new file mode 100644 index 00000000..998414d1 --- /dev/null +++ b/src/mqtt_client.h @@ -0,0 +1,8 @@ +/* + * === API reference + */ + +#ifndef CS_MONGOOSE_SRC_MQTT_CLIENT_H_ +#define CS_MONGOOSE_SRC_MQTT_CLIENT_H_ + +#endif /* CS_MONGOOSE_SRC_MQTT_CLIENT_H_ */ diff --git a/src/mqtt_server.c b/src/mqtt_server.c new file mode 100644 index 00000000..af96d6f9 --- /dev/null +++ b/src/mqtt_server.c @@ -0,0 +1,194 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#include "mongoose/src/internal.h" +#include "mongoose/src/mqtt-server.h" + +#if MG_ENABLE_MQTT_BROKER + +static void mg_mqtt_session_init(struct mg_mqtt_broker *brk, + struct mg_mqtt_session *s, + struct mg_connection *nc) { + s->brk = brk; + s->subscriptions = NULL; + s->num_subscriptions = 0; + s->nc = nc; +} + +static void mg_mqtt_add_session(struct mg_mqtt_session *s) { + LIST_INSERT_HEAD(&s->brk->sessions, s, link); +} + +static void mg_mqtt_remove_session(struct mg_mqtt_session *s) { + LIST_REMOVE(s, link); +} + +static void mg_mqtt_destroy_session(struct mg_mqtt_session *s) { + size_t i; + for (i = 0; i < s->num_subscriptions; i++) { + MG_FREE((void *) s->subscriptions[i].topic); + } + MG_FREE(s->subscriptions); + MG_FREE(s); +} + +static void mg_mqtt_close_session(struct mg_mqtt_session *s) { + mg_mqtt_remove_session(s); + mg_mqtt_destroy_session(s); +} + +void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data) { + LIST_INIT(&brk->sessions); + brk->user_data = user_data; +} + +static void mg_mqtt_broker_handle_connect(struct mg_mqtt_broker *brk, + struct mg_connection *nc) { + struct mg_mqtt_session *s = + (struct mg_mqtt_session *) MG_CALLOC(1, sizeof *s); + if (s == NULL) { + /* LCOV_EXCL_START */ + mg_mqtt_connack(nc, MG_EV_MQTT_CONNACK_SERVER_UNAVAILABLE); + return; + /* LCOV_EXCL_STOP */ + } + + /* TODO(mkm): check header (magic and version) */ + + mg_mqtt_session_init(brk, s, nc); + nc->priv_2 = s; + mg_mqtt_add_session(s); + + mg_mqtt_connack(nc, MG_EV_MQTT_CONNACK_ACCEPTED); +} + +static void mg_mqtt_broker_handle_subscribe(struct mg_connection *nc, + struct mg_mqtt_message *msg) { + struct mg_mqtt_session *ss = (struct mg_mqtt_session *) nc->priv_2; + uint8_t qoss[MG_MQTT_MAX_SESSION_SUBSCRIPTIONS]; + size_t num_subs = 0; + struct mg_str topic; + uint8_t qos; + int pos; + struct mg_mqtt_topic_expression *te; + + for (pos = 0; + (pos = mg_mqtt_next_subscribe_topic(msg, &topic, &qos, pos)) != -1;) { + if (num_subs >= sizeof(MG_MQTT_MAX_SESSION_SUBSCRIPTIONS) || + (ss->num_subscriptions + num_subs >= + MG_MQTT_MAX_SESSION_SUBSCRIPTIONS)) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return; + } + qoss[num_subs++] = qos; + } + + if (num_subs > 0) { + te = (struct mg_mqtt_topic_expression *) MG_REALLOC( + ss->subscriptions, + sizeof(*ss->subscriptions) * (ss->num_subscriptions + num_subs)); + if (te == NULL) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return; + } + ss->subscriptions = te; + for (pos = 0; + pos < (int) msg->payload.len && + (pos = mg_mqtt_next_subscribe_topic(msg, &topic, &qos, pos)) != -1; + ss->num_subscriptions++) { + te = &ss->subscriptions[ss->num_subscriptions]; + te->topic = (char *) MG_MALLOC(topic.len + 1); + te->qos = qos; + memcpy((char *) te->topic, topic.p, topic.len); + ((char *) te->topic)[topic.len] = '\0'; + } + } + + if (pos == (int) msg->payload.len) { + mg_mqtt_suback(nc, qoss, num_subs, msg->message_id); + } else { + /* We did not fully parse the payload, something must be wrong. */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +} + +static void mg_mqtt_broker_handle_publish(struct mg_mqtt_broker *brk, + struct mg_mqtt_message *msg) { + struct mg_mqtt_session *s; + size_t i; + + for (s = mg_mqtt_next(brk, NULL); s != NULL; s = mg_mqtt_next(brk, s)) { + for (i = 0; i < s->num_subscriptions; i++) { + if (mg_mqtt_vmatch_topic_expression(s->subscriptions[i].topic, + msg->topic)) { + char buf[100], *p = buf; + mg_asprintf(&p, sizeof(buf), "%.*s", (int) msg->topic.len, + msg->topic.p); + if (p == NULL) { + return; + } + mg_mqtt_publish(s->nc, p, 0, 0, msg->payload.p, msg->payload.len); + if (p != buf) { + MG_FREE(p); + } + break; + } + } + } +} + +void mg_mqtt_broker(struct mg_connection *nc, int ev, void *data) { + struct mg_mqtt_message *msg = (struct mg_mqtt_message *) data; + struct mg_mqtt_broker *brk; + + if (nc->listener) { + brk = (struct mg_mqtt_broker *) nc->listener->priv_2; + } else { + brk = (struct mg_mqtt_broker *) nc->priv_2; + } + + switch (ev) { + case MG_EV_ACCEPT: + if (nc->proto_data == NULL) mg_set_protocol_mqtt(nc); + nc->priv_2 = NULL; /* Clear up the inherited pointer to broker */ + break; + case MG_EV_MQTT_CONNECT: + if (nc->priv_2 == NULL) { + mg_mqtt_broker_handle_connect(brk, nc); + } else { + /* Repeated CONNECT */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + case MG_EV_MQTT_SUBSCRIBE: + if (nc->priv_2 != NULL) { + mg_mqtt_broker_handle_subscribe(nc, msg); + } else { + /* Subscribe before CONNECT */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + case MG_EV_MQTT_PUBLISH: + if (nc->priv_2 != NULL) { + mg_mqtt_broker_handle_publish(brk, msg); + } else { + /* Publish before CONNECT */ + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + case MG_EV_CLOSE: + if (nc->listener && nc->priv_2 != NULL) { + mg_mqtt_close_session((struct mg_mqtt_session *) nc->priv_2); + } + break; + } +} + +struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk, + struct mg_mqtt_session *s) { + return s == NULL ? LIST_FIRST(&brk->sessions) : LIST_NEXT(s, link); +} + +#endif /* MG_ENABLE_MQTT_BROKER */ diff --git a/src/mqtt_server.h b/src/mqtt_server.h new file mode 100644 index 00000000..db736e89 --- /dev/null +++ b/src/mqtt_server.h @@ -0,0 +1,104 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +/* + * === MQTT Server API reference + */ + +#ifndef CS_MONGOOSE_SRC_MQTT_BROKER_H_ +#define CS_MONGOOSE_SRC_MQTT_BROKER_H_ + +#if MG_ENABLE_MQTT_BROKER + +#include "common/queue.h" +#include "mongoose/src/mqtt.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef MG_MQTT_MAX_SESSION_SUBSCRIPTIONS +#define MG_MQTT_MAX_SESSION_SUBSCRIPTIONS 512 +#endif + +struct mg_mqtt_broker; + +/* MQTT session (Broker side). */ +struct mg_mqtt_session { + struct mg_mqtt_broker *brk; /* Broker */ + LIST_ENTRY(mg_mqtt_session) link; /* mg_mqtt_broker::sessions linkage */ + struct mg_connection *nc; /* Connection with the client */ + size_t num_subscriptions; /* Size of `subscriptions` array */ + void *user_data; /* User data */ + struct mg_mqtt_topic_expression *subscriptions; +}; + +/* MQTT broker. */ +struct mg_mqtt_broker { + LIST_HEAD(_mg_sesshead, mg_mqtt_session) sessions; /* Session list */ + void *user_data; /* User data */ +}; + +/* Initialises a MQTT broker. */ +void mg_mqtt_broker_init(struct mg_mqtt_broker *brk, void *user_data); + +/* + * Processes a MQTT broker message. + * + * The listening connection expects a pointer to an initialised + * `mg_mqtt_broker` structure in the `user_data` field. + * + * Basic usage: + * + * ```c + * mg_mqtt_broker_init(&brk, NULL); + * + * if ((nc = mg_bind(&mgr, address, mg_mqtt_broker)) == NULL) { + * // fail; + * } + * nc->user_data = &brk; + * ``` + * + * New incoming connections will receive a `mg_mqtt_session` structure + * in the connection `user_data`. The original `user_data` will be stored + * in the `user_data` field of the session structure. This allows the user + * handler to store user data before `mg_mqtt_broker` creates the session. + * + * Since only the MG_EV_ACCEPT message is processed by the listening socket, + * for most events the `user_data` will thus point to a `mg_mqtt_session`. + */ +void mg_mqtt_broker(struct mg_connection *brk, int ev, void *data); + +/* + * Iterates over all MQTT session connections. Example: + * + * ```c + * struct mg_mqtt_session *s; + * for (s = mg_mqtt_next(brk, NULL); s != NULL; s = mg_mqtt_next(brk, s)) { + * // Do something + * } + * ``` + */ +struct mg_mqtt_session *mg_mqtt_next(struct mg_mqtt_broker *brk, + struct mg_mqtt_session *s); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_MQTT_BROKER */ +#endif /* CS_MONGOOSE_SRC_MQTT_BROKER_H_ */ diff --git a/src/net.c b/src/net.c new file mode 100644 index 00000000..efd6d620 --- /dev/null +++ b/src/net.c @@ -0,0 +1,1016 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +#include "common/cs_time.h" +#include "mongoose/src/dns.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/resolv.h" +#include "mongoose/src/tun.h" +#include "mongoose/src/util.h" + +#define MG_MAX_HOST_LEN 200 + +#define MG_COPY_COMMON_CONNECTION_OPTIONS(dst, src) \ + memcpy(dst, src, sizeof(*dst)); + +/* Which flags can be pre-set by the user at connection creation time. */ +#define _MG_ALLOWED_CONNECT_FLAGS_MASK \ + (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \ + MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_ENABLE_BROADCAST) +/* Which flags should be modifiable by user's callbacks. */ +#define _MG_CALLBACK_MODIFIABLE_FLAGS_MASK \ + (MG_F_USER_1 | MG_F_USER_2 | MG_F_USER_3 | MG_F_USER_4 | MG_F_USER_5 | \ + MG_F_USER_6 | MG_F_WEBSOCKET_NO_DEFRAG | MG_F_SEND_AND_CLOSE | \ + MG_F_CLOSE_IMMEDIATELY | MG_F_IS_WEBSOCKET | MG_F_DELETE_CHUNK) + +#ifndef intptr_t +#define intptr_t long +#endif + +MG_INTERNAL void mg_add_conn(struct mg_mgr *mgr, struct mg_connection *c) { + DBG(("%p %p", mgr, c)); + c->mgr = mgr; + c->next = mgr->active_connections; + mgr->active_connections = c; + c->prev = NULL; + if (c->next != NULL) c->next->prev = c; + if (c->sock != INVALID_SOCKET) { + c->iface->vtable->add_conn(c); + } +} + +MG_INTERNAL void mg_remove_conn(struct mg_connection *conn) { + if (conn->prev == NULL) conn->mgr->active_connections = conn->next; + if (conn->prev) conn->prev->next = conn->next; + if (conn->next) conn->next->prev = conn->prev; + conn->prev = conn->next = NULL; + conn->iface->vtable->remove_conn(conn); +} + +MG_INTERNAL void mg_call(struct mg_connection *nc, + mg_event_handler_t ev_handler, void *user_data, int ev, + void *ev_data) { + static int nesting_level = 0; + nesting_level++; + if (ev_handler == NULL) { + /* + * If protocol handler is specified, call it. Otherwise, call user-specified + * event handler. + */ + ev_handler = nc->proto_handler ? nc->proto_handler : nc->handler; + } + if (ev != MG_EV_POLL) { + DBG(("%p %s ev=%d ev_data=%p flags=%lu rmbl=%d smbl=%d", nc, + ev_handler == nc->handler ? "user" : "proto", ev, ev_data, nc->flags, + (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); + } + +#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP + if (nc->mgr->hexdump_file != NULL && ev != MG_EV_POLL && ev != MG_EV_RECV && + ev != MG_EV_SEND /* handled separately */) { + mg_hexdump_connection(nc, nc->mgr->hexdump_file, NULL, 0, ev); + } +#endif + if (ev_handler != NULL) { + unsigned long flags_before = nc->flags; + size_t recv_mbuf_before = nc->recv_mbuf.len, recved; + ev_handler(nc, ev, ev_data MG_UD_ARG(user_data)); + recved = (recv_mbuf_before - nc->recv_mbuf.len); + /* Prevent user handler from fiddling with system flags. */ + if (ev_handler == nc->handler && nc->flags != flags_before) { + nc->flags = (flags_before & ~_MG_CALLBACK_MODIFIABLE_FLAGS_MASK) | + (nc->flags & _MG_CALLBACK_MODIFIABLE_FLAGS_MASK); + } + /* It's important to not double-count recved bytes, and since mg_call can be + * called recursively (e.g. proto_handler invokes user handler), we keep + * track of recursion and only report received bytes at the top level. */ + if (nesting_level == 1 && recved > 0 && !(nc->flags & MG_F_UDP)) { + nc->iface->vtable->recved(nc, recved); + } + } + if (ev != MG_EV_POLL) { + DBG(("%p after %s flags=%lu rmbl=%d smbl=%d", nc, + ev_handler == nc->handler ? "user" : "proto", nc->flags, + (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); + } + nesting_level--; +#if !MG_ENABLE_CALLBACK_USERDATA + (void) user_data; +#endif +} + +void mg_if_timer(struct mg_connection *c, double now) { + if (c->ev_timer_time > 0 && now >= c->ev_timer_time) { + double old_value = c->ev_timer_time; + c->ev_timer_time = 0; + mg_call(c, NULL, c->user_data, MG_EV_TIMER, &old_value); + } +} + +void mg_if_poll(struct mg_connection *nc, time_t now) { + if (!(nc->flags & MG_F_SSL) || (nc->flags & MG_F_SSL_HANDSHAKE_DONE)) { + mg_call(nc, NULL, nc->user_data, MG_EV_POLL, &now); + } +} + +void mg_destroy_conn(struct mg_connection *conn, int destroy_if) { + if (destroy_if) conn->iface->vtable->destroy_conn(conn); + if (conn->proto_data != NULL && conn->proto_data_destructor != NULL) { + conn->proto_data_destructor(conn->proto_data); + } +#if MG_ENABLE_SSL + mg_ssl_if_conn_free(conn); +#endif + mbuf_free(&conn->recv_mbuf); + mbuf_free(&conn->send_mbuf); + + memset(conn, 0, sizeof(*conn)); + MG_FREE(conn); +} + +void mg_close_conn(struct mg_connection *conn) { + DBG(("%p %lu %d", conn, conn->flags, conn->sock)); +#if MG_ENABLE_SSL + if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) { + mg_ssl_if_conn_close_notify(conn); + } +#endif + mg_remove_conn(conn); + conn->iface->vtable->destroy_conn(conn); + mg_call(conn, NULL, conn->user_data, MG_EV_CLOSE, NULL); + mg_destroy_conn(conn, 0 /* destroy_if */); +} + +void mg_mgr_init(struct mg_mgr *m, void *user_data) { + struct mg_mgr_init_opts opts; + memset(&opts, 0, sizeof(opts)); + mg_mgr_init_opt(m, user_data, opts); +} + +void mg_mgr_init_opt(struct mg_mgr *m, void *user_data, + struct mg_mgr_init_opts opts) { + memset(m, 0, sizeof(*m)); +#if MG_ENABLE_BROADCAST + m->ctl[0] = m->ctl[1] = INVALID_SOCKET; +#endif + m->user_data = user_data; + +#ifdef _WIN32 + { + WSADATA data; + WSAStartup(MAKEWORD(2, 2), &data); + } +#elif defined(__unix__) + /* Ignore SIGPIPE signal, so if client cancels the request, it + * won't kill the whole process. */ + signal(SIGPIPE, SIG_IGN); +#endif + +#if MG_ENABLE_SSL + { + static int init_done; + if (!init_done) { + mg_ssl_if_init(); + init_done++; + } + } +#endif + { + int i; + if (opts.num_ifaces == 0) { + opts.num_ifaces = mg_num_ifaces; + opts.ifaces = mg_ifaces; + } + if (opts.main_iface != NULL) { + opts.ifaces[MG_MAIN_IFACE] = opts.main_iface; + } + m->num_ifaces = opts.num_ifaces; + m->ifaces = + (struct mg_iface **) MG_MALLOC(sizeof(*m->ifaces) * opts.num_ifaces); + for (i = 0; i < mg_num_ifaces; i++) { + m->ifaces[i] = mg_if_create_iface(opts.ifaces[i], m); + m->ifaces[i]->vtable->init(m->ifaces[i]); + } + } + if (opts.nameserver != NULL) { + m->nameserver = strdup(opts.nameserver); + } + DBG(("==================================")); + DBG(("init mgr=%p", m)); +} + +void mg_mgr_free(struct mg_mgr *m) { + struct mg_connection *conn, *tmp_conn; + + DBG(("%p", m)); + if (m == NULL) return; + /* Do one last poll, see https://github.com/cesanta/mongoose/issues/286 */ + mg_mgr_poll(m, 0); + +#if MG_ENABLE_BROADCAST + if (m->ctl[0] != INVALID_SOCKET) closesocket(m->ctl[0]); + if (m->ctl[1] != INVALID_SOCKET) closesocket(m->ctl[1]); + m->ctl[0] = m->ctl[1] = INVALID_SOCKET; +#endif + + for (conn = m->active_connections; conn != NULL; conn = tmp_conn) { + tmp_conn = conn->next; + mg_close_conn(conn); + } + + { + int i; + for (i = 0; i < m->num_ifaces; i++) { + m->ifaces[i]->vtable->free(m->ifaces[i]); + MG_FREE(m->ifaces[i]); + } + MG_FREE(m->ifaces); + } + + MG_FREE((char *) m->nameserver); +} + +time_t mg_mgr_poll(struct mg_mgr *m, int timeout_ms) { + int i; + time_t now = 0; /* oh GCC, seriously ? */ + + if (m->num_ifaces == 0) { + LOG(LL_ERROR, ("cannot poll: no interfaces")); + return 0; + } + + for (i = 0; i < m->num_ifaces; i++) { + now = m->ifaces[i]->vtable->poll(m->ifaces[i], timeout_ms); + } + return now; +} + +int mg_vprintf(struct mg_connection *nc, const char *fmt, va_list ap) { + char mem[MG_VPRINTF_BUFFER_SIZE], *buf = mem; + int len; + + if ((len = mg_avprintf(&buf, sizeof(mem), fmt, ap)) > 0) { + mg_send(nc, buf, len); + } + if (buf != mem && buf != NULL) { + MG_FREE(buf); /* LCOV_EXCL_LINE */ + } /* LCOV_EXCL_LINE */ + + return len; +} + +int mg_printf(struct mg_connection *conn, const char *fmt, ...) { + int len; + va_list ap; + va_start(ap, fmt); + len = mg_vprintf(conn, fmt, ap); + va_end(ap); + return len; +} + +#if MG_ENABLE_SYNC_RESOLVER +/* TODO(lsm): use non-blocking resolver */ +static int mg_resolve2(const char *host, struct in_addr *ina) { +#if MG_ENABLE_GETADDRINFO + int rv = 0; + struct addrinfo hints, *servinfo, *p; + struct sockaddr_in *h = NULL; + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_INET; + hints.ai_socktype = SOCK_STREAM; + if ((rv = getaddrinfo(host, NULL, NULL, &servinfo)) != 0) { + DBG(("getaddrinfo(%s) failed: %s", host, strerror(mg_get_errno()))); + return 0; + } + for (p = servinfo; p != NULL; p = p->ai_next) { + memcpy(&h, &p->ai_addr, sizeof(struct sockaddr_in *)); + memcpy(ina, &h->sin_addr, sizeof(ina)); + } + freeaddrinfo(servinfo); + return 1; +#else + struct hostent *he; + if ((he = gethostbyname(host)) == NULL) { + DBG(("gethostbyname(%s) failed: %s", host, strerror(mg_get_errno()))); + } else { + memcpy(ina, he->h_addr_list[0], sizeof(*ina)); + return 1; + } + return 0; +#endif /* MG_ENABLE_GETADDRINFO */ +} + +int mg_resolve(const char *host, char *buf, size_t n) { + struct in_addr ad; + return mg_resolve2(host, &ad) ? snprintf(buf, n, "%s", inet_ntoa(ad)) : 0; +} +#endif /* MG_ENABLE_SYNC_RESOLVER */ + +MG_INTERNAL struct mg_connection *mg_create_connection_base( + struct mg_mgr *mgr, mg_event_handler_t callback, + struct mg_add_sock_opts opts) { + struct mg_connection *conn; + + if ((conn = (struct mg_connection *) MG_CALLOC(1, sizeof(*conn))) != NULL) { + conn->sock = INVALID_SOCKET; + conn->handler = callback; + conn->mgr = mgr; + conn->last_io_time = (time_t) mg_time(); + conn->iface = + (opts.iface != NULL ? opts.iface : mgr->ifaces[MG_MAIN_IFACE]); + conn->flags = opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK; + conn->user_data = opts.user_data; + /* + * SIZE_MAX is defined as a long long constant in + * system headers on some platforms and so it + * doesn't compile with pedantic ansi flags. + */ + conn->recv_mbuf_limit = ~0; + } else { + MG_SET_PTRPTR(opts.error_string, "failed to create connection"); + } + + return conn; +} + +MG_INTERNAL struct mg_connection *mg_create_connection( + struct mg_mgr *mgr, mg_event_handler_t callback, + struct mg_add_sock_opts opts) { + struct mg_connection *conn = mg_create_connection_base(mgr, callback, opts); + + if (conn != NULL && !conn->iface->vtable->create_conn(conn)) { + MG_FREE(conn); + conn = NULL; + } + if (conn == NULL) { + MG_SET_PTRPTR(opts.error_string, "failed to init connection"); + } + + return conn; +} + +/* + * Address format: [PROTO://][HOST]:PORT + * + * HOST could be IPv4/IPv6 address or a host name. + * `host` is a destination buffer to hold parsed HOST part. Should be at least + * MG_MAX_HOST_LEN bytes long. + * `proto` is a returned socket type, either SOCK_STREAM or SOCK_DGRAM + * + * Return: + * -1 on parse error + * 0 if HOST needs DNS lookup + * >0 length of the address string + */ +MG_INTERNAL int mg_parse_address(const char *str, union socket_address *sa, + int *proto, char *host, size_t host_len) { + unsigned int a, b, c, d, port = 0; + int ch, len = 0; +#if MG_ENABLE_IPV6 + char buf[100]; +#endif + + /* + * MacOS needs that. If we do not zero it, subsequent bind() will fail. + * Also, all-zeroes in the socket address means binding to all addresses + * for both IPv4 and IPv6 (INADDR_ANY and IN6ADDR_ANY_INIT). + */ + memset(sa, 0, sizeof(*sa)); + sa->sin.sin_family = AF_INET; + + *proto = SOCK_STREAM; + + if (strncmp(str, "udp://", 6) == 0) { + str += 6; + *proto = SOCK_DGRAM; + } else if (strncmp(str, "tcp://", 6) == 0) { + str += 6; + } + + if (sscanf(str, "%u.%u.%u.%u:%u%n", &a, &b, &c, &d, &port, &len) == 5) { + /* Bind to a specific IPv4 address, e.g. 192.168.1.5:8080 */ + sa->sin.sin_addr.s_addr = + htonl(((uint32_t) a << 24) | ((uint32_t) b << 16) | c << 8 | d); + sa->sin.sin_port = htons((uint16_t) port); +#if MG_ENABLE_IPV6 + } else if (sscanf(str, "[%99[^]]]:%u%n", buf, &port, &len) == 2 && + inet_pton(AF_INET6, buf, &sa->sin6.sin6_addr)) { + /* IPv6 address, e.g. [3ffe:2a00:100:7031::1]:8080 */ + sa->sin6.sin6_family = AF_INET6; + sa->sin.sin_port = htons((uint16_t) port); +#endif +#if MG_ENABLE_ASYNC_RESOLVER + } else if (strlen(str) < host_len && + sscanf(str, "%[^ :]:%u%n", host, &port, &len) == 2) { + sa->sin.sin_port = htons((uint16_t) port); + if (mg_resolve_from_hosts_file(host, sa) != 0) { + /* + * if resolving from hosts file failed and the host + * we are trying to resolve is `localhost` - we should + * try to resolve it using `gethostbyname` and do not try + * to resolve it via DNS server if gethostbyname has failed too + */ + if (mg_ncasecmp(host, "localhost", 9) != 0) { + return 0; + } + +#if MG_ENABLE_SYNC_RESOLVER + if (!mg_resolve2(host, &sa->sin.sin_addr)) { + return -1; + } +#else + return -1; +#endif + } +#endif + } else if (sscanf(str, ":%u%n", &port, &len) == 1 || + sscanf(str, "%u%n", &port, &len) == 1) { + /* If only port is specified, bind to IPv4, INADDR_ANY */ + sa->sin.sin_port = htons((uint16_t) port); + } else { + return -1; + } + + /* Required for MG_ENABLE_ASYNC_RESOLVER=0 */ + (void) host; + (void) host_len; + + ch = str[len]; /* Character that follows the address */ + return port < 0xffffUL && (ch == '\0' || ch == ',' || isspace(ch)) ? len : -1; +} + +struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc) { + struct mg_add_sock_opts opts; + struct mg_connection *nc; + memset(&opts, 0, sizeof(opts)); + nc = mg_create_connection(lc->mgr, lc->handler, opts); + if (nc == NULL) return NULL; + nc->listener = lc; + nc->proto_handler = lc->proto_handler; + nc->user_data = lc->user_data; + nc->recv_mbuf_limit = lc->recv_mbuf_limit; + nc->iface = lc->iface; + if (lc->flags & MG_F_SSL) nc->flags |= MG_F_SSL; + mg_add_conn(nc->mgr, nc); + DBG(("%p %p %d %d", lc, nc, nc->sock, (int) nc->flags)); + return nc; +} + +void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, + size_t sa_len) { + (void) sa_len; + nc->sa = *sa; + mg_call(nc, NULL, nc->user_data, MG_EV_ACCEPT, &nc->sa); +} + +void mg_send(struct mg_connection *nc, const void *buf, int len) { + nc->last_io_time = (time_t) mg_time(); + if (nc->flags & MG_F_UDP) { + nc->iface->vtable->udp_send(nc, buf, len); + } else { + nc->iface->vtable->tcp_send(nc, buf, len); + } +} + +void mg_if_sent_cb(struct mg_connection *nc, int num_sent) { + DBG(("%p %d", nc, num_sent)); +#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP + if (nc->mgr && nc->mgr->hexdump_file != NULL) { + char *buf = nc->send_mbuf.buf; + mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, num_sent, MG_EV_SEND); + } +#endif + if (num_sent < 0) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else { + mbuf_remove(&nc->send_mbuf, num_sent); + mbuf_trim(&nc->send_mbuf); + } + mg_call(nc, NULL, nc->user_data, MG_EV_SEND, &num_sent); +} + +MG_INTERNAL void mg_recv_common(struct mg_connection *nc, void *buf, int len, + int own) { + DBG(("%p %d %u", nc, len, (unsigned int) nc->recv_mbuf.len)); + +#if !defined(NO_LIBC) && MG_ENABLE_HEXDUMP + if (nc->mgr && nc->mgr->hexdump_file != NULL) { + mg_hexdump_connection(nc, nc->mgr->hexdump_file, buf, len, MG_EV_RECV); + } +#endif + + if (nc->flags & MG_F_CLOSE_IMMEDIATELY) { + DBG(("%p discarded %d bytes", nc, len)); + /* + * This connection will not survive next poll. Do not deliver events, + * send data to /dev/null without acking. + */ + if (own) { + MG_FREE(buf); + } + return; + } + nc->last_io_time = (time_t) mg_time(); + if (!own) { + mbuf_append(&nc->recv_mbuf, buf, len); + } else if (nc->recv_mbuf.len == 0) { + /* Adopt buf as recv_mbuf's backing store. */ + mbuf_free(&nc->recv_mbuf); + nc->recv_mbuf.buf = (char *) buf; + nc->recv_mbuf.size = nc->recv_mbuf.len = len; + } else { + mbuf_append(&nc->recv_mbuf, buf, len); + MG_FREE(buf); + } + mg_call(nc, NULL, nc->user_data, MG_EV_RECV, &len); +} + +void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own) { + mg_recv_common(nc, buf, len, own); +} + +void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len, + union socket_address *sa, size_t sa_len) { + assert(nc->flags & MG_F_UDP); + DBG(("%p %u", nc, (unsigned int) len)); + if (nc->flags & MG_F_LISTENING) { + struct mg_connection *lc = nc; + /* + * Do we have an existing connection for this source? + * This is very inefficient for long connection lists. + */ + for (nc = mg_next(lc->mgr, NULL); nc != NULL; nc = mg_next(lc->mgr, nc)) { + if (memcmp(&nc->sa.sa, &sa->sa, sa_len) == 0 && nc->listener == lc) { + break; + } + } + if (nc == NULL) { + struct mg_add_sock_opts opts; + memset(&opts, 0, sizeof(opts)); + /* Create fake connection w/out sock initialization */ + nc = mg_create_connection_base(lc->mgr, lc->handler, opts); + if (nc != NULL) { + nc->sock = lc->sock; + nc->listener = lc; + nc->sa = *sa; + nc->proto_handler = lc->proto_handler; + nc->user_data = lc->user_data; + nc->recv_mbuf_limit = lc->recv_mbuf_limit; + nc->flags = MG_F_UDP; + /* + * Long-lived UDP "connections" i.e. interactions that involve more + * than one request and response are rare, most are transactional: + * response is sent and the "connection" is closed. Or - should be. + * But users (including ourselves) tend to forget about that part, + * because UDP is connectionless and one does not think about + * processing a UDP request as handling a connection that needs to be + * closed. Thus, we begin with SEND_AND_CLOSE flag set, which should + * be a reasonable default for most use cases, but it is possible to + * turn it off the connection should be kept alive after processing. + */ + nc->flags |= MG_F_SEND_AND_CLOSE; + mg_add_conn(lc->mgr, nc); + mg_call(nc, NULL, nc->user_data, MG_EV_ACCEPT, &nc->sa); + } else { + DBG(("OOM")); + /* No return here, we still need to drop on the floor */ + } + } + } + if (nc != NULL) { + mg_recv_common(nc, buf, len, 1); + } else { + /* Drop on the floor. */ + MG_FREE(buf); + } +} + +/* + * Schedules an async connect for a resolved address and proto. + * Called from two places: `mg_connect_opt()` and from async resolver. + * When called from the async resolver, it must trigger `MG_EV_CONNECT` event + * with a failure flag to indicate connection failure. + */ +MG_INTERNAL struct mg_connection *mg_do_connect(struct mg_connection *nc, + int proto, + union socket_address *sa) { + DBG(("%p %s://%s:%hu", nc, proto == SOCK_DGRAM ? "udp" : "tcp", + inet_ntoa(sa->sin.sin_addr), ntohs(sa->sin.sin_port))); + + nc->flags |= MG_F_CONNECTING; + if (proto == SOCK_DGRAM) { + nc->iface->vtable->connect_udp(nc); + } else { + nc->iface->vtable->connect_tcp(nc, sa); + } + mg_add_conn(nc->mgr, nc); + return nc; +} + +void mg_if_connect_cb(struct mg_connection *nc, int err) { + DBG(("%p connect, err=%d", nc, err)); + nc->flags &= ~MG_F_CONNECTING; + if (err != 0) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + mg_call(nc, NULL, nc->user_data, MG_EV_CONNECT, &err); +} + +#if MG_ENABLE_ASYNC_RESOLVER +/* + * Callback for the async resolver on mg_connect_opt() call. + * Main task of this function is to trigger MG_EV_CONNECT event with + * either failure (and dealloc the connection) + * or success (and proceed with connect() + */ +static void resolve_cb(struct mg_dns_message *msg, void *data, + enum mg_resolve_err e) { + struct mg_connection *nc = (struct mg_connection *) data; + int i; + int failure = -1; + + nc->flags &= ~MG_F_RESOLVING; + if (msg != NULL) { + /* + * Take the first DNS A answer and run... + */ + for (i = 0; i < msg->num_answers; i++) { + if (msg->answers[i].rtype == MG_DNS_A_RECORD) { + /* + * Async resolver guarantees that there is at least one answer. + * TODO(lsm): handle IPv6 answers too + */ + mg_dns_parse_record_data(msg, &msg->answers[i], &nc->sa.sin.sin_addr, + 4); + mg_do_connect(nc, nc->flags & MG_F_UDP ? SOCK_DGRAM : SOCK_STREAM, + &nc->sa); + return; + } + } + } + + if (e == MG_RESOLVE_TIMEOUT) { + double now = mg_time(); + mg_call(nc, NULL, nc->user_data, MG_EV_TIMER, &now); + } + + /* + * If we get there was no MG_DNS_A_RECORD in the answer + */ + mg_call(nc, NULL, nc->user_data, MG_EV_CONNECT, &failure); + mg_call(nc, NULL, nc->user_data, MG_EV_CLOSE, NULL); + mg_destroy_conn(nc, 1 /* destroy_if */); +} +#endif + +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t callback, + void *user_data)) { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_connect_opt(mgr, address, MG_CB(callback, user_data), opts); +} + +struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t callback, + void *user_data), + struct mg_connect_opts opts) { + struct mg_connection *nc = NULL; + int proto, rc; + struct mg_add_sock_opts add_sock_opts; + char host[MG_MAX_HOST_LEN]; + + MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts); + + if ((nc = mg_create_connection(mgr, callback, add_sock_opts)) == NULL) { + return NULL; + } + + if ((rc = mg_parse_address(address, &nc->sa, &proto, host, sizeof(host))) < + 0) { + /* Address is malformed */ + MG_SET_PTRPTR(opts.error_string, "cannot parse address"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + + nc->flags |= opts.flags & _MG_ALLOWED_CONNECT_FLAGS_MASK; + nc->flags |= (proto == SOCK_DGRAM) ? MG_F_UDP : 0; +#if MG_ENABLE_CALLBACK_USERDATA + nc->user_data = user_data; +#else + nc->user_data = opts.user_data; +#endif + +#if MG_ENABLE_SSL + DBG(("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"), + (opts.ssl_key ? opts.ssl_key : "-"), + (opts.ssl_ca_cert ? opts.ssl_ca_cert : "-"))); + + if (opts.ssl_cert != NULL || opts.ssl_ca_cert != NULL || + opts.ssl_psk_identity != NULL) { + const char *err_msg = NULL; + struct mg_ssl_if_conn_params params; + if (nc->flags & MG_F_UDP) { + MG_SET_PTRPTR(opts.error_string, "SSL for UDP is not supported"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + memset(¶ms, 0, sizeof(params)); + params.cert = opts.ssl_cert; + params.key = opts.ssl_key; + params.ca_cert = opts.ssl_ca_cert; + params.cipher_suites = opts.ssl_cipher_suites; + params.psk_identity = opts.ssl_psk_identity; + params.psk_key = opts.ssl_psk_key; + if (opts.ssl_ca_cert != NULL) { + if (opts.ssl_server_name != NULL) { + if (strcmp(opts.ssl_server_name, "*") != 0) { + params.server_name = opts.ssl_server_name; + } + } else if (rc == 0) { /* If it's a DNS name, use host. */ + params.server_name = host; + } + } + if (mg_ssl_if_conn_init(nc, ¶ms, &err_msg) != MG_SSL_OK) { + MG_SET_PTRPTR(opts.error_string, err_msg); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + nc->flags |= MG_F_SSL; + } +#endif /* MG_ENABLE_SSL */ + + if (rc == 0) { +#if MG_ENABLE_ASYNC_RESOLVER + /* + * DNS resolution is required for host. + * mg_parse_address() fills port in nc->sa, which we pass to resolve_cb() + */ + struct mg_connection *dns_conn = NULL; + struct mg_resolve_async_opts o; + memset(&o, 0, sizeof(o)); + o.dns_conn = &dns_conn; + o.nameserver = opts.nameserver; + if (mg_resolve_async_opt(nc->mgr, host, MG_DNS_A_RECORD, resolve_cb, nc, + o) != 0) { + MG_SET_PTRPTR(opts.error_string, "cannot schedule DNS lookup"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + nc->priv_2 = dns_conn; + nc->flags |= MG_F_RESOLVING; + return nc; +#else + MG_SET_PTRPTR(opts.error_string, "Resolver is disabled"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; +#endif + } else { + /* Address is parsed and resolved to IP. proceed with connect() */ + return mg_do_connect(nc, proto, &nc->sa); + } +} + +struct mg_connection *mg_bind(struct mg_mgr *srv, const char *address, + MG_CB(mg_event_handler_t event_handler, + void *user_data)) { + struct mg_bind_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_bind_opt(srv, address, MG_CB(event_handler, user_data), opts); +} + +struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t callback, + void *user_data), + struct mg_bind_opts opts) { + union socket_address sa; + struct mg_connection *nc = NULL; + int proto, rc; + struct mg_add_sock_opts add_sock_opts; + char host[MG_MAX_HOST_LEN]; + +#if MG_ENABLE_CALLBACK_USERDATA + opts.user_data = user_data; +#endif + + if (callback == NULL) { + MG_SET_PTRPTR(opts.error_string, "handler is required"); + return NULL; + } + + MG_COPY_COMMON_CONNECTION_OPTIONS(&add_sock_opts, &opts); + +#if MG_ENABLE_TUN + if (mg_strncmp(mg_mk_str(address), mg_mk_str("ws://"), 5) == 0 || + mg_strncmp(mg_mk_str(address), mg_mk_str("wss://"), 6) == 0) { + return mg_tun_bind_opt(mgr, address, MG_CB(callback, user_data), opts); + } +#endif + + if (mg_parse_address(address, &sa, &proto, host, sizeof(host)) <= 0) { + MG_SET_PTRPTR(opts.error_string, "cannot parse address"); + return NULL; + } + + nc = mg_create_connection(mgr, callback, add_sock_opts); + if (nc == NULL) { + return NULL; + } + + nc->sa = sa; + nc->flags |= MG_F_LISTENING; + if (proto == SOCK_DGRAM) nc->flags |= MG_F_UDP; + +#if MG_ENABLE_SSL + DBG(("%p %s %s,%s,%s", nc, address, (opts.ssl_cert ? opts.ssl_cert : "-"), + (opts.ssl_key ? opts.ssl_key : "-"), + (opts.ssl_ca_cert ? opts.ssl_ca_cert : "-"))); + + if (opts.ssl_cert != NULL || opts.ssl_ca_cert != NULL) { + const char *err_msg = NULL; + struct mg_ssl_if_conn_params params; + if (nc->flags & MG_F_UDP) { + MG_SET_PTRPTR(opts.error_string, "SSL for UDP is not supported"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + memset(¶ms, 0, sizeof(params)); + params.cert = opts.ssl_cert; + params.key = opts.ssl_key; + params.ca_cert = opts.ssl_ca_cert; + params.cipher_suites = opts.ssl_cipher_suites; + if (mg_ssl_if_conn_init(nc, ¶ms, &err_msg) != MG_SSL_OK) { + MG_SET_PTRPTR(opts.error_string, err_msg); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + nc->flags |= MG_F_SSL; + } +#endif /* MG_ENABLE_SSL */ + + if (nc->flags & MG_F_UDP) { + rc = nc->iface->vtable->listen_udp(nc, &nc->sa); + } else { + rc = nc->iface->vtable->listen_tcp(nc, &nc->sa); + } + if (rc != 0) { + DBG(("Failed to open listener: %d", rc)); + MG_SET_PTRPTR(opts.error_string, "failed to open listener"); + mg_destroy_conn(nc, 1 /* destroy_if */); + return NULL; + } + mg_add_conn(nc->mgr, nc); + + return nc; +} + +struct mg_connection *mg_next(struct mg_mgr *s, struct mg_connection *conn) { + return conn == NULL ? s->active_connections : conn->next; +} + +#if MG_ENABLE_BROADCAST +void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data, + size_t len) { + struct ctl_msg ctl_msg; + + /* + * Mongoose manager has a socketpair, `struct mg_mgr::ctl`, + * where `mg_broadcast()` pushes the message. + * `mg_mgr_poll()` wakes up, reads a message from the socket pair, and calls + * specified callback for each connection. Thus the callback function executes + * in event manager thread. + */ + if (mgr->ctl[0] != INVALID_SOCKET && data != NULL && + len < sizeof(ctl_msg.message)) { + size_t dummy; + + ctl_msg.callback = cb; + memcpy(ctl_msg.message, data, len); + dummy = MG_SEND_FUNC(mgr->ctl[0], (char *) &ctl_msg, + offsetof(struct ctl_msg, message) + len, 0); + dummy = MG_RECV_FUNC(mgr->ctl[0], (char *) &len, 1, 0); + (void) dummy; /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 */ + } +} +#endif /* MG_ENABLE_BROADCAST */ + +static int isbyte(int n) { + return n >= 0 && n <= 255; +} + +static int parse_net(const char *spec, uint32_t *net, uint32_t *mask) { + int n, a, b, c, d, slash = 32, len = 0; + + if ((sscanf(spec, "%d.%d.%d.%d/%d%n", &a, &b, &c, &d, &slash, &n) == 5 || + sscanf(spec, "%d.%d.%d.%d%n", &a, &b, &c, &d, &n) == 4) && + isbyte(a) && isbyte(b) && isbyte(c) && isbyte(d) && slash >= 0 && + slash < 33) { + len = n; + *net = + ((uint32_t) a << 24) | ((uint32_t) b << 16) | ((uint32_t) c << 8) | d; + *mask = slash ? 0xffffffffU << (32 - slash) : 0; + } + + return len; +} + +int mg_check_ip_acl(const char *acl, uint32_t remote_ip) { + int allowed, flag; + uint32_t net, mask; + struct mg_str vec; + + /* If any ACL is set, deny by default */ + allowed = (acl == NULL || *acl == '\0') ? '+' : '-'; + + while ((acl = mg_next_comma_list_entry(acl, &vec, NULL)) != NULL) { + flag = vec.p[0]; + if ((flag != '+' && flag != '-') || + parse_net(&vec.p[1], &net, &mask) == 0) { + return -1; + } + + if (net == (remote_ip & mask)) { + allowed = flag; + } + } + + DBG(("%08x %c", (unsigned int) remote_ip, allowed)); + return allowed == '+'; +} + +/* Move data from one connection to another */ +void mg_forward(struct mg_connection *from, struct mg_connection *to) { + mg_send(to, from->recv_mbuf.buf, from->recv_mbuf.len); + mbuf_remove(&from->recv_mbuf, from->recv_mbuf.len); +} + +double mg_set_timer(struct mg_connection *c, double timestamp) { + double result = c->ev_timer_time; + c->ev_timer_time = timestamp; + /* + * If this connection is resolving, it's not in the list of active + * connections, so not processed yet. It has a DNS resolver connection + * linked to it. Set up a timer for the DNS connection. + */ + DBG(("%p %p %d -> %lu", c, c->priv_2, (c->flags & MG_F_RESOLVING ? 1 : 0), + (unsigned long) timestamp)); + if ((c->flags & MG_F_RESOLVING) && c->priv_2 != NULL) { + ((struct mg_connection *) c->priv_2)->ev_timer_time = timestamp; + } + return result; +} + +void mg_sock_set(struct mg_connection *nc, sock_t sock) { + if (sock != INVALID_SOCKET) { + nc->iface->vtable->sock_set(nc, sock); + } +} + +void mg_if_get_conn_addr(struct mg_connection *nc, int remote, + union socket_address *sa) { + nc->iface->vtable->get_conn_addr(nc, remote, sa); +} + +struct mg_connection *mg_add_sock_opt(struct mg_mgr *s, sock_t sock, + MG_CB(mg_event_handler_t callback, + void *user_data), + struct mg_add_sock_opts opts) { +#if MG_ENABLE_CALLBACK_USERDATA + opts.user_data = user_data; +#endif + + struct mg_connection *nc = mg_create_connection_base(s, callback, opts); + if (nc != NULL) { + mg_sock_set(nc, sock); + mg_add_conn(nc->mgr, nc); + } + return nc; +} + +struct mg_connection *mg_add_sock(struct mg_mgr *s, sock_t sock, + MG_CB(mg_event_handler_t callback, + void *user_data)) { + struct mg_add_sock_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_add_sock_opt(s, sock, MG_CB(callback, user_data), opts); +} + +double mg_time(void) { + return cs_time(); +} diff --git a/src/net.h b/src/net.h new file mode 100644 index 00000000..dab38eeb --- /dev/null +++ b/src/net.h @@ -0,0 +1,592 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +/* + * === Core API: TCP/UDP/SSL + * + * NOTE: Mongoose manager is single threaded. It does not protect + * its data structures by mutexes, therefore all functions that are dealing + * with a particular event manager should be called from the same thread, + * with exception of the `mg_broadcast()` function. It is fine to have different + * event managers handled by different threads. + */ + +#ifndef CS_MONGOOSE_SRC_NET_H_ +#define CS_MONGOOSE_SRC_NET_H_ + +#include "mongoose/src/common.h" +#include "mongoose/src/net_if.h" +#include "common/mbuf.h" + +#ifndef MG_VPRINTF_BUFFER_SIZE +#define MG_VPRINTF_BUFFER_SIZE 100 +#endif + +#ifdef MG_USE_READ_WRITE +#define MG_RECV_FUNC(s, b, l, f) read(s, b, l) +#define MG_SEND_FUNC(s, b, l, f) write(s, b, l) +#else +#define MG_RECV_FUNC(s, b, l, f) recv(s, b, l, f) +#define MG_SEND_FUNC(s, b, l, f) send(s, b, l, f) +#endif + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +union socket_address { + struct sockaddr sa; + struct sockaddr_in sin; +#if MG_ENABLE_IPV6 + struct sockaddr_in6 sin6; +#else + struct sockaddr sin6; +#endif +}; + +struct mg_connection; + +/* + * Callback function (event handler) prototype. Must be defined by the user. + * Mongoose calls the event handler, passing the events defined below. + */ +typedef void (*mg_event_handler_t)(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)); + +/* Events. Meaning of event parameter (evp) is given in the comment. */ +#define MG_EV_POLL 0 /* Sent to each connection on each mg_mgr_poll() call */ +#define MG_EV_ACCEPT 1 /* New connection accepted. union socket_address * */ +#define MG_EV_CONNECT 2 /* connect() succeeded or failed. int * */ +#define MG_EV_RECV 3 /* Data has been received. int *num_bytes */ +#define MG_EV_SEND 4 /* Data has been written to a socket. int *num_bytes */ +#define MG_EV_CLOSE 5 /* Connection is closed. NULL */ +#define MG_EV_TIMER 6 /* now >= conn->ev_timer_time. double * */ + +/* + * Mongoose event manager. + */ +struct mg_mgr { + struct mg_connection *active_connections; +#if MG_ENABLE_HEXDUMP + const char *hexdump_file; /* Debug hexdump file path */ +#endif +#if MG_ENABLE_BROADCAST + sock_t ctl[2]; /* Socketpair for mg_broadcast() */ +#endif + void *user_data; /* User data */ + int num_ifaces; + struct mg_iface **ifaces; /* network interfaces */ + const char *nameserver; /* DNS server to use */ +}; + +/* + * Mongoose connection. + */ +struct mg_connection { + struct mg_connection *next, *prev; /* mg_mgr::active_connections linkage */ + struct mg_connection *listener; /* Set only for accept()-ed connections */ + struct mg_mgr *mgr; /* Pointer to containing manager */ + + sock_t sock; /* Socket to the remote peer */ + int err; + union socket_address sa; /* Remote peer address */ + size_t recv_mbuf_limit; /* Max size of recv buffer */ + struct mbuf recv_mbuf; /* Received data */ + struct mbuf send_mbuf; /* Data scheduled for sending */ + time_t last_io_time; /* Timestamp of the last socket IO */ + double ev_timer_time; /* Timestamp of the future MG_EV_TIMER */ +#if MG_ENABLE_SSL + void *ssl_if_data; /* SSL library data. */ +#endif + mg_event_handler_t proto_handler; /* Protocol-specific event handler */ + void *proto_data; /* Protocol-specific data */ + void (*proto_data_destructor)(void *proto_data); + mg_event_handler_t handler; /* Event handler function */ + void *user_data; /* User-specific data */ + union { + void *v; + /* + * the C standard is fussy about fitting function pointers into + * void pointers, since some archs might have fat pointers for functions. + */ + mg_event_handler_t f; + } priv_1; + void *priv_2; + void *mgr_data; /* Implementation-specific event manager's data. */ + struct mg_iface *iface; + unsigned long flags; +/* Flags set by Mongoose */ +#define MG_F_LISTENING (1 << 0) /* This connection is listening */ +#define MG_F_UDP (1 << 1) /* This connection is UDP */ +#define MG_F_RESOLVING (1 << 2) /* Waiting for async resolver */ +#define MG_F_CONNECTING (1 << 3) /* connect() call in progress */ +#define MG_F_SSL (1 << 4) /* SSL is enabled on the connection */ +#define MG_F_SSL_HANDSHAKE_DONE (1 << 5) /* SSL hanshake has completed */ +#define MG_F_WANT_READ (1 << 6) /* SSL specific */ +#define MG_F_WANT_WRITE (1 << 7) /* SSL specific */ +#define MG_F_IS_WEBSOCKET (1 << 8) /* Websocket specific */ + +/* Flags that are settable by user */ +#define MG_F_SEND_AND_CLOSE (1 << 10) /* Push remaining data and close */ +#define MG_F_CLOSE_IMMEDIATELY (1 << 11) /* Disconnect */ +#define MG_F_WEBSOCKET_NO_DEFRAG (1 << 12) /* Websocket specific */ +#define MG_F_DELETE_CHUNK (1 << 13) /* HTTP specific */ +#define MG_F_ENABLE_BROADCAST (1 << 14) /* Allow broadcast address usage */ +#define MG_F_TUN_DO_NOT_RECONNECT (1 << 15) /* Don't reconnect tunnel */ + +#define MG_F_USER_1 (1 << 20) /* Flags left for application */ +#define MG_F_USER_2 (1 << 21) +#define MG_F_USER_3 (1 << 22) +#define MG_F_USER_4 (1 << 23) +#define MG_F_USER_5 (1 << 24) +#define MG_F_USER_6 (1 << 25) +}; + +/* + * Initialise Mongoose manager. Side effect: ignores SIGPIPE signal. + * `mgr->user_data` field will be initialised with a `user_data` parameter. + * That is an arbitrary pointer, where the user code can associate some data + * with the particular Mongoose manager. For example, a C++ wrapper class + * could be written in which case `user_data` can hold a pointer to the + * class instance. + */ +void mg_mgr_init(struct mg_mgr *mgr, void *user_data); + +/* + * Optional parameters to `mg_mgr_init_opt()`. + * + * If `main_iface` is not NULL, it will be used as the main interface in the + * default interface set. The pointer will be free'd by `mg_mgr_free`. + * Otherwise, the main interface will be autodetected based on the current + * platform. + * + * If `num_ifaces` is 0 and `ifaces` is NULL, the default interface set will be + * used. + * This is an advanced option, as it requires you to construct a full interface + * set, including special networking interfaces required by some optional + * features such as TCP tunneling. Memory backing `ifaces` and each of the + * `num_ifaces` pointers it contains will be reclaimed by `mg_mgr_free`. + */ +struct mg_mgr_init_opts { + const struct mg_iface_vtable *main_iface; + int num_ifaces; + const struct mg_iface_vtable **ifaces; + const char *nameserver; +}; + +/* + * Like `mg_mgr_init` but with more options. + * + * Notably, this allows you to create a manger and choose + * dynamically which networking interface implementation to use. + */ +void mg_mgr_init_opt(struct mg_mgr *mgr, void *user_data, + struct mg_mgr_init_opts opts); + +/* + * De-initialises Mongoose manager. + * + * Closes and deallocates all active connections. + */ +void mg_mgr_free(struct mg_mgr *); + +/* + * This function performs the actual IO and must be called in a loop + * (an event loop). It returns the current timestamp. + * `milli` is the maximum number of milliseconds to sleep. + * `mg_mgr_poll()` checks all connections for IO readiness. If at least one + * of the connections is IO-ready, `mg_mgr_poll()` triggers the respective + * event handlers and returns. + */ +time_t mg_mgr_poll(struct mg_mgr *, int milli); + +#if MG_ENABLE_BROADCAST +/* + * Passes a message of a given length to all connections. + * + * Must be called from a thread that does NOT call `mg_mgr_poll()`. + * Note that `mg_broadcast()` is the only function + * that can be, and must be, called from a different (non-IO) thread. + * + * `func` callback function will be called by the IO thread for each + * connection. When called, the event will be `MG_EV_POLL`, and a message will + * be passed as the `ev_data` pointer. Maximum message size is capped + * by `MG_CTL_MSG_MESSAGE_SIZE` which is set to 8192 bytes. + */ +void mg_broadcast(struct mg_mgr *mgr, mg_event_handler_t cb, void *data, + size_t len); +#endif + +/* + * Iterates over all active connections. + * + * Returns the next connection from the list + * of active connections or `NULL` if there are no more connections. Below + * is the iteration idiom: + * + * ```c + * for (c = mg_next(srv, NULL); c != NULL; c = mg_next(srv, c)) { + * // Do something with connection `c` + * } + * ``` + */ +struct mg_connection *mg_next(struct mg_mgr *mgr, struct mg_connection *c); + +/* + * Optional parameters to `mg_add_sock_opt()`. + * + * `flags` is an initial `struct mg_connection::flags` bitmask to set, + * see `MG_F_*` flags definitions. + */ +struct mg_add_sock_opts { + void *user_data; /* Initial value for connection's user_data */ + unsigned int flags; /* Initial connection flags */ + const char **error_string; /* Placeholder for the error string */ + struct mg_iface *iface; /* Interface instance */ +}; + +/* + * Creates a connection, associates it with the given socket and event handler + * and adds it to the manager. + * + * For more options see the `mg_add_sock_opt` variant. + */ +struct mg_connection *mg_add_sock(struct mg_mgr *mgr, sock_t sock, + MG_CB(mg_event_handler_t handler, + void *user_data)); + +/* + * Creates a connection, associates it with the given socket and event handler + * and adds to the manager. + * + * See the `mg_add_sock_opts` structure for a description of the options. + */ +struct mg_connection *mg_add_sock_opt(struct mg_mgr *mgr, sock_t sock, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_add_sock_opts opts); + +/* + * Optional parameters to `mg_bind_opt()`. + * + * `flags` is an initial `struct mg_connection::flags` bitmask to set, + * see `MG_F_*` flags definitions. + */ +struct mg_bind_opts { + void *user_data; /* Initial value for connection's user_data */ + unsigned int flags; /* Extra connection flags */ + const char **error_string; /* Placeholder for the error string */ + struct mg_iface *iface; /* Interface instance */ +#if MG_ENABLE_SSL + /* + * SSL settings. + * + * Server certificate to present to clients or client certificate to + * present to tunnel dispatcher (for tunneled connections). + */ + const char *ssl_cert; + /* Private key corresponding to the certificate. If ssl_cert is set but + * ssl_key is not, ssl_cert is used. */ + const char *ssl_key; + /* CA bundle used to verify client certificates or tunnel dispatchers. */ + const char *ssl_ca_cert; + /* Colon-delimited list of acceptable cipher suites. + * Names depend on the library used, for example: + * + * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL) + * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256 + * (mbedTLS) + * + * For OpenSSL the list can be obtained by running "openssl ciphers". + * For mbedTLS, names can be found in library/ssl_ciphersuites.c + * If NULL, a reasonable default is used. + */ + const char *ssl_cipher_suites; +#endif +}; + +/* + * Creates a listening connection. + * + * See `mg_bind_opt` for full documentation. + */ +struct mg_connection *mg_bind(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t handler, + void *user_data)); +/* + * Creates a listening connection. + * + * The `address` parameter specifies which address to bind to. It's format is + * the same as for the `mg_connect()` call, where `HOST` part is optional. + * `address` can be just a port number, e.g. `:8000`. To bind to a specific + * interface, an IP address can be specified, e.g. `1.2.3.4:8000`. By default, + * a TCP connection is created. To create UDP connection, prepend `udp://` + * prefix, e.g. `udp://:8000`. To summarize, `address` parameter has following + * format: `[PROTO://][IP_ADDRESS]:PORT`, where `PROTO` could be `tcp` or + * `udp`. + * + * See the `mg_bind_opts` structure for a description of the optional + * parameters. + * + * Returns a new listening connection or `NULL` on error. + * NOTE: The connection remains owned by the manager, do not free(). + */ +struct mg_connection *mg_bind_opt(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_bind_opts opts); + +/* Optional parameters to `mg_connect_opt()` */ +struct mg_connect_opts { + void *user_data; /* Initial value for connection's user_data */ + unsigned int flags; /* Extra connection flags */ + const char **error_string; /* Placeholder for the error string */ + struct mg_iface *iface; /* Interface instance */ + const char *nameserver; /* DNS server to use, NULL for default */ +#if MG_ENABLE_SSL + /* + * SSL settings. + * Client certificate to present to the server. + */ + const char *ssl_cert; + /* + * Private key corresponding to the certificate. + * If ssl_cert is set but ssl_key is not, ssl_cert is used. + */ + const char *ssl_key; + /* + * Verify server certificate using this CA bundle. If set to "*", then SSL + * is enabled but no cert verification is performed. + */ + const char *ssl_ca_cert; + /* Colon-delimited list of acceptable cipher suites. + * Names depend on the library used, for example: + * + * ECDH-ECDSA-AES128-GCM-SHA256:DHE-RSA-AES128-SHA256 (OpenSSL) + * TLS-ECDH-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256 + * (mbedTLS) + * + * For OpenSSL the list can be obtained by running "openssl ciphers". + * For mbedTLS, names can be found in library/ssl_ciphersuites.c + * If NULL, a reasonable default is used. + */ + const char *ssl_cipher_suites; + /* + * Server name verification. If ssl_ca_cert is set and the certificate has + * passed verification, its subject will be verified against this string. + * By default (if ssl_server_name is NULL) hostname part of the address will + * be used. Wildcard matching is supported. A special value of "*" disables + * name verification. + */ + const char *ssl_server_name; + /* + * PSK identity and key. Identity is a NUL-terminated string and key is a hex + * string. Key must be either 16 or 32 bytes (32 or 64 hex digits) for AES-128 + * or AES-256 respectively. + * Note: Default list of cipher suites does not include PSK suites, if you + * want to use PSK you will need to set ssl_cipher_suites as well. + */ + const char *ssl_psk_identity; + const char *ssl_psk_key; +#endif +}; + +/* + * Connects to a remote host. + * + * See `mg_connect_opt()` for full documentation. + */ +struct mg_connection *mg_connect(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t handler, + void *user_data)); + +/* + * Connects to a remote host. + * + * The `address` format is `[PROTO://]HOST:PORT`. `PROTO` could be `tcp` or + * `udp`. `HOST` could be an IP address, + * IPv6 address (if Mongoose is compiled with `-DMG_ENABLE_IPV6`) or a host + * name. If `HOST` is a name, Mongoose will resolve it asynchronously. Examples + * of valid addresses: `google.com:80`, `udp://1.2.3.4:53`, `10.0.0.1:443`, + * `[::1]:80` + * + * See the `mg_connect_opts` structure for a description of the optional + * parameters. + * + * Returns a new outbound connection or `NULL` on error. + * + * NOTE: The connection remains owned by the manager, do not free(). + * + * NOTE: To enable IPv6 addresses `-DMG_ENABLE_IPV6` should be specified + * in the compilation flags. + * + * NOTE: The new connection will receive `MG_EV_CONNECT` as its first event + * which will report the connect success status. + * If the asynchronous resolution fails or the `connect()` syscall fails for + * whatever reason (e.g. with `ECONNREFUSED` or `ENETUNREACH`), then + * `MG_EV_CONNECT` event will report failure. Code example below: + * + * ```c + * static void ev_handler(struct mg_connection *nc, int ev, void *ev_data) { + * int connect_status; + * + * switch (ev) { + * case MG_EV_CONNECT: + * connect_status = * (int *) ev_data; + * if (connect_status == 0) { + * // Success + * } else { + * // Error + * printf("connect() error: %s\n", strerror(connect_status)); + * } + * break; + * ... + * } + * } + * + * ... + * mg_connect(mgr, "my_site.com:80", ev_handler); + * ``` + */ +struct mg_connection *mg_connect_opt(struct mg_mgr *mgr, const char *address, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_connect_opts opts); + +#if MG_ENABLE_SSL && MG_NET_IF != MG_NET_IF_SIMPLELINK +/* + * Note: This function is deprecated. Please, use SSL options in + * mg_connect_opt. + * + * Enables SSL for a given connection. + * `cert` is a server certificate file name for a listening connection + * or a client certificate file name for an outgoing connection. + * The certificate files must be in PEM format. The server certificate file + * must contain a certificate, concatenated with a private key, optionally + * concatenated with DH parameters. + * `ca_cert` is a CA certificate or NULL if peer verification is not + * required. + * Return: NULL on success or error message on error. + */ +const char *mg_set_ssl(struct mg_connection *nc, const char *cert, + const char *ca_cert); +#endif + +/* + * Sends data to the connection. + * + * Note that sending functions do not actually push data to the socket. + * They just append data to the output buffer. MG_EV_SEND will be delivered when + * the data has actually been pushed out. + */ +void mg_send(struct mg_connection *, const void *buf, int len); + +/* Enables format string warnings for mg_printf */ +#if defined(__GNUC__) +__attribute__((format(printf, 2, 3))) +#endif +/* don't separate from mg_printf declaration */ + +/* + * Sends `printf`-style formatted data to the connection. + * + * See `mg_send` for more details on send semantics. + */ +int mg_printf(struct mg_connection *, const char *fmt, ...); + +/* Same as `mg_printf()`, but takes `va_list ap` as an argument. */ +int mg_vprintf(struct mg_connection *, const char *fmt, va_list ap); + +/* + * Creates a socket pair. + * `sock_type` can be either `SOCK_STREAM` or `SOCK_DGRAM`. + * Returns 0 on failure and 1 on success. + */ +int mg_socketpair(sock_t[2], int sock_type); + +#if MG_ENABLE_SYNC_RESOLVER +/* + * Convert domain name into IP address. + * + * This is a utility function. If compilation flags have + * `-DMG_ENABLE_GETADDRINFO`, then `getaddrinfo()` call is used for name + * resolution. Otherwise, `gethostbyname()` is used. + * + * CAUTION: this function can block. + * Return 1 on success, 0 on failure. + */ +int mg_resolve(const char *domain_name, char *ip_addr_buf, size_t buf_len); +#endif + +/* + * Verify given IP address against the ACL. + * + * `remote_ip` - an IPv4 address to check, in host byte order + * `acl` - a comma separated list of IP subnets: `x.x.x.x/x` or `x.x.x.x`. + * Each subnet is + * prepended by either a - or a + sign. A plus sign means allow, where a + * minus sign means deny. If a subnet mask is omitted, such as `-1.2.3.4`, + * it means that only that single IP address is denied. + * Subnet masks may vary from 0 to 32, inclusive. The default setting + * is to allow all access. On each request the full list is traversed, + * and the last match wins. Example: + * + * `-0.0.0.0/0,+192.168/16` - deny all accesses, only allow 192.168/16 subnet + * + * To learn more about subnet masks, see this + * link:https://en.wikipedia.org/wiki/Subnetwork[Wikipedia page on Subnetwork]. + * + * Returns -1 if ACL is malformed, 0 if address is disallowed, 1 if allowed. + */ +int mg_check_ip_acl(const char *acl, uint32_t remote_ip); + +/* + * Schedules an MG_EV_TIMER event to be delivered at `timestamp` time. + * `timestamp` is UNIX time (the number of seconds since Epoch). It is + * `double` instead of `time_t` to allow for sub-second precision. + * Returns the old timer value. + * + * Example: set the connect timeout to 1.5 seconds: + * + * ``` + * c = mg_connect(&mgr, "cesanta.com", ev_handler); + * mg_set_timer(c, mg_time() + 1.5); + * ... + * + * void ev_handler(struct mg_connection *c, int ev, void *ev_data) { + * switch (ev) { + * case MG_EV_CONNECT: + * mg_set_timer(c, 0); // Clear connect timer + * break; + * case MG_EV_TIMER: + * log("Connect timeout"); + * c->flags |= MG_F_CLOSE_IMMEDIATELY; + * break; + * ``` + */ +double mg_set_timer(struct mg_connection *c, double timestamp); + +/* + * A sub-second precision version of time(). + */ +double mg_time(void); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CS_MONGOOSE_SRC_NET_H_ */ diff --git a/src/net_if.c b/src/net_if.c new file mode 100644 index 00000000..1c979666 --- /dev/null +++ b/src/net_if.c @@ -0,0 +1,45 @@ +#include "mongoose/src/net_if.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/net_if_socket.h" +#include "mongoose/src/net_if_tun.h" + +extern const struct mg_iface_vtable mg_default_iface_vtable; + +const struct mg_iface_vtable *mg_ifaces[] = { + &mg_default_iface_vtable, +#if MG_ENABLE_TUN + &mg_tun_iface_vtable, +#endif +}; + +int mg_num_ifaces = (int) (sizeof(mg_ifaces) / sizeof(mg_ifaces[0])); + +struct mg_iface *mg_if_create_iface(const struct mg_iface_vtable *vtable, + struct mg_mgr *mgr) { + struct mg_iface *iface = (struct mg_iface *) MG_CALLOC(1, sizeof(*iface)); + iface->mgr = mgr; + iface->data = NULL; + iface->vtable = vtable; + return iface; +} + +struct mg_iface *mg_find_iface(struct mg_mgr *mgr, + const struct mg_iface_vtable *vtable, + struct mg_iface *from) { + int i = 0; + if (from != NULL) { + for (i = 0; i < mgr->num_ifaces; i++) { + if (mgr->ifaces[i] == from) { + i++; + break; + } + } + } + + for (; i < mgr->num_ifaces; i++) { + if (mgr->ifaces[i]->vtable == vtable) { + return mgr->ifaces[i]; + } + } + return NULL; +} diff --git a/src/net_if.h b/src/net_if.h new file mode 100644 index 00000000..43eb2d99 --- /dev/null +++ b/src/net_if.h @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_NET_IF_H_ +#define CS_MONGOOSE_SRC_NET_IF_H_ + +#include "common/platform.h" + +/* + * Internal async networking core interface. + * Consists of calls made by the core, which should not block, + * and callbacks back into the core ("..._cb"). + * Callbacks may (will) cause methods to be invoked from within, + * but methods are not allowed to invoke callbacks inline. + * + * Implementation must ensure that only one callback is invoked at any time. + */ + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#define MG_MAIN_IFACE 0 + +struct mg_mgr; +struct mg_connection; +union socket_address; + +struct mg_iface_vtable; + +struct mg_iface { + struct mg_mgr *mgr; + void *data; /* Implementation-specific data */ + const struct mg_iface_vtable *vtable; +}; + +struct mg_iface_vtable { + void (*init)(struct mg_iface *iface); + void (*free)(struct mg_iface *iface); + void (*add_conn)(struct mg_connection *nc); + void (*remove_conn)(struct mg_connection *nc); + time_t (*poll)(struct mg_iface *iface, int timeout_ms); + + /* Set up a listening TCP socket on a given address. rv = 0 -> ok. */ + int (*listen_tcp)(struct mg_connection *nc, union socket_address *sa); + /* Request that a "listening" UDP socket be created. */ + int (*listen_udp)(struct mg_connection *nc, union socket_address *sa); + + /* Request that a TCP connection is made to the specified address. */ + void (*connect_tcp)(struct mg_connection *nc, const union socket_address *sa); + /* Open a UDP socket. Doesn't actually connect anything. */ + void (*connect_udp)(struct mg_connection *nc); + + /* Send functions for TCP and UDP. Sent data is copied before return. */ + void (*tcp_send)(struct mg_connection *nc, const void *buf, size_t len); + void (*udp_send)(struct mg_connection *nc, const void *buf, size_t len); + + void (*recved)(struct mg_connection *nc, size_t len); + + /* Perform interface-related connection initialization. Return 1 on ok. */ + int (*create_conn)(struct mg_connection *nc); + /* Perform interface-related cleanup on connection before destruction. */ + void (*destroy_conn)(struct mg_connection *nc); + + /* Associate a socket to a connection. */ + void (*sock_set)(struct mg_connection *nc, sock_t sock); + + /* Put connection's address into *sa, local (remote = 0) or remote. */ + void (*get_conn_addr)(struct mg_connection *nc, int remote, + union socket_address *sa); +}; + +extern const struct mg_iface_vtable *mg_ifaces[]; +extern int mg_num_ifaces; + +/* Creates a new interface instance. */ +struct mg_iface *mg_if_create_iface(const struct mg_iface_vtable *vtable, + struct mg_mgr *mgr); + +/* + * Find an interface with a given implementation. The search is started from + * interface `from`, exclusive. Returns NULL if none is found. + */ +struct mg_iface *mg_find_iface(struct mg_mgr *mgr, + const struct mg_iface_vtable *vtable, + struct mg_iface *from); +/* + * Deliver a new TCP connection. Returns NULL in case on error (unable to + * create connection, in which case interface state should be discarded. + * This is phase 1 of the two-phase process - MG_EV_ACCEPT will be delivered + * when mg_if_accept_tcp_cb is invoked. + */ +struct mg_connection *mg_if_accept_new_conn(struct mg_connection *lc); +void mg_if_accept_tcp_cb(struct mg_connection *nc, union socket_address *sa, + size_t sa_len); + +/* Callback invoked by connect methods. err = 0 -> ok, != 0 -> error. */ +void mg_if_connect_cb(struct mg_connection *nc, int err); +/* Callback that reports that data has been put on the wire. */ +void mg_if_sent_cb(struct mg_connection *nc, int num_sent); +/* + * Receive callback. + * if `own` is true, buf must be heap-allocated and ownership is transferred + * to the core. + * Core will acknowledge consumption by calling iface::recved. + */ +void mg_if_recv_tcp_cb(struct mg_connection *nc, void *buf, int len, int own); +/* + * Receive callback. + * buf must be heap-allocated and ownership is transferred to the core. + * Core will acknowledge consumption by calling iface::recved. + */ +void mg_if_recv_udp_cb(struct mg_connection *nc, void *buf, int len, + union socket_address *sa, size_t sa_len); + +/* void mg_if_close_conn(struct mg_connection *nc); */ + +/* Deliver a POLL event to the connection. */ +void mg_if_poll(struct mg_connection *nc, time_t now); + +/* Deliver a TIMER event to the connection. */ +void mg_if_timer(struct mg_connection *c, double now); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CS_MONGOOSE_SRC_NET_IF_H_ */ diff --git a/src/net_if_socket.c b/src/net_if_socket.c new file mode 100644 index 00000000..2b7bda62 --- /dev/null +++ b/src/net_if_socket.c @@ -0,0 +1,749 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_NET_IF_SOCKET + +#include "mongoose/src/net_if_socket.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/util.h" + +#define MG_TCP_RECV_BUFFER_SIZE 1024 +#define MG_UDP_RECV_BUFFER_SIZE 1500 + +static sock_t mg_open_listening_socket(union socket_address *sa, int type, + int proto); +#if MG_ENABLE_SSL +static void mg_ssl_begin(struct mg_connection *nc); +#endif + +void mg_set_non_blocking_mode(sock_t sock) { +#ifdef _WIN32 + unsigned long on = 1; + ioctlsocket(sock, FIONBIO, &on); +#else + int flags = fcntl(sock, F_GETFL, 0); + fcntl(sock, F_SETFL, flags | O_NONBLOCK); +#endif +} + +static int mg_is_error(void) { + int err = mg_get_errno(); + return err != EINPROGRESS && err != EWOULDBLOCK +#ifndef WINCE + && err != EAGAIN && err != EINTR +#endif +#ifdef _WIN32 + && WSAGetLastError() != WSAEINTR && WSAGetLastError() != WSAEWOULDBLOCK +#endif + ; +} + +void mg_socket_if_connect_tcp(struct mg_connection *nc, + const union socket_address *sa) { + int rc, proto = 0; + nc->sock = socket(AF_INET, SOCK_STREAM, proto); + if (nc->sock == INVALID_SOCKET) { + nc->err = mg_get_errno() ? mg_get_errno() : 1; + return; + } +#if !defined(MG_ESP8266) + mg_set_non_blocking_mode(nc->sock); +#endif + rc = connect(nc->sock, &sa->sa, sizeof(sa->sin)); + nc->err = rc < 0 && mg_is_error() ? mg_get_errno() : 0; + DBG(("%p sock %d rc %d errno %d err %d", nc, nc->sock, rc, mg_get_errno(), + nc->err)); +} + +void mg_socket_if_connect_udp(struct mg_connection *nc) { + nc->sock = socket(AF_INET, SOCK_DGRAM, 0); + if (nc->sock == INVALID_SOCKET) { + nc->err = mg_get_errno() ? mg_get_errno() : 1; + return; + } + if (nc->flags & MG_F_ENABLE_BROADCAST) { + int optval = 1; + if (setsockopt(nc->sock, SOL_SOCKET, SO_BROADCAST, (const char *) &optval, + sizeof(optval)) < 0) { + nc->err = mg_get_errno() ? mg_get_errno() : 1; + return; + } + } + nc->err = 0; +} + +int mg_socket_if_listen_tcp(struct mg_connection *nc, + union socket_address *sa) { + int proto = 0; + sock_t sock = mg_open_listening_socket(sa, SOCK_STREAM, proto); + if (sock == INVALID_SOCKET) { + return (mg_get_errno() ? mg_get_errno() : 1); + } + mg_sock_set(nc, sock); + return 0; +} + +int mg_socket_if_listen_udp(struct mg_connection *nc, + union socket_address *sa) { + sock_t sock = mg_open_listening_socket(sa, SOCK_DGRAM, 0); + if (sock == INVALID_SOCKET) return (mg_get_errno() ? mg_get_errno() : 1); + mg_sock_set(nc, sock); + return 0; +} + +void mg_socket_if_tcp_send(struct mg_connection *nc, const void *buf, + size_t len) { + mbuf_append(&nc->send_mbuf, buf, len); +} + +void mg_socket_if_udp_send(struct mg_connection *nc, const void *buf, + size_t len) { + mbuf_append(&nc->send_mbuf, buf, len); +} + +void mg_socket_if_recved(struct mg_connection *nc, size_t len) { + (void) nc; + (void) len; +} + +int mg_socket_if_create_conn(struct mg_connection *nc) { + (void) nc; + return 1; +} + +void mg_socket_if_destroy_conn(struct mg_connection *nc) { + if (nc->sock == INVALID_SOCKET) return; + if (!(nc->flags & MG_F_UDP)) { + closesocket(nc->sock); + } else { + /* Only close outgoing UDP sockets or listeners. */ + if (nc->listener == NULL) closesocket(nc->sock); + } + nc->sock = INVALID_SOCKET; +} + +static int mg_accept_conn(struct mg_connection *lc) { + struct mg_connection *nc; + union socket_address sa; + socklen_t sa_len = sizeof(sa); + /* NOTE(lsm): on Windows, sock is always > FD_SETSIZE */ + sock_t sock = accept(lc->sock, &sa.sa, &sa_len); + if (sock == INVALID_SOCKET) { + if (mg_is_error()) DBG(("%p: failed to accept: %d", lc, mg_get_errno())); + return 0; + } + nc = mg_if_accept_new_conn(lc); + if (nc == NULL) { + closesocket(sock); + return 0; + } + DBG(("%p conn from %s:%d", nc, inet_ntoa(sa.sin.sin_addr), + ntohs(sa.sin.sin_port))); + mg_sock_set(nc, sock); +#if MG_ENABLE_SSL + if (lc->flags & MG_F_SSL) { + if (mg_ssl_if_conn_accept(nc, lc) != MG_SSL_OK) mg_close_conn(nc); + } else +#endif + { + mg_if_accept_tcp_cb(nc, &sa, sa_len); + } + return 1; +} + +/* 'sa' must be an initialized address to bind to */ +static sock_t mg_open_listening_socket(union socket_address *sa, int type, + int proto) { + socklen_t sa_len = + (sa->sa.sa_family == AF_INET) ? sizeof(sa->sin) : sizeof(sa->sin6); + sock_t sock = INVALID_SOCKET; +#if !MG_LWIP + int on = 1; +#endif + + if ((sock = socket(sa->sa.sa_family, type, proto)) != INVALID_SOCKET && +#if !MG_LWIP /* LWIP doesn't support either */ +#if defined(_WIN32) && defined(SO_EXCLUSIVEADDRUSE) && !defined(WINCE) + /* "Using SO_REUSEADDR and SO_EXCLUSIVEADDRUSE" http://goo.gl/RmrFTm */ + !setsockopt(sock, SOL_SOCKET, SO_EXCLUSIVEADDRUSE, (void *) &on, + sizeof(on)) && +#endif + +#if !defined(_WIN32) || !defined(SO_EXCLUSIVEADDRUSE) + /* + * SO_RESUSEADDR is not enabled on Windows because the semantics of + * SO_REUSEADDR on UNIX and Windows is different. On Windows, + * SO_REUSEADDR allows to bind a socket to a port without error even if + * the port is already open by another program. This is not the behavior + * SO_REUSEADDR was designed for, and leads to hard-to-track failure + * scenarios. Therefore, SO_REUSEADDR was disabled on Windows unless + * SO_EXCLUSIVEADDRUSE is supported and set on a socket. + */ + !setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, (void *) &on, sizeof(on)) && +#endif +#endif /* !MG_LWIP */ + + !bind(sock, &sa->sa, sa_len) && + (type == SOCK_DGRAM || listen(sock, SOMAXCONN) == 0)) { +#if !MG_LWIP + mg_set_non_blocking_mode(sock); + /* In case port was set to 0, get the real port number */ + (void) getsockname(sock, &sa->sa, &sa_len); +#endif + } else if (sock != INVALID_SOCKET) { + closesocket(sock); + sock = INVALID_SOCKET; + } + + return sock; +} + +static void mg_write_to_socket(struct mg_connection *nc) { + struct mbuf *io = &nc->send_mbuf; + int n = 0; + +#if MG_LWIP + /* With LWIP we don't know if the socket is ready */ + if (io->len == 0) return; +#endif + + assert(io->len > 0); + + if (nc->flags & MG_F_UDP) { + int n = + sendto(nc->sock, io->buf, io->len, 0, &nc->sa.sa, sizeof(nc->sa.sin)); + DBG(("%p %d %d %d %s:%hu", nc, nc->sock, n, mg_get_errno(), + inet_ntoa(nc->sa.sin.sin_addr), ntohs(nc->sa.sin.sin_port))); + mg_if_sent_cb(nc, n); + return; + } + +#if MG_ENABLE_SSL + if (nc->flags & MG_F_SSL) { + if (nc->flags & MG_F_SSL_HANDSHAKE_DONE) { + n = mg_ssl_if_write(nc, io->buf, io->len); + DBG(("%p %d bytes -> %d (SSL)", nc, n, nc->sock)); + if (n < 0) { + if (n != MG_SSL_WANT_READ && n != MG_SSL_WANT_WRITE) { + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + return; + } else { + /* Successful SSL operation, clear off SSL wait flags */ + nc->flags &= ~(MG_F_WANT_READ | MG_F_WANT_WRITE); + } + } else { + mg_ssl_begin(nc); + return; + } + } else +#endif + { + n = (int) MG_SEND_FUNC(nc->sock, io->buf, io->len, 0); + DBG(("%p %d bytes -> %d", nc, n, nc->sock)); + } + + mg_if_sent_cb(nc, n); +} + +MG_INTERNAL size_t recv_avail_size(struct mg_connection *conn, size_t max) { + size_t avail; + if (conn->recv_mbuf_limit < conn->recv_mbuf.len) return 0; + avail = conn->recv_mbuf_limit - conn->recv_mbuf.len; + return avail > max ? max : avail; +} + +static void mg_handle_tcp_read(struct mg_connection *conn) { + int n = 0; + char *buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE); + + if (buf == NULL) { + DBG(("OOM")); + return; + } + +#if MG_ENABLE_SSL + if (conn->flags & MG_F_SSL) { + if (conn->flags & MG_F_SSL_HANDSHAKE_DONE) { + /* SSL library may have more bytes ready to read than we ask to read. + * Therefore, read in a loop until we read everything. Without the loop, + * we skip to the next select() cycle which can just timeout. */ + while ((n = mg_ssl_if_read(conn, buf, MG_TCP_RECV_BUFFER_SIZE)) > 0) { + DBG(("%p %d bytes <- %d (SSL)", conn, n, conn->sock)); + mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */); + buf = NULL; + if (conn->flags & MG_F_CLOSE_IMMEDIATELY) break; + /* buf has been freed, we need a new one. */ + buf = (char *) MG_MALLOC(MG_TCP_RECV_BUFFER_SIZE); + if (buf == NULL) break; + } + MG_FREE(buf); + if (n < 0 && n != MG_SSL_WANT_READ) conn->flags |= MG_F_CLOSE_IMMEDIATELY; + } else { + MG_FREE(buf); + mg_ssl_begin(conn); + return; + } + } else +#endif + { + n = (int) MG_RECV_FUNC(conn->sock, buf, + recv_avail_size(conn, MG_TCP_RECV_BUFFER_SIZE), 0); + DBG(("%p %d bytes (PLAIN) <- %d", conn, n, conn->sock)); + if (n > 0) { + mg_if_recv_tcp_cb(conn, buf, n, 1 /* own */); + } else { + MG_FREE(buf); + } + if (n == 0) { + /* Orderly shutdown of the socket, try flushing output. */ + conn->flags |= MG_F_SEND_AND_CLOSE; + } else if (n < 0 && mg_is_error()) { + conn->flags |= MG_F_CLOSE_IMMEDIATELY; + } + } +} + +static int mg_recvfrom(struct mg_connection *nc, union socket_address *sa, + socklen_t *sa_len, char **buf) { + int n; + *buf = (char *) MG_MALLOC(MG_UDP_RECV_BUFFER_SIZE); + if (*buf == NULL) { + DBG(("Out of memory")); + return -ENOMEM; + } + n = recvfrom(nc->sock, *buf, MG_UDP_RECV_BUFFER_SIZE, 0, &sa->sa, sa_len); + if (n <= 0) { + DBG(("%p recvfrom: %s", nc, strerror(mg_get_errno()))); + MG_FREE(*buf); + } + return n; +} + +static void mg_handle_udp_read(struct mg_connection *nc) { + char *buf = NULL; + union socket_address sa; + socklen_t sa_len = sizeof(sa); + int n = mg_recvfrom(nc, &sa, &sa_len, &buf); + DBG(("%p %d bytes from %s:%d", nc, n, inet_ntoa(nc->sa.sin.sin_addr), + ntohs(nc->sa.sin.sin_port))); + mg_if_recv_udp_cb(nc, buf, n, &sa, sa_len); +} + +#if MG_ENABLE_SSL +static void mg_ssl_begin(struct mg_connection *nc) { + int server_side = (nc->listener != NULL); + enum mg_ssl_if_result res = mg_ssl_if_handshake(nc); + DBG(("%p %d res %d", nc, server_side, res)); + + if (res == MG_SSL_OK) { + nc->flags |= MG_F_SSL_HANDSHAKE_DONE; + nc->flags &= ~(MG_F_WANT_READ | MG_F_WANT_WRITE); + + if (server_side) { + union socket_address sa; + socklen_t sa_len = sizeof(sa); + (void) getpeername(nc->sock, &sa.sa, &sa_len); + mg_if_accept_tcp_cb(nc, &sa, sa_len); + } else { + mg_if_connect_cb(nc, 0); + } + } else if (res != MG_SSL_WANT_READ && res != MG_SSL_WANT_WRITE) { + if (!server_side) { + mg_if_connect_cb(nc, res); + } + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +} +#endif /* MG_ENABLE_SSL */ + +#define _MG_F_FD_CAN_READ 1 +#define _MG_F_FD_CAN_WRITE 1 << 1 +#define _MG_F_FD_ERROR 1 << 2 + +void mg_mgr_handle_conn(struct mg_connection *nc, int fd_flags, double now) { + int worth_logging = + fd_flags != 0 || (nc->flags & (MG_F_WANT_READ | MG_F_WANT_WRITE)); + if (worth_logging) { + DBG(("%p fd=%d fd_flags=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, + fd_flags, nc->flags, (int) nc->recv_mbuf.len, + (int) nc->send_mbuf.len)); + } + + if (nc->flags & MG_F_CONNECTING) { + if (fd_flags != 0) { + int err = 0; +#if !defined(MG_ESP8266) + if (!(nc->flags & MG_F_UDP)) { + socklen_t len = sizeof(err); + int ret = + getsockopt(nc->sock, SOL_SOCKET, SO_ERROR, (char *) &err, &len); + if (ret != 0) { + err = 1; + } else if (err == EAGAIN || err == EWOULDBLOCK) { + err = 0; + } + } +#else + /* + * On ESP8266 we use blocking connect. + */ + err = nc->err; +#endif +#if MG_ENABLE_SSL + if ((nc->flags & MG_F_SSL) && err == 0) { + mg_ssl_begin(nc); + } else { + mg_if_connect_cb(nc, err); + } +#else + mg_if_connect_cb(nc, err); +#endif + } else if (nc->err != 0) { + mg_if_connect_cb(nc, nc->err); + } + } + + if (fd_flags & _MG_F_FD_CAN_READ) { + if (nc->flags & MG_F_UDP) { + mg_handle_udp_read(nc); + } else { + if (nc->flags & MG_F_LISTENING) { + /* + * We're not looping here, and accepting just one connection at + * a time. The reason is that eCos does not respect non-blocking + * flag on a listening socket and hangs in a loop. + */ + mg_accept_conn(nc); + } else { + mg_handle_tcp_read(nc); + } + } + } + + if (!(nc->flags & MG_F_CLOSE_IMMEDIATELY)) { + if ((fd_flags & _MG_F_FD_CAN_WRITE) && nc->send_mbuf.len > 0) { + mg_write_to_socket(nc); + } + mg_if_poll(nc, (time_t) now); + mg_if_timer(nc, now); + } + + if (worth_logging) { + DBG(("%p after fd=%d nc_flags=%lu rmbl=%d smbl=%d", nc, nc->sock, nc->flags, + (int) nc->recv_mbuf.len, (int) nc->send_mbuf.len)); + } +} + +#if MG_ENABLE_BROADCAST +static void mg_mgr_handle_ctl_sock(struct mg_mgr *mgr) { + struct ctl_msg ctl_msg; + int len = + (int) MG_RECV_FUNC(mgr->ctl[1], (char *) &ctl_msg, sizeof(ctl_msg), 0); + size_t dummy = MG_SEND_FUNC(mgr->ctl[1], ctl_msg.message, 1, 0); + DBG(("read %d from ctl socket", len)); + (void) dummy; /* https://gcc.gnu.org/bugzilla/show_bug.cgi?id=25509 */ + if (len >= (int) sizeof(ctl_msg.callback) && ctl_msg.callback != NULL) { + struct mg_connection *nc; + for (nc = mg_next(mgr, NULL); nc != NULL; nc = mg_next(mgr, nc)) { + ctl_msg.callback(nc, MG_EV_POLL, + ctl_msg.message MG_UD_ARG(nc->user_data)); + } + } +} +#endif + +/* Associate a socket to a connection. */ +void mg_socket_if_sock_set(struct mg_connection *nc, sock_t sock) { + mg_set_non_blocking_mode(sock); + mg_set_close_on_exec(sock); + nc->sock = sock; + DBG(("%p %d", nc, sock)); +} + +void mg_socket_if_init(struct mg_iface *iface) { + (void) iface; + DBG(("%p using select()", iface->mgr)); +#if MG_ENABLE_BROADCAST + mg_socketpair(iface->mgr->ctl, SOCK_DGRAM); +#endif +} + +void mg_socket_if_free(struct mg_iface *iface) { + (void) iface; +} + +void mg_socket_if_add_conn(struct mg_connection *nc) { + (void) nc; +} + +void mg_socket_if_remove_conn(struct mg_connection *nc) { + (void) nc; +} + +void mg_add_to_set(sock_t sock, fd_set *set, sock_t *max_fd) { + if (sock != INVALID_SOCKET +#ifdef __unix__ + && sock < (sock_t) FD_SETSIZE +#endif + ) { + FD_SET(sock, set); + if (*max_fd == INVALID_SOCKET || sock > *max_fd) { + *max_fd = sock; + } + } +} + +time_t mg_socket_if_poll(struct mg_iface *iface, int timeout_ms) { + struct mg_mgr *mgr = iface->mgr; + double now = mg_time(); + double min_timer; + struct mg_connection *nc, *tmp; + struct timeval tv; + fd_set read_set, write_set, err_set; + sock_t max_fd = INVALID_SOCKET; + int num_fds, num_ev, num_timers = 0; +#ifdef __unix__ + int try_dup = 1; +#endif + + FD_ZERO(&read_set); + FD_ZERO(&write_set); + FD_ZERO(&err_set); +#if MG_ENABLE_BROADCAST + mg_add_to_set(mgr->ctl[1], &read_set, &max_fd); +#endif + + /* + * Note: it is ok to have connections with sock == INVALID_SOCKET in the list, + * e.g. timer-only "connections". + */ + min_timer = 0; + for (nc = mgr->active_connections, num_fds = 0; nc != NULL; nc = tmp) { + tmp = nc->next; + + if (nc->sock != INVALID_SOCKET) { + num_fds++; + +#ifdef __unix__ + /* A hack to make sure all our file descriptos fit into FD_SETSIZE. */ + if (nc->sock >= (sock_t) FD_SETSIZE && try_dup) { + int new_sock = dup(nc->sock); + if (new_sock >= 0) { + if (new_sock < (sock_t) FD_SETSIZE) { + closesocket(nc->sock); + DBG(("new sock %d -> %d", nc->sock, new_sock)); + nc->sock = new_sock; + } else { + closesocket(new_sock); + DBG(("new sock is still larger than FD_SETSIZE, disregard")); + try_dup = 0; + } + } else { + try_dup = 0; + } + } +#endif + + if (!(nc->flags & MG_F_WANT_WRITE) && + nc->recv_mbuf.len < nc->recv_mbuf_limit && + (!(nc->flags & MG_F_UDP) || nc->listener == NULL)) { + mg_add_to_set(nc->sock, &read_set, &max_fd); + } + + if (((nc->flags & MG_F_CONNECTING) && !(nc->flags & MG_F_WANT_READ)) || + (nc->send_mbuf.len > 0 && !(nc->flags & MG_F_CONNECTING))) { + mg_add_to_set(nc->sock, &write_set, &max_fd); + mg_add_to_set(nc->sock, &err_set, &max_fd); + } + } + + if (nc->ev_timer_time > 0) { + if (num_timers == 0 || nc->ev_timer_time < min_timer) { + min_timer = nc->ev_timer_time; + } + num_timers++; + } + } + + /* + * If there is a timer to be fired earlier than the requested timeout, + * adjust the timeout. + */ + if (num_timers > 0) { + double timer_timeout_ms = (min_timer - mg_time()) * 1000 + 1 /* rounding */; + if (timer_timeout_ms < timeout_ms) { + timeout_ms = (int) timer_timeout_ms; + } + } + if (timeout_ms < 0) timeout_ms = 0; + + tv.tv_sec = timeout_ms / 1000; + tv.tv_usec = (timeout_ms % 1000) * 1000; + + num_ev = select((int) max_fd + 1, &read_set, &write_set, &err_set, &tv); + now = mg_time(); +#if 0 + DBG(("select @ %ld num_ev=%d of %d, timeout=%d", (long) now, num_ev, num_fds, + timeout_ms)); +#endif + +#if MG_ENABLE_BROADCAST + if (num_ev > 0 && mgr->ctl[1] != INVALID_SOCKET && + FD_ISSET(mgr->ctl[1], &read_set)) { + mg_mgr_handle_ctl_sock(mgr); + } +#endif + + for (nc = mgr->active_connections; nc != NULL; nc = tmp) { + int fd_flags = 0; + if (nc->sock != INVALID_SOCKET) { + if (num_ev > 0) { + fd_flags = (FD_ISSET(nc->sock, &read_set) && + (!(nc->flags & MG_F_UDP) || nc->listener == NULL) + ? _MG_F_FD_CAN_READ + : 0) | + (FD_ISSET(nc->sock, &write_set) ? _MG_F_FD_CAN_WRITE : 0) | + (FD_ISSET(nc->sock, &err_set) ? _MG_F_FD_ERROR : 0); + } +#if MG_LWIP + /* With LWIP socket emulation layer, we don't get write events for UDP */ + if ((nc->flags & MG_F_UDP) && nc->listener == NULL) { + fd_flags |= _MG_F_FD_CAN_WRITE; + } +#endif + } + tmp = nc->next; + mg_mgr_handle_conn(nc, fd_flags, now); + } + + for (nc = mgr->active_connections; nc != NULL; nc = tmp) { + tmp = nc->next; + if ((nc->flags & MG_F_CLOSE_IMMEDIATELY) || + (nc->send_mbuf.len == 0 && (nc->flags & MG_F_SEND_AND_CLOSE))) { + mg_close_conn(nc); + } + } + + return (time_t) now; +} + +#if MG_ENABLE_BROADCAST +MG_INTERNAL void mg_socketpair_close(sock_t *sock) { + while (1) { + if (closesocket(*sock) == -1 && errno == EINTR) continue; + break; + } + *sock = INVALID_SOCKET; +} + +MG_INTERNAL sock_t +mg_socketpair_accept(sock_t sock, union socket_address *sa, socklen_t sa_len) { + sock_t rc; + while (1) { + if ((rc = accept(sock, &sa->sa, &sa_len)) == INVALID_SOCKET && + errno == EINTR) + continue; + break; + } + return rc; +} + +int mg_socketpair(sock_t sp[2], int sock_type) { + union socket_address sa; + sock_t sock; + socklen_t len = sizeof(sa.sin); + int ret = 0; + + sock = sp[0] = sp[1] = INVALID_SOCKET; + + (void) memset(&sa, 0, sizeof(sa)); + sa.sin.sin_family = AF_INET; + sa.sin.sin_port = htons(0); + sa.sin.sin_addr.s_addr = htonl(0x7f000001); /* 127.0.0.1 */ + + if ((sock = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { + } else if (bind(sock, &sa.sa, len) != 0) { + } else if (sock_type == SOCK_STREAM && listen(sock, 1) != 0) { + } else if (getsockname(sock, &sa.sa, &len) != 0) { + } else if ((sp[0] = socket(AF_INET, sock_type, 0)) == INVALID_SOCKET) { + } else if (connect(sp[0], &sa.sa, len) != 0) { + } else if (sock_type == SOCK_DGRAM && + (getsockname(sp[0], &sa.sa, &len) != 0 || + connect(sock, &sa.sa, len) != 0)) { + } else if ((sp[1] = (sock_type == SOCK_DGRAM ? sock : mg_socketpair_accept( + sock, &sa, len))) == + INVALID_SOCKET) { + } else { + mg_set_close_on_exec(sp[0]); + mg_set_close_on_exec(sp[1]); + if (sock_type == SOCK_STREAM) mg_socketpair_close(&sock); + ret = 1; + } + + if (!ret) { + if (sp[0] != INVALID_SOCKET) mg_socketpair_close(&sp[0]); + if (sp[1] != INVALID_SOCKET) mg_socketpair_close(&sp[1]); + if (sock != INVALID_SOCKET) mg_socketpair_close(&sock); + } + + return ret; +} +#endif /* MG_ENABLE_BROADCAST */ + +static void mg_sock_get_addr(sock_t sock, int remote, + union socket_address *sa) { + socklen_t slen = sizeof(*sa); + memset(sa, 0, slen); + if (remote) { + getpeername(sock, &sa->sa, &slen); + } else { + getsockname(sock, &sa->sa, &slen); + } +} + +void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags) { + union socket_address sa; + mg_sock_get_addr(sock, flags & MG_SOCK_STRINGIFY_REMOTE, &sa); + mg_sock_addr_to_str(&sa, buf, len, flags); +} + +void mg_socket_if_get_conn_addr(struct mg_connection *nc, int remote, + union socket_address *sa) { + if ((nc->flags & MG_F_UDP) && remote) { + memcpy(sa, &nc->sa, sizeof(*sa)); + return; + } + mg_sock_get_addr(nc->sock, remote, sa); +} + +/* clang-format off */ +#define MG_SOCKET_IFACE_VTABLE \ + { \ + mg_socket_if_init, \ + mg_socket_if_free, \ + mg_socket_if_add_conn, \ + mg_socket_if_remove_conn, \ + mg_socket_if_poll, \ + mg_socket_if_listen_tcp, \ + mg_socket_if_listen_udp, \ + mg_socket_if_connect_tcp, \ + mg_socket_if_connect_udp, \ + mg_socket_if_tcp_send, \ + mg_socket_if_udp_send, \ + mg_socket_if_recved, \ + mg_socket_if_create_conn, \ + mg_socket_if_destroy_conn, \ + mg_socket_if_sock_set, \ + mg_socket_if_get_conn_addr, \ + } +/* clang-format on */ + +const struct mg_iface_vtable mg_socket_iface_vtable = MG_SOCKET_IFACE_VTABLE; +#if MG_NET_IF == MG_NET_IF_SOCKET +const struct mg_iface_vtable mg_default_iface_vtable = MG_SOCKET_IFACE_VTABLE; +#endif + +#endif /* MG_ENABLE_NET_IF_SOCKET */ diff --git a/src/net_if_socket.h b/src/net_if_socket.h new file mode 100644 index 00000000..50afe5d6 --- /dev/null +++ b/src/net_if_socket.h @@ -0,0 +1,25 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKET_H_ +#define CS_MONGOOSE_SRC_NET_IF_SOCKET_H_ + +#include "mongoose/src/net_if.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef MG_ENABLE_NET_IF_SOCKET +#define MG_ENABLE_NET_IF_SOCKET MG_NET_IF == MG_NET_IF_SOCKET +#endif + +extern const struct mg_iface_vtable mg_socket_iface_vtable; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* CS_MONGOOSE_SRC_NET_IF_SOCKET_H_ */ diff --git a/src/net_if_socks.c b/src/net_if_socks.c new file mode 100644 index 00000000..c5661e97 --- /dev/null +++ b/src/net_if_socks.c @@ -0,0 +1,209 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_SOCKS + +struct socksdata { + char *proxy_addr; /* HOST:PORT of the socks5 proxy server */ + struct mg_connection *s; /* Respective connection to the server */ + struct mg_connection *c; /* Connection to the client */ + struct mbuf tmp; /* Temporary buffer for sent data */ +}; + +static void socks_if_disband(struct socksdata *d) { + LOG(LL_DEBUG, ("disbanding proxy %p %p", d->c, d->s)); + if (d->c) d->c->flags |= MG_F_SEND_AND_CLOSE; + if (d->s) d->s->flags |= MG_F_SEND_AND_CLOSE; + d->c = d->s = NULL; +} + +static void socks_if_handler(struct mg_connection *c, int ev, void *ev_data) { + struct socksdata *d = (struct socksdata *) c->user_data; + if (ev == MG_EV_CONNECT) { + int res = *(int *) ev_data; + if (res == 0) { + /* Send handshake to the proxy server */ + unsigned char buf[] = {MG_SOCKS_VERSION, 1, MG_SOCKS_HANDSHAKE_NOAUTH}; + mg_send(d->s, buf, sizeof(buf)); + LOG(LL_DEBUG, ("Sent handshake to %s", d->proxy_addr)); + } else { + LOG(LL_ERROR, ("Cannot connect to %s: %d", d->proxy_addr, res)); + d->c->flags |= MG_F_CLOSE_IMMEDIATELY; + } + } else if (ev == MG_EV_CLOSE) { + socks_if_disband(d); + } else if (ev == MG_EV_RECV) { + /* Handle handshake reply */ + if (!(c->flags & MG_SOCKS_HANDSHAKE_DONE)) { + /* TODO(lsm): process IPv6 too */ + unsigned char buf[10] = {MG_SOCKS_VERSION, MG_SOCKS_CMD_CONNECT, 0, + MG_SOCKS_ADDR_IPV4}; + if (c->recv_mbuf.len < 2) return; + if ((unsigned char) c->recv_mbuf.buf[1] == MG_SOCKS_HANDSHAKE_FAILURE) { + LOG(LL_ERROR, ("Server kicked us out")); + socks_if_disband(d); + return; + } + mbuf_remove(&c->recv_mbuf, 2); + c->flags |= MG_SOCKS_HANDSHAKE_DONE; + + /* Send connect request */ + memcpy(buf + 4, &d->c->sa.sin.sin_addr, 4); + memcpy(buf + 8, &d->c->sa.sin.sin_port, 2); + mg_send(c, buf, sizeof(buf)); + } + /* Process connect request */ + if ((c->flags & MG_SOCKS_HANDSHAKE_DONE) && + !(c->flags & MG_SOCKS_CONNECT_DONE)) { + if (c->recv_mbuf.len < 10) return; + if (c->recv_mbuf.buf[1] != MG_SOCKS_SUCCESS) { + LOG(LL_ERROR, ("Socks connection error: %d", c->recv_mbuf.buf[1])); + socks_if_disband(d); + return; + } + mbuf_remove(&c->recv_mbuf, 10); + c->flags |= MG_SOCKS_CONNECT_DONE; + /* Connected. Move sent data from client, if any, to server */ + if (d->s && d->c) { + mbuf_append(&d->s->send_mbuf, d->tmp.buf, d->tmp.len); + mbuf_free(&d->tmp); + } + } + /* All flags are set, we're in relay mode */ + if ((c->flags & MG_SOCKS_CONNECT_DONE) && d->c && d->s) { + mbuf_append(&d->c->recv_mbuf, d->s->recv_mbuf.buf, d->s->recv_mbuf.len); + mbuf_remove(&d->s->recv_mbuf, d->s->recv_mbuf.len); + } + } +} + +static void mg_socks_if_connect_tcp(struct mg_connection *c, + const union socket_address *sa) { + struct socksdata *d = (struct socksdata *) c->iface->data; + d->c = c; + d->s = mg_connect(c->mgr, d->proxy_addr, socks_if_handler); + d->s->user_data = d; + LOG(LL_DEBUG, ("%p %s", c, d->proxy_addr)); + (void) sa; +} + +static void mg_socks_if_connect_udp(struct mg_connection *c) { + (void) c; +} + +static int mg_socks_if_listen_tcp(struct mg_connection *c, + union socket_address *sa) { + (void) c; + (void) sa; + return 0; +} + +static int mg_socks_if_listen_udp(struct mg_connection *c, + union socket_address *sa) { + (void) c; + (void) sa; + return -1; +} + +static void mg_socks_if_tcp_send(struct mg_connection *c, const void *buf, + size_t len) { + struct socksdata *d = (struct socksdata *) c->iface->data; + LOG(LL_DEBUG, ("%p -> %p %d %d", c, buf, (int) len, (int) c->send_mbuf.len)); + if (d && d->s && d->s->flags & MG_SOCKS_CONNECT_DONE) { + mbuf_append(&d->s->send_mbuf, d->tmp.buf, d->tmp.len); + mbuf_append(&d->s->send_mbuf, buf, len); + mbuf_free(&d->tmp); + } else { + mbuf_append(&d->tmp, buf, len); + } +} + +static void mg_socks_if_udp_send(struct mg_connection *c, const void *buf, + size_t len) { + (void) c; + (void) buf; + (void) len; +} + +static void mg_socks_if_recved(struct mg_connection *c, size_t len) { + (void) c; + (void) len; +} + +static int mg_socks_if_create_conn(struct mg_connection *c) { + (void) c; + return 1; +} + +static void mg_socks_if_destroy_conn(struct mg_connection *c) { + c->iface->vtable->free(c->iface); + MG_FREE(c->iface); + c->iface = NULL; + LOG(LL_DEBUG, ("%p", c)); +} + +static void mg_socks_if_sock_set(struct mg_connection *c, sock_t sock) { + (void) c; + (void) sock; +} + +static void mg_socks_if_init(struct mg_iface *iface) { + (void) iface; +} + +static void mg_socks_if_free(struct mg_iface *iface) { + struct socksdata *d = (struct socksdata *) iface->data; + LOG(LL_DEBUG, ("%p", iface)); + if (d != NULL) { + socks_if_disband(d); + mbuf_free(&d->tmp); + MG_FREE(d->proxy_addr); + MG_FREE(d); + iface->data = NULL; + } +} + +static void mg_socks_if_add_conn(struct mg_connection *c) { + c->sock = INVALID_SOCKET; +} + +static void mg_socks_if_remove_conn(struct mg_connection *c) { + (void) c; +} + +static time_t mg_socks_if_poll(struct mg_iface *iface, int timeout_ms) { + LOG(LL_DEBUG, ("%p", iface)); + (void) iface; + (void) timeout_ms; + return (time_t) cs_time(); +} + +static void mg_socks_if_get_conn_addr(struct mg_connection *c, int remote, + union socket_address *sa) { + LOG(LL_DEBUG, ("%p", c)); + (void) c; + (void) remote; + (void) sa; +} + +const struct mg_iface_vtable mg_socks_iface_vtable = { + mg_socks_if_init, mg_socks_if_free, + mg_socks_if_add_conn, mg_socks_if_remove_conn, + mg_socks_if_poll, mg_socks_if_listen_tcp, + mg_socks_if_listen_udp, mg_socks_if_connect_tcp, + mg_socks_if_connect_udp, mg_socks_if_tcp_send, + mg_socks_if_udp_send, mg_socks_if_recved, + mg_socks_if_create_conn, mg_socks_if_destroy_conn, + mg_socks_if_sock_set, mg_socks_if_get_conn_addr, +}; + +struct mg_iface *mg_socks_mk_iface(struct mg_mgr *mgr, const char *proxy_addr) { + struct mg_iface *iface = mg_if_create_iface(&mg_socks_iface_vtable, mgr); + iface->data = MG_CALLOC(1, sizeof(struct socksdata)); + ((struct socksdata *) iface->data)->proxy_addr = strdup(proxy_addr); + return iface; +} + +#endif diff --git a/src/net_if_socks.h b/src/net_if_socks.h new file mode 100644 index 00000000..f89c354d --- /dev/null +++ b/src/net_if_socks.h @@ -0,0 +1,22 @@ +/* +* Copyright (c) 2014-2017 Cesanta Software Limited +* All rights reserved +*/ + +#ifndef CS_MONGOOSE_SRC_NET_IF_SOCKS_H_ +#define CS_MONGOOSE_SRC_NET_IF_SOCKS_H_ + +#if MG_ENABLE_SOCKS +#include "mongoose/src/net_if.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +extern const struct mg_iface_vtable mg_socks_iface_vtable; + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* MG_ENABLE_SOCKS */ +#endif /* CS_MONGOOSE_SRC_NET_IF_SOCKS_H_ */ diff --git a/src/net_if_tun.c b/src/net_if_tun.c new file mode 100644 index 00000000..9785eb85 --- /dev/null +++ b/src/net_if_tun.c @@ -0,0 +1,171 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_TUN + +#include "common/cs_dbg.h" +#include "common/cs_time.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/net_if_tun.h" +#include "mongoose/src/tun.h" +#include "mongoose/src/util.h" + +#define MG_TCP_RECV_BUFFER_SIZE 1024 +#define MG_UDP_RECV_BUFFER_SIZE 1500 + +void mg_tun_if_connect_tcp(struct mg_connection *nc, + const union socket_address *sa) { + (void) nc; + (void) sa; +} + +void mg_tun_if_connect_udp(struct mg_connection *nc) { + (void) nc; +} + +int mg_tun_if_listen_tcp(struct mg_connection *nc, union socket_address *sa) { + (void) nc; + (void) sa; + return 0; +} + +int mg_tun_if_listen_udp(struct mg_connection *nc, union socket_address *sa) { + (void) nc; + (void) sa; + return -1; +} + +void mg_tun_if_tcp_send(struct mg_connection *nc, const void *buf, size_t len) { + struct mg_tun_client *client = (struct mg_tun_client *) nc->iface->data; + uint32_t stream_id = (uint32_t)(uintptr_t) nc->mgr_data; + struct mg_str msg = {(char *) buf, len}; +#if MG_ENABLE_HEXDUMP + char hex[512]; + mg_hexdump(buf, len, hex, sizeof(hex)); + LOG(LL_DEBUG, ("sending to stream 0x%x:\n%s", (unsigned int) stream_id, hex)); +#endif + + mg_tun_send_frame(client->disp, stream_id, MG_TUN_DATA_FRAME, 0, msg); +} + +void mg_tun_if_udp_send(struct mg_connection *nc, const void *buf, size_t len) { + (void) nc; + (void) buf; + (void) len; +} + +void mg_tun_if_recved(struct mg_connection *nc, size_t len) { + (void) nc; + (void) len; +} + +int mg_tun_if_create_conn(struct mg_connection *nc) { + (void) nc; + return 1; +} + +void mg_tun_if_destroy_conn(struct mg_connection *nc) { + struct mg_tun_client *client = (struct mg_tun_client *) nc->iface->data; + + if (nc->flags & MG_F_LISTENING) { + mg_tun_destroy_client(client); + } else if (client->disp) { + uint32_t stream_id = (uint32_t)(uintptr_t) nc->mgr_data; + struct mg_str msg = {NULL, 0}; + + LOG(LL_DEBUG, ("closing 0x%x:", (unsigned int) stream_id)); + mg_tun_send_frame(client->disp, stream_id, MG_TUN_DATA_FRAME, + MG_TUN_F_END_STREAM, msg); + } +} + +/* Associate a socket to a connection. */ +void mg_tun_if_sock_set(struct mg_connection *nc, sock_t sock) { + (void) nc; + (void) sock; +} + +void mg_tun_if_init(struct mg_iface *iface) { + (void) iface; +} + +void mg_tun_if_free(struct mg_iface *iface) { + (void) iface; +} + +void mg_tun_if_add_conn(struct mg_connection *nc) { + nc->sock = INVALID_SOCKET; +} + +void mg_tun_if_remove_conn(struct mg_connection *nc) { + (void) nc; +} + +time_t mg_tun_if_poll(struct mg_iface *iface, int timeout_ms) { + (void) iface; + (void) timeout_ms; + return (time_t) cs_time(); +} + +void mg_tun_if_get_conn_addr(struct mg_connection *nc, int remote, + union socket_address *sa) { + (void) nc; + (void) remote; + (void) sa; +} + +struct mg_connection *mg_tun_if_find_conn(struct mg_tun_client *client, + uint32_t stream_id) { + struct mg_connection *nc = NULL; + + for (nc = client->mgr->active_connections; nc != NULL; nc = nc->next) { + if (nc->iface != client->iface || (nc->flags & MG_F_LISTENING)) { + continue; + } + if (stream_id == (uint32_t)(uintptr_t) nc->mgr_data) { + return nc; + } + } + + if (stream_id > client->last_stream_id) { + /* create a new connection */ + LOG(LL_DEBUG, ("new stream 0x%x, accepting", (unsigned int) stream_id)); + nc = mg_if_accept_new_conn(client->listener); + nc->mgr_data = (void *) (uintptr_t) stream_id; + client->last_stream_id = stream_id; + } else { + LOG(LL_DEBUG, + ("Ignoring stream 0x%x (last_stream_id 0x%x)", (unsigned int) stream_id, + (unsigned int) client->last_stream_id)); + } + + return nc; +} + +/* clang-format off */ +#define MG_TUN_IFACE_VTABLE \ + { \ + mg_tun_if_init, \ + mg_tun_if_free, \ + mg_tun_if_add_conn, \ + mg_tun_if_remove_conn, \ + mg_tun_if_poll, \ + mg_tun_if_listen_tcp, \ + mg_tun_if_listen_udp, \ + mg_tun_if_connect_tcp, \ + mg_tun_if_connect_udp, \ + mg_tun_if_tcp_send, \ + mg_tun_if_udp_send, \ + mg_tun_if_recved, \ + mg_tun_if_create_conn, \ + mg_tun_if_destroy_conn, \ + mg_tun_if_sock_set, \ + mg_tun_if_get_conn_addr, \ + } +/* clang-format on */ + +const struct mg_iface_vtable mg_tun_iface_vtable = MG_TUN_IFACE_VTABLE; + +#endif /* MG_ENABLE_TUN */ diff --git a/src/net_if_tun.h b/src/net_if_tun.h new file mode 100644 index 00000000..f9830cc0 --- /dev/null +++ b/src/net_if_tun.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_NET_IF_TUN_H_ +#define CS_MONGOOSE_SRC_NET_IF_TUN_H_ + +#if MG_ENABLE_TUN + +#include "mongoose/src/net_if.h" + +struct mg_tun_client; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +extern const struct mg_iface_vtable mg_tun_iface_vtable; + +struct mg_connection *mg_tun_if_find_conn(struct mg_tun_client *client, + uint32_t stream_id); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_TUN */ + +#endif /* CS_MONGOOSE_SRC_NET_IF_TUN_H_ */ diff --git a/src/resolv.c b/src/resolv.c new file mode 100644 index 00000000..1241d88d --- /dev/null +++ b/src/resolv.c @@ -0,0 +1,292 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_ASYNC_RESOLVER + +#include "mongoose/src/internal.h" +#include "mongoose/src/resolv.h" + +#ifndef MG_DEFAULT_NAMESERVER +#define MG_DEFAULT_NAMESERVER "8.8.8.8" +#endif + +struct mg_resolve_async_request { + char name[1024]; + int query; + mg_resolve_callback_t callback; + void *data; + time_t timeout; + int max_retries; + enum mg_resolve_err err; + + /* state */ + time_t last_time; + int retries; +}; + +/* + * Find what nameserver to use. + * + * Return 0 if OK, -1 if error + */ +static int mg_get_ip_address_of_nameserver(char *name, size_t name_len) { + int ret = -1; + +#ifdef _WIN32 + int i; + LONG err; + HKEY hKey, hSub; + wchar_t subkey[512], value[128], + *key = L"SYSTEM\\ControlSet001\\Services\\Tcpip\\Parameters\\Interfaces"; + + if ((err = RegOpenKeyExW(HKEY_LOCAL_MACHINE, key, 0, KEY_READ, &hKey)) != + ERROR_SUCCESS) { + fprintf(stderr, "cannot open reg key %S: %ld\n", key, err); + ret = -1; + } else { + for (ret = -1, i = 0; 1; i++) { + DWORD subkey_size = sizeof(subkey), type, len = sizeof(value); + if (RegEnumKeyExW(hKey, i, subkey, &subkey_size, NULL, NULL, NULL, + NULL) != ERROR_SUCCESS) { + break; + } + if (RegOpenKeyExW(hKey, subkey, 0, KEY_READ, &hSub) == ERROR_SUCCESS && + ((RegQueryValueExW(hSub, L"NameServer", 0, &type, (void *) value, + &len) == ERROR_SUCCESS && + value[0] != '\0') || + (RegQueryValueExW(hSub, L"DhcpNameServer", 0, &type, (void *) value, + &len) == ERROR_SUCCESS && + value[0] != '\0'))) { + /* + * See https://github.com/cesanta/mongoose/issues/176 + * The value taken from the registry can be empty, a single + * IP address, or multiple IP addresses separated by comma. + * If it's empty, check the next interface. + * If it's multiple IP addresses, take the first one. + */ + wchar_t *comma = wcschr(value, ','); + if (comma != NULL) { + *comma = '\0'; + } + /* %S will convert wchar_t -> char */ + snprintf(name, name_len, "%S", value); + ret = 0; + RegCloseKey(hSub); + break; + } + } + RegCloseKey(hKey); + } +#elif MG_ENABLE_FILESYSTEM && defined(MG_RESOLV_CONF_FILE_NAME) + FILE *fp; + char line[512]; + + if ((fp = mg_fopen(MG_RESOLV_CONF_FILE_NAME, "r")) == NULL) { + ret = -1; + } else { + /* Try to figure out what nameserver to use */ + for (ret = -1; fgets(line, sizeof(line), fp) != NULL;) { + unsigned int a, b, c, d; + if (sscanf(line, "nameserver %u.%u.%u.%u", &a, &b, &c, &d) == 4) { + snprintf(name, name_len, "%u.%u.%u.%u", a, b, c, d); + ret = 0; + break; + } + } + (void) fclose(fp); + } +#else + snprintf(name, name_len, "%s", MG_DEFAULT_NAMESERVER); +#endif /* _WIN32 */ + + return ret; +} + +int mg_resolve_from_hosts_file(const char *name, union socket_address *usa) { +#if MG_ENABLE_FILESYSTEM && defined(MG_HOSTS_FILE_NAME) + /* TODO(mkm) cache /etc/hosts */ + FILE *fp; + char line[1024]; + char *p; + char alias[256]; + unsigned int a, b, c, d; + int len = 0; + + if ((fp = mg_fopen(MG_HOSTS_FILE_NAME, "r")) == NULL) { + return -1; + } + + for (; fgets(line, sizeof(line), fp) != NULL;) { + if (line[0] == '#') continue; + + if (sscanf(line, "%u.%u.%u.%u%n", &a, &b, &c, &d, &len) == 0) { + /* TODO(mkm): handle ipv6 */ + continue; + } + for (p = line + len; sscanf(p, "%s%n", alias, &len) == 1; p += len) { + if (strcmp(alias, name) == 0) { + usa->sin.sin_addr.s_addr = htonl(a << 24 | b << 16 | c << 8 | d); + fclose(fp); + return 0; + } + } + } + + fclose(fp); +#else + (void) name; + (void) usa; +#endif + + return -1; +} + +static void mg_resolve_async_eh(struct mg_connection *nc, int ev, + void *data MG_UD_ARG(void *user_data)) { + time_t now = (time_t) mg_time(); + struct mg_resolve_async_request *req; + struct mg_dns_message *msg; + int first = 0; +#if !MG_ENABLE_CALLBACK_USERDATA + void *user_data = nc->user_data; +#endif + + if (ev != MG_EV_POLL) DBG(("ev=%d user_data=%p", ev, user_data)); + + req = (struct mg_resolve_async_request *) user_data; + + if (req == NULL) { + return; + } + + switch (ev) { + case MG_EV_CONNECT: + /* don't depend on timer not being at epoch for sending out first req */ + first = 1; + /* fallthrough */ + case MG_EV_POLL: + if (req->retries > req->max_retries) { + req->err = MG_RESOLVE_EXCEEDED_RETRY_COUNT; + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + } + if (first || now - req->last_time >= req->timeout) { + mg_send_dns_query(nc, req->name, req->query); + req->last_time = now; + req->retries++; + } + break; + case MG_EV_RECV: + msg = (struct mg_dns_message *) MG_MALLOC(sizeof(*msg)); + if (mg_parse_dns(nc->recv_mbuf.buf, *(int *) data, msg) == 0 && + msg->num_answers > 0) { + req->callback(msg, req->data, MG_RESOLVE_OK); + nc->user_data = NULL; + MG_FREE(req); + } else { + req->err = MG_RESOLVE_NO_ANSWERS; + } + MG_FREE(msg); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + case MG_EV_SEND: + /* + * If a send error occurs, prevent closing of the connection by the core. + * We will retry after timeout. + */ + nc->flags &= ~MG_F_CLOSE_IMMEDIATELY; + mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len); + break; + case MG_EV_TIMER: + req->err = MG_RESOLVE_TIMEOUT; + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + case MG_EV_CLOSE: + /* If we got here with request still not done, fire an error callback. */ + if (req != NULL) { + char addr[32]; + mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr), MG_SOCK_STRINGIFY_IP); +#ifdef MG_LOG_DNS_FAILURES + LOG(LL_ERROR, ("Failed to resolve '%s', server %s", req->name, addr)); +#endif + req->callback(NULL, req->data, req->err); + nc->user_data = NULL; + MG_FREE(req); + } + break; + } +} + +int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query, + mg_resolve_callback_t cb, void *data) { + struct mg_resolve_async_opts opts; + memset(&opts, 0, sizeof(opts)); + return mg_resolve_async_opt(mgr, name, query, cb, data, opts); +} + +int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query, + mg_resolve_callback_t cb, void *data, + struct mg_resolve_async_opts opts) { + struct mg_resolve_async_request *req; + struct mg_connection *dns_nc; + const char *nameserver = opts.nameserver; + char dns_server_buff[17], nameserver_url[26]; + + if (nameserver == NULL) { + nameserver = mgr->nameserver; + } + + DBG(("%s %d %p", name, query, opts.dns_conn)); + + /* resolve with DNS */ + req = (struct mg_resolve_async_request *) MG_CALLOC(1, sizeof(*req)); + if (req == NULL) { + return -1; + } + + strncpy(req->name, name, sizeof(req->name)); + req->name[sizeof(req->name) - 1] = '\0'; + + req->query = query; + req->callback = cb; + req->data = data; + /* TODO(mkm): parse defaults out of resolve.conf */ + req->max_retries = opts.max_retries ? opts.max_retries : 2; + req->timeout = opts.timeout ? opts.timeout : 5; + + /* Lazily initialize dns server */ + if (nameserver == NULL) { + if (mg_get_ip_address_of_nameserver(dns_server_buff, + sizeof(dns_server_buff)) != -1) { + nameserver = dns_server_buff; + } else { + nameserver = MG_DEFAULT_NAMESERVER; + } + } + + snprintf(nameserver_url, sizeof(nameserver_url), "udp://%s:53", nameserver); + + dns_nc = mg_connect(mgr, nameserver_url, MG_CB(mg_resolve_async_eh, NULL)); + if (dns_nc == NULL) { + MG_FREE(req); + return -1; + } + dns_nc->user_data = req; + if (opts.dns_conn != NULL) { + *opts.dns_conn = dns_nc; + } + + return 0; +} + +void mg_set_nameserver(struct mg_mgr *mgr, const char *nameserver) { + MG_FREE((char *) mgr->nameserver); + mgr->nameserver = NULL; + if (nameserver != NULL) { + mgr->nameserver = strdup(nameserver); + } +} + +#endif /* MG_ENABLE_ASYNC_RESOLVER */ diff --git a/src/resolv.h b/src/resolv.h new file mode 100644 index 00000000..17635b2d --- /dev/null +++ b/src/resolv.h @@ -0,0 +1,81 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === API reference + */ + +#ifndef CS_MONGOOSE_SRC_RESOLV_H_ +#define CS_MONGOOSE_SRC_RESOLV_H_ + +#include "mongoose/src/dns.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +enum mg_resolve_err { + MG_RESOLVE_OK = 0, + MG_RESOLVE_NO_ANSWERS = 1, + MG_RESOLVE_EXCEEDED_RETRY_COUNT = 2, + MG_RESOLVE_TIMEOUT = 3 +}; + +typedef void (*mg_resolve_callback_t)(struct mg_dns_message *dns_message, + void *user_data, enum mg_resolve_err); + +/* Options for `mg_resolve_async_opt`. */ +struct mg_resolve_async_opts { + const char *nameserver; + int max_retries; /* defaults to 2 if zero */ + int timeout; /* in seconds; defaults to 5 if zero */ + int accept_literal; /* pseudo-resolve literal ipv4 and ipv6 addrs */ + int only_literal; /* only resolves literal addrs; sync cb invocation */ + struct mg_connection **dns_conn; /* return DNS connection */ +}; + +/* See `mg_resolve_async_opt()` */ +int mg_resolve_async(struct mg_mgr *mgr, const char *name, int query, + mg_resolve_callback_t cb, void *data); + +/* Set default DNS server */ +void mg_set_nameserver(struct mg_mgr *mgr, const char *nameserver); + +/* + * Resolved a DNS name asynchronously. + * + * Upon successful resolution, the user callback will be invoked + * with the full DNS response message and a pointer to the user's + * context `data`. + * + * In case of timeout while performing the resolution the callback + * will receive a NULL `msg`. + * + * The DNS answers can be extracted with `mg_next_record` and + * `mg_dns_parse_record_data`: + * + * [source,c] + * ---- + * struct in_addr ina; + * struct mg_dns_resource_record *rr = mg_next_record(msg, MG_DNS_A_RECORD, + * NULL); + * mg_dns_parse_record_data(msg, rr, &ina, sizeof(ina)); + * ---- + */ +int mg_resolve_async_opt(struct mg_mgr *mgr, const char *name, int query, + mg_resolve_callback_t cb, void *data, + struct mg_resolve_async_opts opts); + +/* + * Resolve a name from `/etc/hosts`. + * + * Returns 0 on success, -1 on failure. + */ +int mg_resolve_from_hosts_file(const char *host, union socket_address *usa); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CS_MONGOOSE_SRC_RESOLV_H_ */ diff --git a/src/sntp.c b/src/sntp.c new file mode 100644 index 00000000..4482540e --- /dev/null +++ b/src/sntp.c @@ -0,0 +1,288 @@ +/* + * Copyright (c) 2016 Cesanta Software Limited + * All rights reserved + */ + +#include "mongoose/src/internal.h" +#include "mongoose/src/sntp.h" +#include "mongoose/src/util.h" + +#if MG_ENABLE_SNTP + +#define SNTP_TIME_OFFSET 2208988800 + +#ifndef SNTP_TIMEOUT +#define SNTP_TIMEOUT 10 +#endif + +#ifndef SNTP_ATTEMPTS +#define SNTP_ATTEMPTS 3 +#endif + +static uint64_t mg_get_sec(uint64_t val) { + return (val & 0xFFFFFFFF00000000) >> 32; +} + +static uint64_t mg_get_usec(uint64_t val) { + uint64_t tmp = (val & 0x00000000FFFFFFFF); + tmp *= 1000000; + tmp >>= 32; + return tmp; +} + +static void mg_ntp_to_tv(uint64_t val, struct timeval *tv) { + uint64_t tmp; + tmp = mg_get_sec(val); + tmp -= SNTP_TIME_OFFSET; + tv->tv_sec = tmp; + tv->tv_usec = mg_get_usec(val); +} + +static void mg_get_ntp_ts(const char *ntp, uint64_t *val) { + uint32_t tmp; + memcpy(&tmp, ntp, sizeof(tmp)); + tmp = ntohl(tmp); + *val = (uint64_t) tmp << 32; + memcpy(&tmp, ntp + 4, sizeof(tmp)); + tmp = ntohl(tmp); + *val |= tmp; +} + +void mg_sntp_send_request(struct mg_connection *c) { + uint8_t buf[48] = {0}; + /* + * header - 8 bit: + * LI (2 bit) - 3 (not in sync), VN (3 bit) - 4 (version), + * mode (3 bit) - 3 (client) + */ + buf[0] = (3 << 6) | (4 << 3) | 3; + +/* + * Next fields should be empty in client request + * stratum, 8 bit + * poll interval, 8 bit + * rrecision, 8 bit + * root delay, 32 bit + * root dispersion, 32 bit + * ref id, 32 bit + * ref timestamp, 64 bit + * originate Timestamp, 64 bit + * receive Timestamp, 64 bit +*/ + +/* + * convert time to sntp format (sntp starts from 00:00:00 01.01.1900) + * according to rfc868 it is 2208988800L sec + * this information is used to correct roundtrip delay + * but if local clock is absolutely broken (and doesn't work even + * as simple timer), it is better to disable it +*/ +#ifndef MG_SNTP_NO_DELAY_CORRECTION + uint32_t sec; + sec = htonl((uint32_t)(mg_time() + SNTP_TIME_OFFSET)); + memcpy(&buf[40], &sec, sizeof(sec)); +#endif + + mg_send(c, buf, sizeof(buf)); +} + +#ifndef MG_SNTP_NO_DELAY_CORRECTION +static uint64_t mg_calculate_delay(uint64_t t1, uint64_t t2, uint64_t t3) { + /* roundloop delay = (T4 - T1) - (T3 - T2) */ + uint64_t d1 = ((mg_time() + SNTP_TIME_OFFSET) * 1000000) - + (mg_get_sec(t1) * 1000000 + mg_get_usec(t1)); + uint64_t d2 = (mg_get_sec(t3) * 1000000 + mg_get_usec(t3)) - + (mg_get_sec(t2) * 1000000 + mg_get_usec(t2)); + + return (d1 > d2) ? d1 - d2 : 0; +} +#endif + +MG_INTERNAL int mg_sntp_parse_reply(const char *buf, int len, + struct mg_sntp_message *msg) { + uint8_t hdr; + uint64_t trsm_ts_T3, delay = 0; + int mode; + struct timeval tv; + + if (len < 48) { + return -1; + } + + hdr = buf[0]; + + if ((hdr & 0x38) >> 3 != 4) { + /* Wrong version */ + return -1; + } + + mode = hdr & 0x7; + if (mode != 4 && mode != 5) { + /* Not a server reply */ + return -1; + } + + memset(msg, 0, sizeof(*msg)); + + msg->kiss_of_death = (buf[1] == 0); /* Server asks to not send requests */ + + mg_get_ntp_ts(&buf[40], &trsm_ts_T3); + +#ifndef MG_SNTP_NO_DELAY_CORRECTION + { + uint64_t orig_ts_T1, recv_ts_T2; + mg_get_ntp_ts(&buf[24], &orig_ts_T1); + mg_get_ntp_ts(&buf[32], &recv_ts_T2); + delay = mg_calculate_delay(orig_ts_T1, recv_ts_T2, trsm_ts_T3); + } +#endif + + mg_ntp_to_tv(trsm_ts_T3, &tv); + + msg->time = (double) tv.tv_sec + (((double) tv.tv_usec + delay) / 1000000.0); + + return 0; +} + +static void mg_sntp_handler(struct mg_connection *c, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { + struct mbuf *io = &c->recv_mbuf; + struct mg_sntp_message msg; + + c->handler(c, ev, ev_data MG_UD_ARG(user_data)); + + switch (ev) { + case MG_EV_RECV: { + if (mg_sntp_parse_reply(io->buf, io->len, &msg) < 0) { + DBG(("Invalid SNTP packet received (%d)", (int) io->len)); + c->handler(c, MG_SNTP_MALFORMED_REPLY, NULL MG_UD_ARG(user_data)); + } else { + c->handler(c, MG_SNTP_REPLY, (void *) &msg MG_UD_ARG(user_data)); + } + + mbuf_remove(io, io->len); + break; + } + } +} + +int mg_set_protocol_sntp(struct mg_connection *c) { + if ((c->flags & MG_F_UDP) == 0) { + return -1; + } + + c->proto_handler = mg_sntp_handler; + + return 0; +} + +struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, + MG_CB(mg_event_handler_t event_handler, + void *user_data), + const char *sntp_server_name) { + struct mg_connection *c = NULL; + char url[100], *p_url = url; + const char *proto = "", *port = "", *tmp; + + /* If port is not specified, use default (123) */ + tmp = strchr(sntp_server_name, ':'); + if (tmp != NULL && *(tmp + 1) == '/') { + tmp = strchr(tmp + 1, ':'); + } + + if (tmp == NULL) { + port = ":123"; + } + + /* Add udp:// if needed */ + if (strncmp(sntp_server_name, "udp://", 6) != 0) { + proto = "udp://"; + } + + mg_asprintf(&p_url, sizeof(url), "%s%s%s", proto, sntp_server_name, port); + + c = mg_connect(mgr, p_url, event_handler MG_UD_ARG(user_data)); + + if (c == NULL) { + goto cleanup; + } + + mg_set_protocol_sntp(c); + +cleanup: + if (p_url != url) { + MG_FREE(p_url); + } + + return c; +} + +struct sntp_data { + mg_event_handler_t hander; + int count; +}; + +static void mg_sntp_util_ev_handler(struct mg_connection *c, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { +#if !MG_ENABLE_CALLBACK_USERDATA + void *user_data = c->user_data; +#endif + struct sntp_data *sd = (struct sntp_data *) user_data; + + switch (ev) { + case MG_EV_CONNECT: + if (*(int *) ev_data != 0) { + mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL); + break; + } + /* fallthrough */ + case MG_EV_TIMER: + if (sd->count <= SNTP_ATTEMPTS) { + mg_sntp_send_request(c); + mg_set_timer(c, mg_time() + 10); + sd->count++; + } else { + mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL); + c->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + case MG_SNTP_MALFORMED_REPLY: + mg_call(c, sd->hander, c->user_data, MG_SNTP_FAILED, NULL); + c->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + case MG_SNTP_REPLY: + mg_call(c, sd->hander, c->user_data, MG_SNTP_REPLY, ev_data); + c->flags |= MG_F_CLOSE_IMMEDIATELY; + break; + case MG_EV_CLOSE: + MG_FREE(user_data); + c->user_data = NULL; + break; + } +} + +struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr, + mg_event_handler_t event_handler, + const char *sntp_server_name) { + struct mg_connection *c; + struct sntp_data *sd = (struct sntp_data *) MG_CALLOC(1, sizeof(*sd)); + if (sd == NULL) { + return NULL; + } + + c = mg_sntp_connect(mgr, MG_CB(mg_sntp_util_ev_handler, sd), + sntp_server_name); + if (c == NULL) { + MG_FREE(sd); + return NULL; + } + + sd->hander = event_handler; +#if !MG_ENABLE_CALLBACK_USERDATA + c->user_data = sd; +#endif + + return c; +} + +#endif /* MG_ENABLE_SNTP */ diff --git a/src/sntp.h b/src/sntp.h new file mode 100644 index 00000000..5cdff2ec --- /dev/null +++ b/src/sntp.h @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_SNTP_H_ +#define CS_MONGOOSE_SRC_SNTP_H_ + +#if MG_ENABLE_SNTP + +#define MG_SNTP_EVENT_BASE 500 + +/* + * Received reply from time server. Event handler parameter contains + * pointer to mg_sntp_message structure + */ +#define MG_SNTP_REPLY (MG_SNTP_EVENT_BASE + 1) + +/* Received malformed SNTP packet */ +#define MG_SNTP_MALFORMED_REPLY (MG_SNTP_EVENT_BASE + 2) + +/* Failed to get time from server (timeout etc) */ +#define MG_SNTP_FAILED (MG_SNTP_EVENT_BASE + 3) + +struct mg_sntp_message { + /* if server sends this flags, user should not send requests to it */ + int kiss_of_death; + /* usual mg_time */ + double time; +}; + +/* Establishes connection to given sntp server */ +struct mg_connection *mg_sntp_connect(struct mg_mgr *mgr, + MG_CB(mg_event_handler_t event_handler, + void *user_data), + const char *sntp_server_name); + +/* Sends time request to given connection */ +void mg_sntp_send_request(struct mg_connection *c); + +/* + * Helper function + * Establishes connection to time server, tries to send request + * repeats sending SNTP_ATTEMPTS times every SNTP_TIMEOUT sec + * (if needed) + * See sntp_client example + */ +struct mg_connection *mg_sntp_get_time(struct mg_mgr *mgr, + mg_event_handler_t event_handler, + const char *sntp_server_name); + +#endif + +#endif /* CS_MONGOOSE_SRC_SNTP_H_ */ diff --git a/src/socks.c b/src/socks.c new file mode 100644 index 00000000..bb4b44c8 --- /dev/null +++ b/src/socks.c @@ -0,0 +1,159 @@ +/* + * Copyright (c) 2017 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_SOCKS + +#include "mongoose/src/socks.h" +#include "mongoose/src/internal.h" + +/* + * https://www.ietf.org/rfc/rfc1928.txt paragraph 3, handle client handshake + * + * +----+----------+----------+ + * |VER | NMETHODS | METHODS | + * +----+----------+----------+ + * | 1 | 1 | 1 to 255 | + * +----+----------+----------+ + */ +static void mg_socks5_handshake(struct mg_connection *c) { + struct mbuf *r = &c->recv_mbuf; + if (r->buf[0] != MG_SOCKS_VERSION) { + c->flags |= MG_F_CLOSE_IMMEDIATELY; + } else if (r->len > 2 && (size_t) r->buf[1] + 2 <= r->len) { + /* https://www.ietf.org/rfc/rfc1928.txt paragraph 3 */ + unsigned char reply[2] = {MG_SOCKS_VERSION, MG_SOCKS_HANDSHAKE_FAILURE}; + int i; + for (i = 2; i < r->buf[1] + 2; i++) { + /* TODO(lsm): support other auth methods */ + if (r->buf[i] == MG_SOCKS_HANDSHAKE_NOAUTH) reply[1] = r->buf[i]; + } + mbuf_remove(r, 2 + r->buf[1]); + mg_send(c, reply, sizeof(reply)); + c->flags |= MG_SOCKS_HANDSHAKE_DONE; /* Mark handshake done */ + } +} + +static void disband(struct mg_connection *c) { + struct mg_connection *c2 = (struct mg_connection *) c->user_data; + if (c2 != NULL) { + c2->flags |= MG_F_SEND_AND_CLOSE; + c2->user_data = NULL; + } + c->flags |= MG_F_SEND_AND_CLOSE; + c->user_data = NULL; +} + +static void relay_data(struct mg_connection *c) { + struct mg_connection *c2 = (struct mg_connection *) c->user_data; + if (c2 != NULL) { + mg_send(c2, c->recv_mbuf.buf, c->recv_mbuf.len); + mbuf_remove(&c->recv_mbuf, c->recv_mbuf.len); + } else { + c->flags |= MG_F_SEND_AND_CLOSE; + } +} + +static void serv_ev_handler(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_CLOSE) { + disband(c); + } else if (ev == MG_EV_RECV) { + relay_data(c); + } else if (ev == MG_EV_CONNECT) { + int res = *(int *) ev_data; + if (res != 0) LOG(LL_ERROR, ("connect error: %d", res)); + } +} + +static void mg_socks5_connect(struct mg_connection *c, const char *addr) { + struct mg_connection *serv = mg_connect(c->mgr, addr, serv_ev_handler); + serv->user_data = c; + c->user_data = serv; +} + +/* + * Request, https://www.ietf.org/rfc/rfc1928.txt paragraph 4 + * + * +----+-----+-------+------+----------+----------+ + * |VER | CMD | RSV | ATYP | DST.ADDR | DST.PORT | + * +----+-----+-------+------+----------+----------+ + * | 1 | 1 | X'00' | 1 | Variable | 2 | + * +----+-----+-------+------+----------+----------+ + */ +static void mg_socks5_handle_request(struct mg_connection *c) { + struct mbuf *r = &c->recv_mbuf; + unsigned char *p = (unsigned char *) r->buf; + unsigned char addr_len = 4, reply = MG_SOCKS_SUCCESS; + int ver, cmd, atyp; + char addr[300]; + + if (r->len < 8) return; /* return if not fully buffered. min DST.ADDR is 2 */ + ver = p[0]; + cmd = p[1]; + atyp = p[3]; + + /* TODO(lsm): support other commands */ + if (ver != MG_SOCKS_VERSION || cmd != MG_SOCKS_CMD_CONNECT) { + reply = MG_SOCKS_CMD_NOT_SUPPORTED; + } else if (atyp == MG_SOCKS_ADDR_IPV4) { + addr_len = 4; + if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */ + snprintf(addr, sizeof(addr), "%d.%d.%d.%d:%d", p[4], p[5], p[6], p[7], + p[8] << 8 | p[9]); + mg_socks5_connect(c, addr); + } else if (atyp == MG_SOCKS_ADDR_IPV6) { + addr_len = 16; + if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */ + snprintf(addr, sizeof(addr), "[%x:%x:%x:%x:%x:%x:%x:%x]:%d", + p[4] << 8 | p[5], p[6] << 8 | p[7], p[8] << 8 | p[9], + p[10] << 8 | p[11], p[12] << 8 | p[13], p[14] << 8 | p[15], + p[16] << 8 | p[17], p[18] << 8 | p[19], p[20] << 8 | p[21]); + mg_socks5_connect(c, addr); + } else if (atyp == MG_SOCKS_ADDR_DOMAIN) { + addr_len = p[4] + 1; + if (r->len < (size_t) addr_len + 6) return; /* return if not buffered */ + snprintf(addr, sizeof(addr), "%.*s:%d", p[4], p + 5, + p[4 + addr_len] << 8 | p[4 + addr_len + 1]); + mg_socks5_connect(c, addr); + } else { + reply = MG_SOCKS_ADDR_NOT_SUPPORTED; + } + + /* + * Reply, https://www.ietf.org/rfc/rfc1928.txt paragraph 5 + * + * +----+-----+-------+------+----------+----------+ + * |VER | REP | RSV | ATYP | BND.ADDR | BND.PORT | + * +----+-----+-------+------+----------+----------+ + * | 1 | 1 | X'00' | 1 | Variable | 2 | + * +----+-----+-------+------+----------+----------+ + */ + { + unsigned char buf[] = {MG_SOCKS_VERSION, reply, 0}; + mg_send(c, buf, sizeof(buf)); + } + mg_send(c, r->buf + 3, addr_len + 1 + 2); + + mbuf_remove(r, 6 + addr_len); /* Remove request from the input stream */ + c->flags |= MG_SOCKS_CONNECT_DONE; /* Mark ourselves as connected */ +} + +static void socks_handler(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_RECV) { + if (!(c->flags & MG_SOCKS_HANDSHAKE_DONE)) mg_socks5_handshake(c); + if (c->flags & MG_SOCKS_HANDSHAKE_DONE && + !(c->flags & MG_SOCKS_CONNECT_DONE)) { + mg_socks5_handle_request(c); + } + if (c->flags & MG_SOCKS_CONNECT_DONE) relay_data(c); + } else if (ev == MG_EV_CLOSE) { + disband(c); + } + (void) ev_data; +} + +void mg_set_protocol_socks(struct mg_connection *c) { + c->proto_handler = socks_handler; +} +#endif diff --git a/src/socks.h b/src/socks.h new file mode 100644 index 00000000..e7b231a0 --- /dev/null +++ b/src/socks.h @@ -0,0 +1,66 @@ +/* + * Copyright (c) 2017 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_SOCKS_H_ +#define CS_MONGOOSE_SRC_SOCKS_H_ + +#if MG_ENABLE_SOCKS + +#define MG_SOCKS_VERSION 5 + +#define MG_SOCKS_HANDSHAKE_DONE MG_F_USER_1 +#define MG_SOCKS_CONNECT_DONE MG_F_USER_2 + +/* SOCKS5 handshake methods */ +enum mg_socks_handshake_method { + MG_SOCKS_HANDSHAKE_NOAUTH = 0, /* Handshake method - no authentication */ + MG_SOCKS_HANDSHAKE_GSSAPI = 1, /* Handshake method - GSSAPI auth */ + MG_SOCKS_HANDSHAKE_USERPASS = 2, /* Handshake method - user/password auth */ + MG_SOCKS_HANDSHAKE_FAILURE = 0xff, /* Handshake method - failure */ +}; + +/* SOCKS5 commands */ +enum mg_socks_command { + MG_SOCKS_CMD_CONNECT = 1, /* Command: CONNECT */ + MG_SOCKS_CMD_BIND = 2, /* Command: BIND */ + MG_SOCKS_CMD_UDP_ASSOCIATE = 3, /* Command: UDP ASSOCIATE */ +}; + +/* SOCKS5 address types */ +enum mg_socks_address_type { + MG_SOCKS_ADDR_IPV4 = 1, /* Address type: IPv4 */ + MG_SOCKS_ADDR_DOMAIN = 3, /* Address type: Domain name */ + MG_SOCKS_ADDR_IPV6 = 4, /* Address type: IPv6 */ +}; + +/* SOCKS5 response codes */ +enum mg_socks_response { + MG_SOCKS_SUCCESS = 0, /* Response: success */ + MG_SOCKS_FAILURE = 1, /* Response: failure */ + MG_SOCKS_NOT_ALLOWED = 2, /* Response: connection not allowed */ + MG_SOCKS_NET_UNREACHABLE = 3, /* Response: network unreachable */ + MG_SOCKS_HOST_UNREACHABLE = 4, /* Response: network unreachable */ + MG_SOCKS_CONN_REFUSED = 5, /* Response: network unreachable */ + MG_SOCKS_TTL_EXPIRED = 6, /* Response: network unreachable */ + MG_SOCKS_CMD_NOT_SUPPORTED = 7, /* Response: network unreachable */ + MG_SOCKS_ADDR_NOT_SUPPORTED = 8, /* Response: network unreachable */ +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* Turn the connection into the SOCKS server */ +void mg_set_protocol_socks(struct mg_connection *c); + +/* Create socks tunnel for the client connection */ +struct mg_iface *mg_socks_mk_iface(struct mg_mgr *, const char *proxy_addr); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif +#endif diff --git a/src/ssl_if.h b/src/ssl_if.h new file mode 100644 index 00000000..62f24b51 --- /dev/null +++ b/src/ssl_if.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_SSL_IF_H_ +#define CS_MONGOOSE_SRC_SSL_IF_H_ + +#if MG_ENABLE_SSL + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct mg_ssl_if_ctx; +struct mg_connection; + +void mg_ssl_if_init(); + +enum mg_ssl_if_result { + MG_SSL_OK = 0, + MG_SSL_WANT_READ = -1, + MG_SSL_WANT_WRITE = -2, + MG_SSL_ERROR = -3, +}; + +struct mg_ssl_if_conn_params { + const char *cert; + const char *key; + const char *ca_cert; + const char *server_name; + const char *cipher_suites; + const char *psk_identity; + const char *psk_key; +}; + +enum mg_ssl_if_result mg_ssl_if_conn_init( + struct mg_connection *nc, const struct mg_ssl_if_conn_params *params, + const char **err_msg); +enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc, + struct mg_connection *lc); +void mg_ssl_if_conn_close_notify(struct mg_connection *nc); +void mg_ssl_if_conn_free(struct mg_connection *nc); + +enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc); +int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size); +int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_SSL */ + +#endif /* CS_MONGOOSE_SRC_SSL_IF_H_ */ diff --git a/src/ssl_if_mbedtls.c b/src/ssl_if_mbedtls.c new file mode 100644 index 00000000..4c1c1db1 --- /dev/null +++ b/src/ssl_if_mbedtls.c @@ -0,0 +1,478 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS + +#include +#include +#include +#include +#include + +static void mg_ssl_mbed_log(void *ctx, int level, const char *file, int line, + const char *str) { + enum cs_log_level cs_level; + switch (level) { + case 1: + cs_level = LL_ERROR; + break; + case 2: + case 3: + cs_level = LL_DEBUG; + break; + default: + cs_level = LL_VERBOSE_DEBUG; + } + /* mbedTLS passes strings with \n at the end, strip it. */ + LOG(cs_level, ("%p %.*s", ctx, (int) (strlen(str) - 1), str)); + (void) file; + (void) line; +} + +struct mg_ssl_if_ctx { + mbedtls_ssl_config *conf; + mbedtls_ssl_context *ssl; + mbedtls_x509_crt *cert; + mbedtls_pk_context *key; + mbedtls_x509_crt *ca_cert; + struct mbuf cipher_suites; +}; + +/* Must be provided by the platform. ctx is struct mg_connection. */ +extern int mg_ssl_if_mbed_random(void *ctx, unsigned char *buf, size_t len); + +void mg_ssl_if_init() { +} + +enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc, + struct mg_connection *lc) { + struct mg_ssl_if_ctx *ctx = + (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx)); + struct mg_ssl_if_ctx *lc_ctx = (struct mg_ssl_if_ctx *) lc->ssl_if_data; + nc->ssl_if_data = ctx; + if (ctx == NULL || lc_ctx == NULL) return MG_SSL_ERROR; + ctx->ssl = (mbedtls_ssl_context *) MG_CALLOC(1, sizeof(*ctx->ssl)); + if (mbedtls_ssl_setup(ctx->ssl, lc_ctx->conf) != 0) { + return MG_SSL_ERROR; + } + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_use_cert(struct mg_ssl_if_ctx *ctx, + const char *cert, const char *key, + const char **err_msg); +static enum mg_ssl_if_result mg_use_ca_cert(struct mg_ssl_if_ctx *ctx, + const char *cert); +static enum mg_ssl_if_result mg_set_cipher_list(struct mg_ssl_if_ctx *ctx, + const char *ciphers); +static enum mg_ssl_if_result mg_ssl_if_mbed_set_psk(struct mg_ssl_if_ctx *ctx, + const char *identity, + const char *key); + +enum mg_ssl_if_result mg_ssl_if_conn_init( + struct mg_connection *nc, const struct mg_ssl_if_conn_params *params, + const char **err_msg) { + struct mg_ssl_if_ctx *ctx = + (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx)); + DBG(("%p %s,%s,%s", nc, (params->cert ? params->cert : ""), + (params->key ? params->key : ""), + (params->ca_cert ? params->ca_cert : ""))); + + if (ctx == NULL) { + MG_SET_PTRPTR(err_msg, "Out of memory"); + return MG_SSL_ERROR; + } + nc->ssl_if_data = ctx; + ctx->conf = (mbedtls_ssl_config *) MG_CALLOC(1, sizeof(*ctx->conf)); + mbuf_init(&ctx->cipher_suites, 0); + mbedtls_ssl_config_init(ctx->conf); + mbedtls_ssl_conf_dbg(ctx->conf, mg_ssl_mbed_log, nc); + if (mbedtls_ssl_config_defaults( + ctx->conf, (nc->flags & MG_F_LISTENING ? MBEDTLS_SSL_IS_SERVER + : MBEDTLS_SSL_IS_CLIENT), + MBEDTLS_SSL_TRANSPORT_STREAM, MBEDTLS_SSL_PRESET_DEFAULT) != 0) { + MG_SET_PTRPTR(err_msg, "Failed to init SSL config"); + return MG_SSL_ERROR; + } + + /* TLS 1.2 and up */ + mbedtls_ssl_conf_min_version(ctx->conf, MBEDTLS_SSL_MAJOR_VERSION_3, + MBEDTLS_SSL_MINOR_VERSION_3); + mbedtls_ssl_conf_rng(ctx->conf, mg_ssl_if_mbed_random, nc); + + if (params->cert != NULL && + mg_use_cert(ctx, params->cert, params->key, err_msg) != MG_SSL_OK) { + return MG_SSL_ERROR; + } + + if (params->ca_cert != NULL && + mg_use_ca_cert(ctx, params->ca_cert) != MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid SSL CA cert"); + return MG_SSL_ERROR; + } + + if (mg_set_cipher_list(ctx, params->cipher_suites) != MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid cipher suite list"); + return MG_SSL_ERROR; + } + + if (mg_ssl_if_mbed_set_psk(ctx, params->psk_identity, params->psk_key) != + MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid PSK settings"); + return MG_SSL_ERROR; + } + + if (!(nc->flags & MG_F_LISTENING)) { + ctx->ssl = (mbedtls_ssl_context *) MG_CALLOC(1, sizeof(*ctx->ssl)); + mbedtls_ssl_init(ctx->ssl); + if (mbedtls_ssl_setup(ctx->ssl, ctx->conf) != 0) { + MG_SET_PTRPTR(err_msg, "Failed to create SSL session"); + return MG_SSL_ERROR; + } + if (params->server_name != NULL && + mbedtls_ssl_set_hostname(ctx->ssl, params->server_name) != 0) { + return MG_SSL_ERROR; + } + } + +#ifdef MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN + if (mbedtls_ssl_conf_max_frag_len(ctx->conf, +#if MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 512 + MBEDTLS_SSL_MAX_FRAG_LEN_512 +#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 1024 + MBEDTLS_SSL_MAX_FRAG_LEN_1024 +#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 2048 + MBEDTLS_SSL_MAX_FRAG_LEN_2048 +#elif MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN == 4096 + MBEDTLS_SSL_MAX_FRAG_LEN_4096 +#else +#error Invalid MG_SSL_IF_MBEDTLS_MAX_FRAG_LEN +#endif + ) != 0) { + return MG_SSL_ERROR; + } +#endif + + nc->flags |= MG_F_SSL; + + return MG_SSL_OK; +} + +#if MG_NET_IF == MG_NET_IF_LWIP_LOW_LEVEL +int ssl_socket_send(void *ctx, const unsigned char *buf, size_t len); +int ssl_socket_recv(void *ctx, unsigned char *buf, size_t len); +#else +static int ssl_socket_send(void *ctx, const unsigned char *buf, size_t len) { + struct mg_connection *nc = (struct mg_connection *) ctx; + int n = (int) MG_SEND_FUNC(nc->sock, buf, len, 0); + LOG(LL_DEBUG, ("%p %d -> %d", nc, (int) len, n)); + if (n >= 0) return n; + n = mg_get_errno(); + return ((n == EAGAIN || n == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_WRITE : -1); +} + +static int ssl_socket_recv(void *ctx, unsigned char *buf, size_t len) { + struct mg_connection *nc = (struct mg_connection *) ctx; + int n = (int) MG_RECV_FUNC(nc->sock, buf, len, 0); + LOG(LL_DEBUG, ("%p %d <- %d", nc, (int) len, n)); + if (n >= 0) return n; + n = mg_get_errno(); + return ((n == EAGAIN || n == EINPROGRESS) ? MBEDTLS_ERR_SSL_WANT_READ : -1); +} +#endif + +static enum mg_ssl_if_result mg_ssl_if_mbed_err(struct mg_connection *nc, + int ret) { + if (ret == MBEDTLS_ERR_SSL_WANT_READ) return MG_SSL_WANT_READ; + if (ret == MBEDTLS_ERR_SSL_WANT_WRITE) return MG_SSL_WANT_WRITE; + if (ret != + MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY) { /* CLOSE_NOTIFY = Normal shutdown */ + LOG(LL_ERROR, ("%p SSL error: %d", nc, ret)); + } + nc->err = ret; + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return MG_SSL_ERROR; +} + +static void mg_ssl_if_mbed_free_certs_and_keys(struct mg_ssl_if_ctx *ctx) { + if (ctx->cert != NULL) { + mbedtls_x509_crt_free(ctx->cert); + MG_FREE(ctx->cert); + ctx->cert = NULL; + mbedtls_pk_free(ctx->key); + MG_FREE(ctx->key); + ctx->key = NULL; + } + if (ctx->ca_cert != NULL) { + mbedtls_ssl_conf_ca_chain(ctx->conf, NULL, NULL); +#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK + if (ctx->ca_cert->ca_chain_file != NULL) { + MG_FREE((void *) ctx->ca_cert->ca_chain_file); + ctx->ca_cert->ca_chain_file = NULL; + } +#endif + mbedtls_x509_crt_free(ctx->ca_cert); + MG_FREE(ctx->ca_cert); + ctx->ca_cert = NULL; + } +} + +enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int err; + /* If bio is not yet set, do it now. */ + if (ctx->ssl->p_bio == NULL) { + mbedtls_ssl_set_bio(ctx->ssl, nc, ssl_socket_send, ssl_socket_recv, NULL); + } + err = mbedtls_ssl_handshake(ctx->ssl); + if (err != 0) return mg_ssl_if_mbed_err(nc, err); +#ifdef MG_SSL_IF_MBEDTLS_FREE_CERTS + /* + * Free the peer certificate, we don't need it after handshake. + * Note that this effectively disables renegotiation. + */ + mbedtls_x509_crt_free(ctx->ssl->session->peer_cert); + mbedtls_free(ctx->ssl->session->peer_cert); + ctx->ssl->session->peer_cert = NULL; + /* On a client connection we can also free our own and CA certs. */ + if (nc->listener == NULL) { + if (ctx->conf->key_cert != NULL) { + /* Note that this assumes one key_cert entry, which matches our init. */ + MG_FREE(ctx->conf->key_cert); + ctx->conf->key_cert = NULL; + } + mbedtls_ssl_conf_ca_chain(ctx->conf, NULL, NULL); + mg_ssl_if_mbed_free_certs_and_keys(ctx); + } +#endif + return MG_SSL_OK; +} + +int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int n = mbedtls_ssl_read(ctx->ssl, (unsigned char *) buf, buf_size); + DBG(("%p %d -> %d", nc, (int) buf_size, n)); + if (n < 0) return mg_ssl_if_mbed_err(nc, n); + if (n == 0) nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return n; +} + +int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int n = mbedtls_ssl_write(ctx->ssl, (const unsigned char *) data, len); + DBG(("%p %d -> %d", nc, (int) len, n)); + if (n < 0) return mg_ssl_if_mbed_err(nc, n); + return n; +} + +void mg_ssl_if_conn_close_notify(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + if (ctx == NULL) return; + mbedtls_ssl_close_notify(ctx->ssl); +} + +void mg_ssl_if_conn_free(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + if (ctx == NULL) return; + nc->ssl_if_data = NULL; + if (ctx->ssl != NULL) { + mbedtls_ssl_free(ctx->ssl); + MG_FREE(ctx->ssl); + } + mg_ssl_if_mbed_free_certs_and_keys(ctx); + if (ctx->conf != NULL) { + mbedtls_ssl_config_free(ctx->conf); + MG_FREE(ctx->conf); + } + mbuf_free(&ctx->cipher_suites); + memset(ctx, 0, sizeof(*ctx)); + MG_FREE(ctx); +} + +static enum mg_ssl_if_result mg_use_ca_cert(struct mg_ssl_if_ctx *ctx, + const char *ca_cert) { + if (ca_cert == NULL || strcmp(ca_cert, "*") == 0) { + mbedtls_ssl_conf_authmode(ctx->conf, MBEDTLS_SSL_VERIFY_NONE); + return MG_SSL_OK; + } + ctx->ca_cert = (mbedtls_x509_crt *) MG_CALLOC(1, sizeof(*ctx->ca_cert)); + mbedtls_x509_crt_init(ctx->ca_cert); +#ifdef MBEDTLS_X509_CA_CHAIN_ON_DISK + ca_cert = strdup(ca_cert); + if (mbedtls_x509_crt_set_ca_chain_file(ctx->ca_cert, ca_cert) != 0) { + return MG_SSL_ERROR; + } +#else + if (mbedtls_x509_crt_parse_file(ctx->ca_cert, ca_cert) != 0) { + return MG_SSL_ERROR; + } +#endif + mbedtls_ssl_conf_ca_chain(ctx->conf, ctx->ca_cert, NULL); + mbedtls_ssl_conf_authmode(ctx->conf, MBEDTLS_SSL_VERIFY_REQUIRED); + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_use_cert(struct mg_ssl_if_ctx *ctx, + const char *cert, const char *key, + const char **err_msg) { + if (key == NULL) key = cert; + if (cert == NULL || cert[0] == '\0' || key == NULL || key[0] == '\0') { + return MG_SSL_OK; + } + ctx->cert = (mbedtls_x509_crt *) MG_CALLOC(1, sizeof(*ctx->cert)); + mbedtls_x509_crt_init(ctx->cert); + ctx->key = (mbedtls_pk_context *) MG_CALLOC(1, sizeof(*ctx->key)); + mbedtls_pk_init(ctx->key); + if (mbedtls_x509_crt_parse_file(ctx->cert, cert) != 0) { + MG_SET_PTRPTR(err_msg, "Invalid SSL cert"); + return MG_SSL_ERROR; + } + if (mbedtls_pk_parse_keyfile(ctx->key, key, NULL) != 0) { + MG_SET_PTRPTR(err_msg, "Invalid SSL key"); + return MG_SSL_ERROR; + } + if (mbedtls_ssl_conf_own_cert(ctx->conf, ctx->cert, ctx->key) != 0) { + MG_SET_PTRPTR(err_msg, "Invalid SSL key or cert"); + return MG_SSL_ERROR; + } + return MG_SSL_OK; +} + +static const int mg_s_cipher_list[] = { +#if CS_PLATFORM != CS_P_ESP8266 + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_DHE_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_DHE_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, + MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, +#else + /* + * ECDHE is way too slow on ESP8266 w/o cryptochip, this sometimes results + * in WiFi STA deauths. Use weaker but faster cipher suites. Sad but true. + * Disable DHE completely because it's just hopelessly slow. + */ + MBEDTLS_TLS_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDHE_ECDSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_ECDSA_WITH_AES_128_CBC_SHA, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_GCM_SHA256, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA256, + MBEDTLS_TLS_ECDH_RSA_WITH_AES_128_CBC_SHA, + MBEDTLS_TLS_RSA_WITH_AES_128_CBC_SHA, +#endif /* CS_PLATFORM != CS_P_ESP8266 */ + 0, +}; + +/* + * Ciphers can be specified as a colon-separated list of cipher suite names. + * These can be found in + * https://github.com/ARMmbed/mbedtls/blob/development/library/ssl_ciphersuites.c#L267 + * E.g.: TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CCM + */ +static enum mg_ssl_if_result mg_set_cipher_list(struct mg_ssl_if_ctx *ctx, + const char *ciphers) { + if (ciphers != NULL) { + int l, id; + const char *s = ciphers, *e; + char tmp[50]; + while (s != NULL) { + e = strchr(s, ':'); + l = (e != NULL ? (e - s) : (int) strlen(s)); + strncpy(tmp, s, l); + tmp[l] = '\0'; + id = mbedtls_ssl_get_ciphersuite_id(tmp); + DBG(("%s -> %04x", tmp, id)); + if (id != 0) { + mbuf_append(&ctx->cipher_suites, &id, sizeof(id)); + } + s = (e != NULL ? e + 1 : NULL); + } + if (ctx->cipher_suites.len == 0) return MG_SSL_ERROR; + id = 0; + mbuf_append(&ctx->cipher_suites, &id, sizeof(id)); + mbuf_trim(&ctx->cipher_suites); + mbedtls_ssl_conf_ciphersuites(ctx->conf, + (const int *) ctx->cipher_suites.buf); + } else { + mbedtls_ssl_conf_ciphersuites(ctx->conf, mg_s_cipher_list); + } + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_ssl_if_mbed_set_psk(struct mg_ssl_if_ctx *ctx, + const char *identity, + const char *key_str) { + unsigned char key[32]; + size_t key_len; + if (identity == NULL && key_str == NULL) return MG_SSL_OK; + if (identity == NULL || key_str == NULL) return MG_SSL_ERROR; + key_len = strlen(key_str); + if (key_len != 32 && key_len != 64) return MG_SSL_ERROR; + size_t i = 0; + memset(key, 0, sizeof(key)); + key_len = 0; + for (i = 0; key_str[i] != '\0'; i++) { + unsigned char c; + char hc = tolower((int) key_str[i]); + if (hc >= '0' && hc <= '9') { + c = hc - '0'; + } else if (hc >= 'a' && hc <= 'f') { + c = hc - 'a' + 0xa; + } else { + return MG_SSL_ERROR; + } + key_len = i / 2; + key[key_len] <<= 4; + key[key_len] |= c; + } + key_len++; + DBG(("identity = '%s', key = (%u)", identity, (unsigned int) key_len)); + /* mbedTLS makes copies of psk and identity. */ + if (mbedtls_ssl_conf_psk(ctx->conf, (const unsigned char *) key, key_len, + (const unsigned char *) identity, + strlen(identity)) != 0) { + return MG_SSL_ERROR; + } + return MG_SSL_OK; +} + +const char *mg_set_ssl(struct mg_connection *nc, const char *cert, + const char *ca_cert) { + const char *err_msg = NULL; + struct mg_ssl_if_conn_params params; + memset(¶ms, 0, sizeof(params)); + params.cert = cert; + params.ca_cert = ca_cert; + if (mg_ssl_if_conn_init(nc, ¶ms, &err_msg) != MG_SSL_OK) { + return err_msg; + } + return NULL; +} + +/* Lazy RNG. Warning: it would be a bad idea to do this in production! */ +#ifdef MG_SSL_MBED_DUMMY_RANDOM +int mg_ssl_if_mbed_random(void *ctx, unsigned char *buf, size_t len) { + (void) ctx; + while (len--) *buf++ = rand(); + return 0; +} +#endif + +#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_MBEDTLS */ diff --git a/src/ssl_if_openssl.c b/src/ssl_if_openssl.c new file mode 100644 index 00000000..2f3da5ea --- /dev/null +++ b/src/ssl_if_openssl.c @@ -0,0 +1,394 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL + +#ifdef __APPLE__ +#pragma GCC diagnostic ignored "-Wdeprecated-declarations" +#endif + +#include + +struct mg_ssl_if_ctx { + SSL *ssl; + SSL_CTX *ssl_ctx; + struct mbuf psk; + size_t identity_len; +}; + +void mg_ssl_if_init() { + SSL_library_init(); +} + +enum mg_ssl_if_result mg_ssl_if_conn_accept(struct mg_connection *nc, + struct mg_connection *lc) { + struct mg_ssl_if_ctx *ctx = + (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx)); + struct mg_ssl_if_ctx *lc_ctx = (struct mg_ssl_if_ctx *) lc->ssl_if_data; + nc->ssl_if_data = ctx; + if (ctx == NULL || lc_ctx == NULL) return MG_SSL_ERROR; + ctx->ssl_ctx = lc_ctx->ssl_ctx; + if ((ctx->ssl = SSL_new(ctx->ssl_ctx)) == NULL) { + return MG_SSL_ERROR; + } + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_use_cert(SSL_CTX *ctx, const char *cert, + const char *key, const char **err_msg); +static enum mg_ssl_if_result mg_use_ca_cert(SSL_CTX *ctx, const char *cert); +static enum mg_ssl_if_result mg_set_cipher_list(SSL_CTX *ctx, const char *cl); +static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx, + const char *identity, + const char *key_str); + +enum mg_ssl_if_result mg_ssl_if_conn_init( + struct mg_connection *nc, const struct mg_ssl_if_conn_params *params, + const char **err_msg) { + struct mg_ssl_if_ctx *ctx = + (struct mg_ssl_if_ctx *) MG_CALLOC(1, sizeof(*ctx)); + DBG(("%p %s,%s,%s", nc, (params->cert ? params->cert : ""), + (params->key ? params->key : ""), + (params->ca_cert ? params->ca_cert : ""))); + if (ctx == NULL) { + MG_SET_PTRPTR(err_msg, "Out of memory"); + return MG_SSL_ERROR; + } + nc->ssl_if_data = ctx; + if (nc->flags & MG_F_LISTENING) { + ctx->ssl_ctx = SSL_CTX_new(SSLv23_server_method()); + } else { + ctx->ssl_ctx = SSL_CTX_new(SSLv23_client_method()); + } + if (ctx->ssl_ctx == NULL) { + MG_SET_PTRPTR(err_msg, "Failed to create SSL context"); + return MG_SSL_ERROR; + } + +#ifndef KR_VERSION + /* Disable deprecated protocols. */ + SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv2); + SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_SSLv3); + SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_TLSv1); +#ifdef MG_SSL_OPENSSL_NO_COMPRESSION + SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_NO_COMPRESSION); +#endif +#ifdef MG_SSL_OPENSSL_CIPHER_SERVER_PREFERENCE + SSL_CTX_set_options(ctx->ssl_ctx, SSL_OP_CIPHER_SERVER_PREFERENCE); +#endif +#else +/* Krypton only supports TLSv1.2 anyway. */ +#endif + + if (params->cert != NULL && + mg_use_cert(ctx->ssl_ctx, params->cert, params->key, err_msg) != + MG_SSL_OK) { + return MG_SSL_ERROR; + } + + if (params->ca_cert != NULL && + mg_use_ca_cert(ctx->ssl_ctx, params->ca_cert) != MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid SSL CA cert"); + return MG_SSL_ERROR; + } + + if (params->server_name != NULL) { +#ifdef KR_VERSION + SSL_CTX_kr_set_verify_name(ctx->ssl_ctx, params->server_name); +#else +/* TODO(rojer): Implement server name verification on OpenSSL. */ +#endif + } + + if (mg_set_cipher_list(ctx->ssl_ctx, params->cipher_suites) != MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid cipher suite list"); + return MG_SSL_ERROR; + } + + mbuf_init(&ctx->psk, 0); + if (mg_ssl_if_ossl_set_psk(ctx, params->psk_identity, params->psk_key) != + MG_SSL_OK) { + MG_SET_PTRPTR(err_msg, "Invalid PSK settings"); + return MG_SSL_ERROR; + } + + if (!(nc->flags & MG_F_LISTENING) && + (ctx->ssl = SSL_new(ctx->ssl_ctx)) == NULL) { + MG_SET_PTRPTR(err_msg, "Failed to create SSL session"); + return MG_SSL_ERROR; + } + + nc->flags |= MG_F_SSL; + + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_ssl_if_ssl_err(struct mg_connection *nc, + int res) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int err = SSL_get_error(ctx->ssl, res); + if (err == SSL_ERROR_WANT_READ) return MG_SSL_WANT_READ; + if (err == SSL_ERROR_WANT_WRITE) return MG_SSL_WANT_WRITE; + DBG(("%p %p SSL error: %d %d", nc, ctx->ssl_ctx, res, err)); + nc->err = err; + return MG_SSL_ERROR; +} + +enum mg_ssl_if_result mg_ssl_if_handshake(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int server_side = (nc->listener != NULL); + int res; + /* If descriptor is not yet set, do it now. */ + if (SSL_get_fd(ctx->ssl) < 0) { + if (SSL_set_fd(ctx->ssl, nc->sock) != 1) return MG_SSL_ERROR; + } + res = server_side ? SSL_accept(ctx->ssl) : SSL_connect(ctx->ssl); + if (res != 1) return mg_ssl_if_ssl_err(nc, res); + return MG_SSL_OK; +} + +int mg_ssl_if_read(struct mg_connection *nc, void *buf, size_t buf_size) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int n = SSL_read(ctx->ssl, buf, buf_size); + DBG(("%p %d -> %d", nc, (int) buf_size, n)); + if (n < 0) return mg_ssl_if_ssl_err(nc, n); + if (n == 0) nc->flags |= MG_F_CLOSE_IMMEDIATELY; + return n; +} + +int mg_ssl_if_write(struct mg_connection *nc, const void *data, size_t len) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + int n = SSL_write(ctx->ssl, data, len); + DBG(("%p %d -> %d", nc, (int) len, n)); + if (n <= 0) return mg_ssl_if_ssl_err(nc, n); + return n; +} + +void mg_ssl_if_conn_close_notify(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + if (ctx == NULL) return; + SSL_shutdown(ctx->ssl); +} + +void mg_ssl_if_conn_free(struct mg_connection *nc) { + struct mg_ssl_if_ctx *ctx = (struct mg_ssl_if_ctx *) nc->ssl_if_data; + if (ctx == NULL) return; + nc->ssl_if_data = NULL; + if (ctx->ssl != NULL) SSL_free(ctx->ssl); + if (ctx->ssl_ctx != NULL && nc->listener == NULL) SSL_CTX_free(ctx->ssl_ctx); + mbuf_free(&ctx->psk); + memset(ctx, 0, sizeof(*ctx)); + MG_FREE(ctx); +} + +/* + * Cipher suite options used for TLS negotiation. + * https://wiki.mozilla.org/Security/Server_Side_TLS#Recommended_configurations + */ +static const char mg_s_cipher_list[] = +#if defined(MG_SSL_CRYPTO_MODERN) + "ECDHE-ECDSA-AES256-GCM-SHA384:ECDHE-RSA-AES256-GCM-SHA384:" + "ECDHE-ECDSA-AES128-GCM-SHA256:ECDHE-RSA-AES128-GCM-SHA256:" + "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:" + "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:" + "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:" + "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:" + "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:" + "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:" + "!aNULL:!eNULL:!EXPORT:!DES:!RC4:!3DES:!MD5:!PSK" +#elif defined(MG_SSL_CRYPTO_OLD) + "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:" + "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:" + "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:" + "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:" + "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:" + "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:" + "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:" + "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:ECDHE-RSA-DES-CBC3-SHA:" + "ECDHE-ECDSA-DES-CBC3-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:" + "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:DES-CBC3-SHA:" + "HIGH:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:" + "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA" +#else /* Default - intermediate. */ + "ECDHE-RSA-AES128-GCM-SHA256:ECDHE-ECDSA-AES128-GCM-SHA256:" + "ECDHE-RSA-AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384:" + "DHE-RSA-AES128-GCM-SHA256:DHE-DSS-AES128-GCM-SHA256:kEDH+AESGCM:" + "ECDHE-RSA-AES128-SHA256:ECDHE-ECDSA-AES128-SHA256:ECDHE-RSA-AES128-SHA:" + "ECDHE-ECDSA-AES128-SHA:ECDHE-RSA-AES256-SHA384:ECDHE-ECDSA-AES256-SHA384:" + "ECDHE-RSA-AES256-SHA:ECDHE-ECDSA-AES256-SHA:DHE-RSA-AES128-SHA256:" + "DHE-RSA-AES128-SHA:DHE-DSS-AES128-SHA256:DHE-RSA-AES256-SHA256:" + "DHE-DSS-AES256-SHA:DHE-RSA-AES256-SHA:AES128-GCM-SHA256:AES256-GCM-SHA384:" + "AES128-SHA256:AES256-SHA256:AES128-SHA:AES256-SHA:AES:CAMELLIA:" + "DES-CBC3-SHA:!aNULL:!eNULL:!EXPORT:!DES:!RC4:!MD5:!PSK:!aECDH:" + "!EDH-DSS-DES-CBC3-SHA:!EDH-RSA-DES-CBC3-SHA:!KRB5-DES-CBC3-SHA" +#endif + ; + +/* + * Default DH params for PFS cipher negotiation. This is a 2048-bit group. + * Will be used if none are provided by the user in the certificate file. + */ +#if !MG_DISABLE_PFS && !defined(KR_VERSION) +static const char mg_s_default_dh_params[] = + "\ +-----BEGIN DH PARAMETERS-----\n\ +MIIBCAKCAQEAlvbgD/qh9znWIlGFcV0zdltD7rq8FeShIqIhkQ0C7hYFThrBvF2E\n\ +Z9bmgaP+sfQwGpVlv9mtaWjvERbu6mEG7JTkgmVUJrUt/wiRzwTaCXBqZkdUO8Tq\n\ ++E6VOEQAilstG90ikN1Tfo+K6+X68XkRUIlgawBTKuvKVwBhuvlqTGerOtnXWnrt\n\ +ym//hd3cd5PBYGBix0i7oR4xdghvfR2WLVu0LgdThTBb6XP7gLd19cQ1JuBtAajZ\n\ +wMuPn7qlUkEFDIkAZy59/Hue/H2Q2vU/JsvVhHWCQBL4F1ofEAt50il6ZxR1QfFK\n\ +9VGKDC4oOgm9DlxwwBoC2FjqmvQlqVV3kwIBAg==\n\ +-----END DH PARAMETERS-----\n"; +#endif + +static enum mg_ssl_if_result mg_use_ca_cert(SSL_CTX *ctx, const char *cert) { + if (cert == NULL || strcmp(cert, "*") == 0) { + return MG_SSL_OK; + } + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT, 0); + return SSL_CTX_load_verify_locations(ctx, cert, NULL) == 1 ? MG_SSL_OK + : MG_SSL_ERROR; +} + +static enum mg_ssl_if_result mg_use_cert(SSL_CTX *ctx, const char *cert, + const char *key, + const char **err_msg) { + if (key == NULL) key = cert; + if (cert == NULL || cert[0] == '\0' || key == NULL || key[0] == '\0') { + return MG_SSL_OK; + } else if (SSL_CTX_use_certificate_file(ctx, cert, 1) == 0) { + MG_SET_PTRPTR(err_msg, "Invalid SSL cert"); + return MG_SSL_ERROR; + } else if (SSL_CTX_use_PrivateKey_file(ctx, key, 1) == 0) { + MG_SET_PTRPTR(err_msg, "Invalid SSL key"); + return MG_SSL_ERROR; + } else if (SSL_CTX_use_certificate_chain_file(ctx, cert) == 0) { + MG_SET_PTRPTR(err_msg, "Invalid CA bundle"); + return MG_SSL_ERROR; + } else { + SSL_CTX_set_mode(ctx, SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER); +#if !MG_DISABLE_PFS && !defined(KR_VERSION) + BIO *bio = NULL; + DH *dh = NULL; + + /* Try to read DH parameters from the cert/key file. */ + bio = BIO_new_file(cert, "r"); + if (bio != NULL) { + dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); + BIO_free(bio); + } + /* + * If there are no DH params in the file, fall back to hard-coded ones. + * Not ideal, but better than nothing. + */ + if (dh == NULL) { + bio = BIO_new_mem_buf((void *) mg_s_default_dh_params, -1); + dh = PEM_read_bio_DHparams(bio, NULL, NULL, NULL); + BIO_free(bio); + } + if (dh != NULL) { + SSL_CTX_set_tmp_dh(ctx, dh); + SSL_CTX_set_options(ctx, SSL_OP_SINGLE_DH_USE); + DH_free(dh); + } +#if OPENSSL_VERSION_NUMBER > 0x10002000L + SSL_CTX_set_ecdh_auto(ctx, 1); +#endif +#endif + } + return MG_SSL_OK; +} + +static enum mg_ssl_if_result mg_set_cipher_list(SSL_CTX *ctx, const char *cl) { + return (SSL_CTX_set_cipher_list(ctx, cl ? cl : mg_s_cipher_list) == 1 + ? MG_SSL_OK + : MG_SSL_ERROR); +} + +#ifndef KR_VERSION +static unsigned int mg_ssl_if_ossl_psk_cb(SSL *ssl, const char *hint, + char *identity, + unsigned int max_identity_len, + unsigned char *psk, + unsigned int max_psk_len) { + struct mg_ssl_if_ctx *ctx = + (struct mg_ssl_if_ctx *) SSL_CTX_get_app_data(SSL_get_SSL_CTX(ssl)); + size_t key_len = ctx->psk.len - ctx->identity_len - 1; + DBG(("hint: '%s'", (hint ? hint : ""))); + if (ctx->identity_len + 1 > max_identity_len) { + DBG(("identity too long")); + return 0; + } + if (key_len > max_psk_len) { + DBG(("key too long")); + return 0; + } + memcpy(identity, ctx->psk.buf, ctx->identity_len + 1); + memcpy(psk, ctx->psk.buf + ctx->identity_len + 1, key_len); + (void) ssl; + return key_len; +} + +static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx, + const char *identity, + const char *key_str) { + unsigned char key[32]; + size_t key_len; + size_t i = 0; + if (identity == NULL && key_str == NULL) return MG_SSL_OK; + if (identity == NULL || key_str == NULL) return MG_SSL_ERROR; + key_len = strlen(key_str); + if (key_len != 32 && key_len != 64) return MG_SSL_ERROR; + memset(key, 0, sizeof(key)); + key_len = 0; + for (i = 0; key_str[i] != '\0'; i++) { + unsigned char c; + char hc = tolower((int) key_str[i]); + if (hc >= '0' && hc <= '9') { + c = hc - '0'; + } else if (hc >= 'a' && hc <= 'f') { + c = hc - 'a' + 0xa; + } else { + return MG_SSL_ERROR; + } + key_len = i / 2; + key[key_len] <<= 4; + key[key_len] |= c; + } + key_len++; + DBG(("identity = '%s', key = (%u)", identity, (unsigned int) key_len)); + ctx->identity_len = strlen(identity); + mbuf_append(&ctx->psk, identity, ctx->identity_len + 1); + mbuf_append(&ctx->psk, key, key_len); + SSL_CTX_set_psk_client_callback(ctx->ssl_ctx, mg_ssl_if_ossl_psk_cb); + SSL_CTX_set_app_data(ctx->ssl_ctx, ctx); + return MG_SSL_OK; +} +#else +static enum mg_ssl_if_result mg_ssl_if_ossl_set_psk(struct mg_ssl_if_ctx *ctx, + const char *identity, + const char *key_str) { + (void) ctx; + (void) identity; + (void) key_str; + /* Krypton does not support PSK. */ + return MG_SSL_ERROR; +} +#endif /* defined(KR_VERSION) */ + +const char *mg_set_ssl(struct mg_connection *nc, const char *cert, + const char *ca_cert) { + const char *err_msg = NULL; + struct mg_ssl_if_conn_params params; + memset(¶ms, 0, sizeof(params)); + params.cert = cert; + params.ca_cert = ca_cert; + if (mg_ssl_if_conn_init(nc, ¶ms, &err_msg) != MG_SSL_OK) { + return err_msg; + } + return NULL; +} + +#endif /* MG_ENABLE_SSL && MG_SSL_IF == MG_SSL_IF_OPENSSL */ diff --git a/src/tun.c b/src/tun.c new file mode 100644 index 00000000..da52ce76 --- /dev/null +++ b/src/tun.c @@ -0,0 +1,315 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#if MG_ENABLE_TUN + +#include "common/cs_dbg.h" +#include "mongoose/src/http.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/net.h" +#include "mongoose/src/net_if_tun.h" +#include "mongoose/src/tun.h" +#include "mongoose/src/util.h" + +static void mg_tun_reconnect(struct mg_tun_client *client, int timeout); + +static void mg_tun_init_client(struct mg_tun_client *client, struct mg_mgr *mgr, + struct mg_iface *iface, const char *dispatcher, + struct mg_tun_ssl_opts ssl) { + client->mgr = mgr; + client->iface = iface; + client->disp_url = dispatcher; + client->last_stream_id = 0; + client->ssl = ssl; + + client->disp = NULL; /* will be set by mg_tun_reconnect */ + client->listener = NULL; /* will be set by mg_do_bind */ + client->reconnect = NULL; /* will be set by mg_tun_reconnect */ +} + +void mg_tun_log_frame(struct mg_tun_frame *frame) { + LOG(LL_DEBUG, ("Got TUN frame: type=0x%x, flags=0x%x stream_id=0x%x, " + "len=%d", + frame->type, frame->flags, (unsigned int) frame->stream_id, + (int) frame->body.len)); +#if MG_ENABLE_HEXDUMP + { + char hex[512]; + mg_hexdump(frame->body.p, frame->body.len, hex, sizeof(hex) - 1); + hex[sizeof(hex) - 1] = '\0'; + LOG(LL_DEBUG, ("body:\n%s", hex)); + } +#else + LOG(LL_DEBUG, ("body: '%.*s'", (int) frame->body.len, frame->body.p)); +#endif +} + +static void mg_tun_close_all(struct mg_tun_client *client) { + struct mg_connection *nc; + for (nc = client->mgr->active_connections; nc != NULL; nc = nc->next) { + if (nc->iface == client->iface && !(nc->flags & MG_F_LISTENING)) { + LOG(LL_DEBUG, ("Closing tunneled connection %p", nc)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + /* mg_close_conn(nc); */ + } + } +} + +static void mg_tun_client_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { +#if !MG_ENABLE_CALLBACK_USERDATA + void *user_data = nc->user_data; +#else + (void) nc; +#endif + struct mg_tun_client *client = (struct mg_tun_client *) user_data; + + switch (ev) { + case MG_EV_CONNECT: { + int err = *(int *) ev_data; + + if (err) { + LOG(LL_ERROR, ("Cannot connect to the tunnel dispatcher: %d", err)); + } else { + LOG(LL_INFO, ("Connected to the tunnel dispatcher")); + } + break; + } + case MG_EV_HTTP_REPLY: { + struct http_message *hm = (struct http_message *) ev_data; + + if (hm->resp_code != 200) { + LOG(LL_ERROR, + ("Tunnel dispatcher reply non-OK status code %d", hm->resp_code)); + } + break; + } + case MG_EV_WEBSOCKET_HANDSHAKE_DONE: { + LOG(LL_INFO, ("Tunnel dispatcher handshake done")); + break; + } + case MG_EV_WEBSOCKET_FRAME: { + struct websocket_message *wm = (struct websocket_message *) ev_data; + struct mg_connection *tc; + struct mg_tun_frame frame; + + if (mg_tun_parse_frame(wm->data, wm->size, &frame) == -1) { + LOG(LL_ERROR, ("Got invalid tun frame dropping")); + break; + } + + mg_tun_log_frame(&frame); + + tc = mg_tun_if_find_conn(client, frame.stream_id); + if (tc == NULL) { + if (frame.body.len > 0) { + LOG(LL_DEBUG, ("Got frame after receiving end has been closed")); + } + break; + } + if (frame.body.len > 0) { + mg_if_recv_tcp_cb(tc, (void *) frame.body.p, frame.body.len, + 0 /* own */); + } + if (frame.flags & MG_TUN_F_END_STREAM) { + LOG(LL_DEBUG, ("Closing tunneled connection because got end of stream " + "from other end")); + tc->flags |= MG_F_CLOSE_IMMEDIATELY; + mg_close_conn(tc); + } + break; + } + case MG_EV_CLOSE: { + LOG(LL_DEBUG, ("Closing all tunneled connections")); + /* + * The client might have been already freed when the listening socket is + * closed. + */ + if (client != NULL) { + mg_tun_close_all(client); + client->disp = NULL; + LOG(LL_INFO, ("Dispatcher connection is no more, reconnecting")); + /* TODO(mkm): implement exp back off */ + mg_tun_reconnect(client, MG_TUN_RECONNECT_INTERVAL); + } + break; + } + default: + break; + } +} + +static void mg_tun_do_reconnect(struct mg_tun_client *client) { + struct mg_connection *dc; + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); +#if MG_ENABLE_SSL + opts.ssl_cert = client->ssl.ssl_cert; + opts.ssl_key = client->ssl.ssl_key; + opts.ssl_ca_cert = client->ssl.ssl_ca_cert; +#endif + /* HTTP/Websocket listener */ + if ((dc = mg_connect_ws_opt(client->mgr, MG_CB(mg_tun_client_handler, client), + opts, client->disp_url, MG_TUN_PROTO_NAME, + NULL)) == NULL) { + LOG(LL_ERROR, + ("Cannot connect to WS server on addr [%s]\n", client->disp_url)); + return; + } + + client->disp = dc; +#if !MG_ENABLE_CALLBACK_USERDATA + dc->user_data = client; +#endif +} + +void mg_tun_reconnect_ev_handler(struct mg_connection *nc, int ev, + void *ev_data MG_UD_ARG(void *user_data)) { +#if !MG_ENABLE_CALLBACK_USERDATA + void *user_data = nc->user_data; +#else + (void) nc; +#endif + struct mg_tun_client *client = (struct mg_tun_client *) user_data; + (void) ev_data; + + switch (ev) { + case MG_EV_TIMER: + if (!(client->listener->flags & MG_F_TUN_DO_NOT_RECONNECT)) { + mg_tun_do_reconnect(client); + } else { + /* Reconnecting is suppressed, we'll check again at the next poll */ + mg_tun_reconnect(client, 0); + } + break; + } +} + +static void mg_tun_reconnect(struct mg_tun_client *client, int timeout) { + if (client->reconnect == NULL) { + client->reconnect = mg_add_sock(client->mgr, INVALID_SOCKET, + MG_CB(mg_tun_reconnect_ev_handler, client)); +#if !MG_ENABLE_CALLBACK_USERDATA + client->reconnect->user_data = client; +#endif + } + client->reconnect->ev_timer_time = mg_time() + timeout; +} + +static struct mg_tun_client *mg_tun_create_client(struct mg_mgr *mgr, + const char *dispatcher, + struct mg_tun_ssl_opts ssl) { + struct mg_tun_client *client = NULL; + struct mg_iface *iface = mg_find_iface(mgr, &mg_tun_iface_vtable, NULL); + if (iface == NULL) { + LOG(LL_ERROR, ("The tun feature requires the manager to have a tun " + "interface enabled")); + return NULL; + } + + client = (struct mg_tun_client *) MG_MALLOC(sizeof(*client)); + mg_tun_init_client(client, mgr, iface, dispatcher, ssl); + iface->data = client; + + /* + * We need to give application a chance to set MG_F_TUN_DO_NOT_RECONNECT on a + * listening connection right after mg_tun_bind_opt() returned it, so we + * should use mg_tun_reconnect() here, instead of mg_tun_do_reconnect() + */ + mg_tun_reconnect(client, 0); + return client; +} + +void mg_tun_destroy_client(struct mg_tun_client *client) { + /* + * NOTE: + * `client` is NULL in case of OOM + * `client->disp` is NULL if connection failed + * `client->iface is NULL is `mg_find_iface` failed + */ + + if (client != NULL && client->disp != NULL) { + /* the dispatcher connection handler will in turn close all tunnels */ + client->disp->flags |= MG_F_CLOSE_IMMEDIATELY; + /* this is used as a signal to other tun handlers that the party is over */ + client->disp->user_data = NULL; + } + + if (client != NULL && client->reconnect != NULL) { + client->reconnect->flags |= MG_F_CLOSE_IMMEDIATELY; + } + + if (client != NULL && client->iface != NULL) { + client->iface->data = NULL; + } + + MG_FREE(client); +} + +static struct mg_connection *mg_tun_do_bind(struct mg_tun_client *client, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_bind_opts opts) { + struct mg_connection *lc; + opts.iface = client->iface; + lc = mg_bind_opt(client->mgr, ":1234" /* dummy port */, + MG_CB(handler, user_data), opts); + client->listener = lc; + return lc; +} + +struct mg_connection *mg_tun_bind_opt(struct mg_mgr *mgr, + const char *dispatcher, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_bind_opts opts) { +#if MG_ENABLE_SSL + struct mg_tun_ssl_opts ssl = {opts.ssl_cert, opts.ssl_key, opts.ssl_ca_cert}; +#else + struct mg_tun_ssl_opts ssl = {0}; +#endif + struct mg_tun_client *client = mg_tun_create_client(mgr, dispatcher, ssl); + if (client == NULL) { + return NULL; + } +#if MG_ENABLE_SSL + /* these options don't make sense in the local mouth of the tunnel */ + opts.ssl_cert = NULL; + opts.ssl_key = NULL; + opts.ssl_ca_cert = NULL; +#endif + return mg_tun_do_bind(client, MG_CB(handler, user_data), opts); +} + +int mg_tun_parse_frame(void *data, size_t len, struct mg_tun_frame *frame) { + const size_t header_size = sizeof(uint32_t) + sizeof(uint8_t) * 2; + if (len < header_size) { + return -1; + } + + frame->type = *(uint8_t *) (data); + frame->flags = *(uint8_t *) ((char *) data + 1); + memcpy(&frame->stream_id, (char *) data + 2, sizeof(uint32_t)); + frame->stream_id = ntohl(frame->stream_id); + frame->body.p = (char *) data + header_size; + frame->body.len = len - header_size; + return 0; +} + +void mg_tun_send_frame(struct mg_connection *ws, uint32_t stream_id, + uint8_t type, uint8_t flags, struct mg_str msg) { + stream_id = htonl(stream_id); + { + struct mg_str parts[] = { + {(char *) &type, sizeof(type)}, + {(char *) &flags, sizeof(flags)}, + {(char *) &stream_id, sizeof(stream_id)}, + {msg.p, msg.len} /* vc6 doesn't like just `msg` here */}; + mg_send_websocket_framev(ws, WEBSOCKET_OP_BINARY, parts, + sizeof(parts) / sizeof(parts[0])); + } +} + +#endif /* MG_ENABLE_TUN */ diff --git a/src/tun.h b/src/tun.h new file mode 100644 index 00000000..d305b0b3 --- /dev/null +++ b/src/tun.h @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2014-2016 Cesanta Software Limited + * All rights reserved + */ + +#ifndef CS_MONGOOSE_SRC_TUN_H_ +#define CS_MONGOOSE_SRC_TUN_H_ + +#if MG_ENABLE_TUN + +#include "mongoose/src/net.h" +#include "common/mg_str.h" + +#ifndef MG_TUN_RECONNECT_INTERVAL +#define MG_TUN_RECONNECT_INTERVAL 1 +#endif + +#define MG_TUN_PROTO_NAME "mg_tun" + +#define MG_TUN_DATA_FRAME 0x0 +#define MG_TUN_F_END_STREAM 0x1 + +/* + * MG TUN frame format is loosely based on HTTP/2. + * However since the communication happens via WebSocket + * there is no need to encode the frame length, since that's + * solved by WebSocket framing. + * + * TODO(mkm): Detailed description of the protocol. + */ +struct mg_tun_frame { + uint8_t type; + uint8_t flags; + uint32_t stream_id; /* opaque stream identifier */ + struct mg_str body; +}; + +struct mg_tun_ssl_opts { +#if MG_ENABLE_SSL + const char *ssl_cert; + const char *ssl_key; + const char *ssl_ca_cert; +#else + int dummy; /* some compilers don't like empty structs */ +#endif +}; + +struct mg_tun_client { + struct mg_mgr *mgr; + struct mg_iface *iface; + const char *disp_url; + struct mg_tun_ssl_opts ssl; + + uint32_t last_stream_id; /* stream id of most recently accepted connection */ + + struct mg_connection *disp; + struct mg_connection *listener; + struct mg_connection *reconnect; +}; + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +struct mg_connection *mg_tun_bind_opt(struct mg_mgr *mgr, + const char *dispatcher, + MG_CB(mg_event_handler_t handler, + void *user_data), + struct mg_bind_opts opts); + +int mg_tun_parse_frame(void *data, size_t len, struct mg_tun_frame *frame); + +void mg_tun_send_frame(struct mg_connection *ws, uint32_t stream_id, + uint8_t type, uint8_t flags, struct mg_str msg); + +void mg_tun_destroy_client(struct mg_tun_client *client); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ + +#endif /* MG_ENABLE_TUN */ + +#endif /* CS_MONGOOSE_SRC_TUN_H_ */ diff --git a/src/uri.c b/src/uri.c new file mode 100644 index 00000000..90c397f3 --- /dev/null +++ b/src/uri.c @@ -0,0 +1,261 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#include "mongoose/src/internal.h" +#include "mongoose/src/uri.h" + +/* + * scan string until encountering one of `seps`, keeping track of component + * boundaries in `res`. + * + * `p` will point to the char after the separator or it will be `end`. + */ +static void parse_uri_component(const char **p, const char *end, + const char *seps, struct mg_str *res) { + const char *q; + res->p = *p; + for (; *p < end; (*p)++) { + for (q = seps; *q != '\0'; q++) { + if (**p == *q) break; + } + if (*q != '\0') break; + } + res->len = (*p) - res->p; + if (*p < end) (*p)++; +} + +int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme, + struct mg_str *user_info, struct mg_str *host, + unsigned int *port, struct mg_str *path, struct mg_str *query, + struct mg_str *fragment) { + struct mg_str rscheme = {0, 0}, ruser_info = {0, 0}, rhost = {0, 0}, + rpath = {0, 0}, rquery = {0, 0}, rfragment = {0, 0}; + unsigned int rport = 0; + enum { + P_START, + P_SCHEME_OR_PORT, + P_USER_INFO, + P_HOST, + P_PORT, + P_REST + } state = P_START; + + const char *p = uri.p, *end = p + uri.len; + while (p < end) { + switch (state) { + case P_START: + /* + * expecting on of: + * - `scheme://xxxx` + * - `xxxx:port` + * - `[a:b:c]:port` + * - `xxxx/path` + */ + if (*p == '[') { + state = P_HOST; + break; + } + for (; p < end; p++) { + if (*p == ':') { + state = P_SCHEME_OR_PORT; + break; + } else if (*p == '/') { + state = P_REST; + break; + } + } + if (state == P_START || state == P_REST) { + rhost.p = uri.p; + rhost.len = p - uri.p; + } + break; + case P_SCHEME_OR_PORT: + if (end - p >= 3 && strncmp(p, "://", 3) == 0) { + rscheme.p = uri.p; + rscheme.len = p - uri.p; + state = P_USER_INFO; + p += 3; + } else { + rhost.p = uri.p; + rhost.len = p - uri.p; + state = P_PORT; + } + break; + case P_USER_INFO: + ruser_info.p = p; + for (; p < end; p++) { + if (*p == '@' || *p == '[' || *p == '/') { + break; + } + } + if (p == end || *p == '/' || *p == '[') { + /* backtrack and parse as host */ + p = ruser_info.p; + } + ruser_info.len = p - ruser_info.p; + state = P_HOST; + break; + case P_HOST: + if (*p == '@') p++; + rhost.p = p; + if (*p == '[') { + int found = 0; + for (; !found && p < end; p++) { + found = (*p == ']'); + } + if (!found) return -1; + } else { + for (; p < end; p++) { + if (*p == ':' || *p == '/') break; + } + } + rhost.len = p - rhost.p; + if (p < end) { + if (*p == ':') { + state = P_PORT; + break; + } else if (*p == '/') { + state = P_REST; + break; + } + } + break; + case P_PORT: + p++; + for (; p < end; p++) { + if (*p == '/') { + state = P_REST; + break; + } + rport *= 10; + rport += *p - '0'; + } + break; + case P_REST: + /* `p` points to separator. `path` includes the separator */ + parse_uri_component(&p, end, "?#", &rpath); + if (p < end && *(p - 1) == '?') { + parse_uri_component(&p, end, "#", &rquery); + } + parse_uri_component(&p, end, "", &rfragment); + break; + } + } + + if (scheme != 0) *scheme = rscheme; + if (user_info != 0) *user_info = ruser_info; + if (host != 0) *host = rhost; + if (port != 0) *port = rport; + if (path != 0) *path = rpath; + if (query != 0) *query = rquery; + if (fragment != 0) *fragment = rfragment; + + return 0; +} + +/* Normalize the URI path. Remove/resolve "." and "..". */ +int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out) { + const char *s = in->p, *se = s + in->len; + char *cp = (char *) out->p, *d; + + if (in->len == 0 || *s != '/') { + out->len = 0; + return 0; + } + + d = cp; + + while (s < se) { + const char *next = s; + struct mg_str component; + parse_uri_component(&next, se, "/", &component); + if (mg_vcmp(&component, ".") == 0) { + /* Yum. */ + } else if (mg_vcmp(&component, "..") == 0) { + /* Backtrack to previous slash. */ + if (d > cp + 1 && *(d - 1) == '/') d--; + while (d > cp && *(d - 1) != '/') d--; + } else { + memmove(d, s, next - s); + d += next - s; + } + s = next; + } + if (d == cp) *d++ = '/'; + + out->p = cp; + out->len = d - cp; + return 1; +} + +int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info, + const struct mg_str *host, unsigned int port, + const struct mg_str *path, const struct mg_str *query, + const struct mg_str *fragment, int normalize_path, + struct mg_str *uri) { + int result = -1; + struct mbuf out; + mbuf_init(&out, 0); + + if (scheme != NULL && scheme->len > 0) { + mbuf_append(&out, scheme->p, scheme->len); + mbuf_append(&out, "://", 3); + } + + if (user_info != NULL && user_info->len > 0) { + mbuf_append(&out, user_info->p, user_info->len); + mbuf_append(&out, "@", 1); + } + + if (host != NULL && host->len > 0) { + mbuf_append(&out, host->p, host->len); + } + + if (port != 0) { + char port_str[20]; + int port_str_len = sprintf(port_str, ":%u", port); + mbuf_append(&out, port_str, port_str_len); + } + + if (path != NULL && path->len > 0) { + if (normalize_path) { + struct mg_str npath = mg_strdup(*path); + if (npath.len != path->len) goto out; + if (!mg_normalize_uri_path(path, &npath)) { + free((void *) npath.p); + goto out; + } + mbuf_append(&out, npath.p, npath.len); + free((void *) npath.p); + } else { + mbuf_append(&out, path->p, path->len); + } + } else if (normalize_path) { + mbuf_append(&out, "/", 1); + } + + if (query != NULL && query->len > 0) { + mbuf_append(&out, "?", 1); + mbuf_append(&out, query->p, query->len); + } + + if (fragment != NULL && fragment->len > 0) { + mbuf_append(&out, "#", 1); + mbuf_append(&out, fragment->p, fragment->len); + } + + result = 0; + +out: + if (result == 0) { + uri->p = out.buf; + uri->len = out.len; + } else { + mbuf_free(&out); + uri->p = NULL; + uri->len = 0; + } + return result; +} diff --git a/src/uri.h b/src/uri.h new file mode 100644 index 00000000..7e31d54a --- /dev/null +++ b/src/uri.h @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === URI + */ + +#ifndef CS_MONGOOSE_SRC_URI_H_ +#define CS_MONGOOSE_SRC_URI_H_ + +#include "mongoose/src/net.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/* + * Parses an URI and fills string chunks with locations of the respective + * uri components within the input uri string. NULL pointers will be + * ignored. + * + * General syntax: + * + * [scheme://[user_info@]]host[:port][/path][?query][#fragment] + * + * Example: + * + * foo.com:80 + * tcp://foo.com:1234 + * http://foo.com:80/bar?baz=1 + * https://user:pw@foo.com:443/blah + * + * `path` will include the leading slash. `query` won't include the leading `?`. + * `host` can contain embedded colons if surrounded by square brackets in order + * to support IPv6 literal addresses. + * + * + * Returns 0 on success, -1 on error. + */ +int mg_parse_uri(const struct mg_str uri, struct mg_str *scheme, + struct mg_str *user_info, struct mg_str *host, + unsigned int *port, struct mg_str *path, struct mg_str *query, + struct mg_str *fragment); + +/* + * Assemble URI from parts. Any of the inputs can be NULL or zero-length mg_str. + * + * If normalize_path is true, path is normalized by resolving relative refs. + * + * Result is a heap-allocated string (uri->p must be free()d after use). + * + * Returns 0 on success, -1 on error. + */ +int mg_assemble_uri(const struct mg_str *scheme, const struct mg_str *user_info, + const struct mg_str *host, unsigned int port, + const struct mg_str *path, const struct mg_str *query, + const struct mg_str *fragment, int normalize_path, + struct mg_str *uri); + +int mg_normalize_uri_path(const struct mg_str *in, struct mg_str *out); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CS_MONGOOSE_SRC_URI_H_ */ diff --git a/src/util.c b/src/util.c new file mode 100644 index 00000000..4202c532 --- /dev/null +++ b/src/util.c @@ -0,0 +1,336 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +#include "common/cs_base64.h" +#include "mongoose/src/internal.h" +#include "mongoose/src/util.h" + +/* For platforms with limited libc */ +#ifndef MAX +#define MAX(a, b) ((a) > (b) ? (a) : (b)) +#endif + +const char *mg_skip(const char *s, const char *end, const char *delims, + struct mg_str *v) { + v->p = s; + while (s < end && strchr(delims, *(unsigned char *) s) == NULL) s++; + v->len = s - v->p; + while (s < end && strchr(delims, *(unsigned char *) s) != NULL) s++; + return s; +} + +#if MG_ENABLE_FILESYSTEM && !defined(MG_USER_FILE_FUNCTIONS) +int mg_stat(const char *path, cs_stat_t *st) { +#ifdef _WIN32 + wchar_t wpath[MG_MAX_PATH]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + DBG(("[%ls] -> %d", wpath, _wstati64(wpath, st))); + return _wstati64(wpath, st); +#else + return stat(path, st); +#endif +} + +FILE *mg_fopen(const char *path, const char *mode) { +#ifdef _WIN32 + wchar_t wpath[MG_MAX_PATH], wmode[10]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + to_wchar(mode, wmode, ARRAY_SIZE(wmode)); + return _wfopen(wpath, wmode); +#else + return fopen(path, mode); +#endif +} + +int mg_open(const char *path, int flag, int mode) { /* LCOV_EXCL_LINE */ +#if defined(_WIN32) && !defined(WINCE) + wchar_t wpath[MG_MAX_PATH]; + to_wchar(path, wpath, ARRAY_SIZE(wpath)); + return _wopen(wpath, flag, mode); +#else + return open(path, flag, mode); /* LCOV_EXCL_LINE */ +#endif +} + +size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f) { + return fread(ptr, size, count, f); +} + +size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f) { + return fwrite(ptr, size, count, f); +} +#endif + +void mg_base64_encode(const unsigned char *src, int src_len, char *dst) { + cs_base64_encode(src, src_len, dst); +} + +int mg_base64_decode(const unsigned char *s, int len, char *dst) { + return cs_base64_decode(s, len, dst, NULL); +} + +#if MG_ENABLE_THREADS +void *mg_start_thread(void *(*f)(void *), void *p) { +#ifdef WINCE + return (void *) CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE) f, p, 0, NULL); +#elif defined(_WIN32) + return (void *) _beginthread((void(__cdecl *) (void *) ) f, 0, p); +#else + pthread_t thread_id = (pthread_t) 0; + pthread_attr_t attr; + + (void) pthread_attr_init(&attr); + (void) pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + +#if defined(MG_STACK_SIZE) && MG_STACK_SIZE > 1 + (void) pthread_attr_setstacksize(&attr, MG_STACK_SIZE); +#endif + + pthread_create(&thread_id, &attr, f, p); + pthread_attr_destroy(&attr); + + return (void *) thread_id; +#endif +} +#endif /* MG_ENABLE_THREADS */ + +/* Set close-on-exec bit for a given socket. */ +void mg_set_close_on_exec(sock_t sock) { +#if defined(_WIN32) && !defined(WINCE) + (void) SetHandleInformation((HANDLE) sock, HANDLE_FLAG_INHERIT, 0); +#elif defined(__unix__) + fcntl(sock, F_SETFD, FD_CLOEXEC); +#else + (void) sock; +#endif +} + +int mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, + int flags) { + int is_v6; + if (buf == NULL || len <= 0) return 0; + memset(buf, 0, len); +#if MG_ENABLE_IPV6 + is_v6 = sa->sa.sa_family == AF_INET6; +#else + is_v6 = 0; +#endif + if (flags & MG_SOCK_STRINGIFY_IP) { +#if MG_ENABLE_IPV6 + const void *addr = NULL; + char *start = buf; + socklen_t capacity = len; + if (!is_v6) { + addr = &sa->sin.sin_addr; + } else { + addr = (void *) &sa->sin6.sin6_addr; + if (flags & MG_SOCK_STRINGIFY_PORT) { + *buf = '['; + start++; + capacity--; + } + } + if (inet_ntop(sa->sa.sa_family, addr, start, capacity) == NULL) { + goto cleanup; + } +#elif defined(_WIN32) || MG_LWIP || (MG_NET_IF == MG_NET_IF_PIC32) + /* Only Windoze Vista (and newer) have inet_ntop() */ + char *addr_str = inet_ntoa(sa->sin.sin_addr); + if (addr_str != NULL) { + strncpy(buf, inet_ntoa(sa->sin.sin_addr), len - 1); + } else { + goto cleanup; + } +#else + if (inet_ntop(AF_INET, (void *) &sa->sin.sin_addr, buf, len - 1) == NULL) { + goto cleanup; + } +#endif + } + if (flags & MG_SOCK_STRINGIFY_PORT) { + int port = ntohs(sa->sin.sin_port); + if (flags & MG_SOCK_STRINGIFY_IP) { + int buf_len = strlen(buf); + snprintf(buf + buf_len, len - (buf_len + 1), "%s:%d", (is_v6 ? "]" : ""), + port); + } else { + snprintf(buf, len, "%d", port); + } + } + + return strlen(buf); + +cleanup: + *buf = '\0'; + return 0; +} + +int mg_conn_addr_to_str(struct mg_connection *nc, char *buf, size_t len, + int flags) { + union socket_address sa; + memset(&sa, 0, sizeof(sa)); + mg_if_get_conn_addr(nc, flags & MG_SOCK_STRINGIFY_REMOTE, &sa); + return mg_sock_addr_to_str(&sa, buf, len, flags); +} + +#if MG_ENABLE_HEXDUMP +static int mg_hexdump_n(const void *buf, int len, char *dst, int dst_len, + int offset) { + const unsigned char *p = (const unsigned char *) buf; + char ascii[17] = ""; + int i, idx, n = 0; + + for (i = 0; i < len; i++) { + idx = i % 16; + if (idx == 0) { + if (i > 0) n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n", ascii); + n += snprintf(dst + n, MAX(dst_len - n, 0), "%04x ", i + offset); + } + if (dst_len - n < 0) { + return n; + } + n += snprintf(dst + n, MAX(dst_len - n, 0), " %02x", p[i]); + ascii[idx] = p[i] < 0x20 || p[i] > 0x7e ? '.' : p[i]; + ascii[idx + 1] = '\0'; + } + + while (i++ % 16) n += snprintf(dst + n, MAX(dst_len - n, 0), "%s", " "); + n += snprintf(dst + n, MAX(dst_len - n, 0), " %s\n", ascii); + + return n; +} + +int mg_hexdump(const void *buf, int len, char *dst, int dst_len) { + return mg_hexdump_n(buf, len, dst, dst_len, 0); +} + +void mg_hexdumpf(FILE *fp, const void *buf, int len) { + char tmp[80]; + int offset = 0, n; + while (len > 0) { + n = (len < 16 ? len : 16); + mg_hexdump_n(((const char *) buf) + offset, n, tmp, sizeof(tmp), offset); + fputs(tmp, fp); + offset += n; + len -= n; + } +} + +void mg_hexdump_connection(struct mg_connection *nc, const char *path, + const void *buf, int num_bytes, int ev) { + FILE *fp = NULL; + char src[60], dst[60]; + const char *tag = NULL; + switch (ev) { + case MG_EV_RECV: + tag = "<-"; + break; + case MG_EV_SEND: + tag = "->"; + break; + case MG_EV_ACCEPT: + tag = " 0) { + mg_hexdumpf(fp, buf, num_bytes); + } + if (fp != stdout && fp != stderr) fclose(fp); +} +#endif + +int mg_is_big_endian(void) { + static const int n = 1; + /* TODO(mkm) use compiletime check with 4-byte char literal */ + return ((char *) &n)[0] == 0; +} + +DO_NOT_WARN_UNUSED MG_INTERNAL int mg_get_errno(void) { +#ifndef WINCE + return errno; +#else + /* TODO(alashkin): translate error codes? */ + return GetLastError(); +#endif +} + +void mg_mbuf_append_base64_putc(char ch, void *user_data) { + struct mbuf *mbuf = (struct mbuf *) user_data; + mbuf_append(mbuf, &ch, sizeof(ch)); +} + +void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len) { + struct cs_base64_ctx ctx; + cs_base64_init(&ctx, mg_mbuf_append_base64_putc, mbuf); + cs_base64_update(&ctx, (const char *) data, len); + cs_base64_finish(&ctx); +} + +void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass, + struct mbuf *buf) { + const char *header_prefix = "Authorization: Basic "; + const char *header_suffix = "\r\n"; + + struct cs_base64_ctx ctx; + cs_base64_init(&ctx, mg_mbuf_append_base64_putc, buf); + + mbuf_append(buf, header_prefix, strlen(header_prefix)); + + cs_base64_update(&ctx, user.p, user.len); + if (pass.len > 0) { + cs_base64_update(&ctx, ":", 1); + cs_base64_update(&ctx, pass.p, pass.len); + } + cs_base64_finish(&ctx); + mbuf_append(buf, header_suffix, strlen(header_suffix)); +} + +struct mg_str mg_url_encode(const struct mg_str src) { + static const char *dont_escape = "._-$,;~()/"; + static const char *hex = "0123456789abcdef"; + size_t i = 0; + struct mbuf mb; + mbuf_init(&mb, src.len); + + for (i = 0; i < src.len; i++) { + const unsigned char c = *((const unsigned char *) src.p + i); + if (isalnum(c) || strchr(dont_escape, c) != NULL) { + mbuf_append(&mb, &c, 1); + } else { + mbuf_append(&mb, "%", 1); + mbuf_append(&mb, &hex[c >> 4], 1); + mbuf_append(&mb, &hex[c & 15], 1); + } + } + mbuf_append(&mb, "", 1); + mbuf_trim(&mb); + return mg_mk_str_n(mb.buf, mb.len - 1); +} diff --git a/src/util.h b/src/util.h new file mode 100644 index 00000000..15a6ee24 --- /dev/null +++ b/src/util.h @@ -0,0 +1,208 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + */ + +/* + * === Utility API + */ + +#ifndef CS_MONGOOSE_SRC_UTIL_H_ +#define CS_MONGOOSE_SRC_UTIL_H_ + +#include + +#include "mongoose/src/common.h" +#include "mongoose/src/net_if.h" + +#ifdef __cplusplus +extern "C" { +#endif /* __cplusplus */ + +#ifndef MG_MAX_PATH +#ifdef PATH_MAX +#define MG_MAX_PATH PATH_MAX +#else +#define MG_MAX_PATH 256 +#endif +#endif + +/* + * Fetches substring from input string `s`, `end` into `v`. + * Skips initial delimiter characters. Records first non-delimiter character + * at the beginning of substring `v`. Then scans the rest of the string + * until a delimiter character or end-of-string is found. + * `delimiters` is a 0-terminated string containing delimiter characters. + * Either one of `delimiters` or `end_string` terminates the search. + * Returns an `s` pointer, advanced forward where parsing has stopped. + */ +const char *mg_skip(const char *s, const char *end_string, + const char *delimiters, struct mg_str *v); + +/* + * Decodes base64-encoded string `s`, `len` into the destination `dst`. + * The destination has to have enough space to hold the decoded buffer. + * Decoding stops either when all strings have been decoded or invalid an + * character appeared. + * Destination is '\0'-terminated. + * Returns the number of decoded characters. On success, that should be equal + * to `len`. On error (invalid character) the return value is smaller then + * `len`. + */ +int mg_base64_decode(const unsigned char *s, int len, char *dst); + +/* + * Base64-encode chunk of memory `src`, `src_len` into the destination `dst`. + * Destination has to have enough space to hold encoded buffer. + * Destination is '\0'-terminated. + */ +void mg_base64_encode(const unsigned char *src, int src_len, char *dst); + +#if MG_ENABLE_FILESYSTEM +/* + * Performs a 64-bit `stat()` call against a given file. + * + * `path` should be UTF8 encoded. + * + * Return value is the same as for `stat()` syscall. + */ +int mg_stat(const char *path, cs_stat_t *st); + +/* + * Opens the given file and returns a file stream. + * + * `path` and `mode` should be UTF8 encoded. + * + * Return value is the same as for the `fopen()` call. + */ +FILE *mg_fopen(const char *path, const char *mode); + +/* + * Opens the given file and returns a file stream. + * + * `path` should be UTF8 encoded. + * + * Return value is the same as for the `open()` syscall. + */ +int mg_open(const char *path, int flag, int mode); + +/* + * Reads data from the given file stream. + * + * Return value is a number of bytes readen. + */ +size_t mg_fread(void *ptr, size_t size, size_t count, FILE *f); + +/* + * Writes data to the given file stream. + * + * Return value is a number of bytes wtitten. + */ +size_t mg_fwrite(const void *ptr, size_t size, size_t count, FILE *f); + +#endif /* MG_ENABLE_FILESYSTEM */ + +#if MG_ENABLE_THREADS +/* + * Starts a new detached thread. + * Arguments and semantics are the same as pthead's `pthread_create()`. + * `thread_func` is a thread function, `thread_func_param` is a parameter + * that is passed to the thread function. + */ +void *mg_start_thread(void *(*thread_func)(void *), void *thread_func_param); +#endif + +void mg_set_close_on_exec(sock_t); + +#define MG_SOCK_STRINGIFY_IP 1 +#define MG_SOCK_STRINGIFY_PORT 2 +#define MG_SOCK_STRINGIFY_REMOTE 4 +/* + * Converts a connection's local or remote address into string. + * + * The `flags` parameter is a bit mask that controls the behaviour, + * see `MG_SOCK_STRINGIFY_*` definitions. + * + * - MG_SOCK_STRINGIFY_IP - print IP address + * - MG_SOCK_STRINGIFY_PORT - print port number + * - MG_SOCK_STRINGIFY_REMOTE - print remote peer's IP/port, not local address + * + * If both port number and IP address are printed, they are separated by `:`. + * If compiled with `-DMG_ENABLE_IPV6`, IPv6 addresses are supported. + * Return length of the stringified address. + */ +int mg_conn_addr_to_str(struct mg_connection *c, char *buf, size_t len, + int flags); +#if MG_NET_IF == MG_NET_IF_SOCKET +/* Legacy interface. */ +void mg_sock_to_str(sock_t sock, char *buf, size_t len, int flags); +#endif + +/* + * Convert the socket's address into string. + * + * `flags` is MG_SOCK_STRINGIFY_IP and/or MG_SOCK_STRINGIFY_PORT. + */ +int mg_sock_addr_to_str(const union socket_address *sa, char *buf, size_t len, + int flags); + +#if MG_ENABLE_HEXDUMP +/* + * Generates a human-readable hexdump of memory chunk. + * + * Takes a memory buffer `buf` of length `len` and creates a hex dump of that + * buffer in `dst`. The generated output is a-la hexdump(1). + * Returns the length of generated string, excluding terminating `\0`. If + * returned length is bigger than `dst_len`, the overflow bytes are discarded. + */ +int mg_hexdump(const void *buf, int len, char *dst, int dst_len); + +/* Same as mg_hexdump, but with output going to file instead of a buffer. */ +void mg_hexdumpf(FILE *fp, const void *buf, int len); + +/* + * Generates human-readable hexdump of the data sent or received by the + * connection. `path` is a file name where hexdump should be written. + * `num_bytes` is a number of bytes sent/received. `ev` is one of the `MG_*` + * events sent to an event handler. This function is supposed to be called from + * the event handler. + */ +void mg_hexdump_connection(struct mg_connection *nc, const char *path, + const void *buf, int num_bytes, int ev); +#endif + +/* + * Returns true if target platform is big endian. + */ +int mg_is_big_endian(void); + +/* + * Use with cs_base64_init/update/finish in order to write out base64 in chunks. + */ +void mg_mbuf_append_base64_putc(char ch, void *user_data); + +/* + * Encode `len` bytes starting at `data` as base64 and append them to an mbuf. + */ +void mg_mbuf_append_base64(struct mbuf *mbuf, const void *data, size_t len); + +/* + * Generate a Basic Auth header and appends it to buf. + * If pass is NULL, then user is expected to contain the credentials pair + * already encoded as `user:pass`. + */ +void mg_basic_auth_header(const struct mg_str user, const struct mg_str pass, + struct mbuf *buf); + +/* + * URL-escape the specified string. + * All non-printable characters are escaped, plus `._-$,;~()/`. + * Input need not be NUL-terminated, but the returned string is. + * Returned string is heap-allocated and must be free()'d. + */ +struct mg_str mg_url_encode(const struct mg_str src); + +#ifdef __cplusplus +} +#endif /* __cplusplus */ +#endif /* CS_MONGOOSE_SRC_UTIL_H_ */ diff --git a/test/Makefile b/test/Makefile new file mode 100644 index 00000000..96f463e2 --- /dev/null +++ b/test/Makefile @@ -0,0 +1,104 @@ +# vim: ts=8 noet sw=8 ai cin smarttab + +PROG = unit_test +REPO_ROOT ?= ../../ +SRC_DIR = ../src +TEST_SOURCES = unit_test.c $(REPO_ROOT)/common/test_util.c $(REPO_ROOT)/tuna/dispatcher.c +AMALGAMATED_SOURCES = ../mongoose.c +KRYPTON_PATH = $(REPO_ROOT)/krypton +# or Krypton, or mbedTLS +SSL ?= OpenSSL + +COMMON_FEATURE_FLAGS = \ + -DKR_MODULE_LINES \ + -DMG_ENABLE_COAP \ + -DMG_ENABLE_DNS_SERVER \ + -DMG_ENABLE_HTTP_SSI_EXEC \ + -DMG_ENABLE_HTTP_WEBDAV \ + -DMG_ENABLE_MQTT_BROKER \ + -DMG_ENABLE_SOCKS \ + -DMG_ENABLE_SYNC_RESOLVER \ + -DMG_ENABLE_THREADS \ + -DMG_DISABLE_DAV_AUTH \ + -DMG_INTERNAL="" \ + -DMG_MODULE_LINES \ + -DMG_CALLOC=test_calloc \ + -DMG_MALLOC=test_malloc \ + -DMG_ENABLE_POLL_UNTIL \ + -DMG_ENABLE_SNTP -DMG_SNTP_NO_DELAY_CORRECTION \ + -DMG_ENABLE_HTTP_STREAMING_MULTIPART +UNIX_FEATURE_FLAGS=-DMG_ENABLE_IPV6 -DMG_ENABLE_SSL +CFLAGS = -W -Wall -Wundef -Werror -g -O0 -Wno-multichar -D__USE_MISC \ + $(COMMON_FEATURE_FLAGS) $(UNIX_FEATURE_FLAGS) \ + -I$(REPO_ROOT) -include unit_test.h -pthread $(CFLAGS_EXTRA) + +LDFLAGS = -lm + +ifeq "$(SSL)" "OpenSSL" +# OpenSSL is the default +LDFLAGS += -lcrypto -lssl +endif +ifeq "$(SSL)" "Krypton" +CFLAGS += $(KRYPTON_PATH)/krypton.c -I$(KRYPTON_PATH) +endif +ifeq "$(SSL)" "mbedTLS" +CFLAGS += -DMG_SSL_IF=MG_SSL_IF_MBEDTLS -DMG_SSL_MBED_DUMMY_RANDOM +LDFLAGS += -lmbedcrypto -lmbedtls -lmbedx509 +endif + +include test.mk +include $(SRC_DIR)/modules.mk + +# http://crossgcc.rts-software.org/doku.php?id=compiling_for_win32 +MINGW_GCC=/usr/local/gcc-4.8.0-qt-4.8.4-for-mingw32/win32-gcc/bin/i586-mingw32-gcc + +.PHONY: $(PROG).exe $(PROG)_mingw.exe + +$(PROG)_mingw.exe: Makefile + $(MINGW_GCC) $(AMALGAMATED_SOURCES) -o $(PROG)_mingw.exe -W -Wall -Werror + +$(PROG).exe: $(AMALGAMATED_SOURCES) data/cgi/index.cgi.exe +ifndef VC6_DIR + $(error Please set VC6_DIR) +endif + Include=$(VC6_DIR)/include Lib=$(VC6_DIR)/lib \ + wine $(VC6_DIR)/bin/cl \ + $(TEST_SOURCES) $(AMALGAMATED_SOURCES) \ + /MD /I. /I.. /Zi \ + /FIunit_test.h \ + /I$(REPO_ROOT) \ + $(COMMON_FEATURE_FLAGS) $(CFLAGS_EXTRA) \ + -DMG_ENABLE_SSL $(KRYPTON_PATH)/krypton.c -I$(KRYPTON_PATH) \ + kernel32.lib advapi32.lib \ + /Fe$@ + cp data/cgi/index.cgi.exe data/cgi/index.cgi + +win: $(PROG).exe + wine $(PROG).exe $(TEST_FILTER) + +# CGI test program. + +$(PROG)-%: data/cgi/index.cgi + +data/cgi/index.cgi: index_cgi.c ccgi-1.2/ccgi.c + @echo -e "CC\tindex.cgi" + @mkdir -p data/cgi + @$(CC) -Wall -Werror -o $@ $^ -Iccgi-1.2 + +data/cgi/index.cgi.exe: index_cgi.c ccgi-1.2/ccgi.c + @echo -e "CC\tindex.cgi.exe" + @mkdir -p data/cgi + Include=$(VC6_DIR)/include Lib=$(VC6_DIR)/lib \ + wine $(VC6_DIR)/bin/cl \ + /MD /Iccgi-1.2 /Zi index_cgi.c ccgi-1.2/ccgi.c /Fe$@ + +clean: clean_index_cgi + +clean_index_cgi: + @echo -e "CLEAN\tindex.cgi" + @rm -f data/cgi/index.cgi* + +# Interactive: +# docker run -v $(CURDIR)/../..:/cesanta -t -i --entrypoint=/bin/bash cesanta/mongoose_test +docker: + docker run --rm -v $(CURDIR)/../..:/cesanta cesanta/mongoose_test diff --git a/test/ca.pem b/test/ca.pem new file mode 100644 index 00000000..88da4b5b --- /dev/null +++ b/test/ca.pem @@ -0,0 +1,49 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAv3/TSSi5hZDwMKG43eqe+GzR1lRMXVYt9I1Mr987v1DT99xR +Dcpfo/3aj6B/V/G67oPz+zbVZN/ZPvvA1Z82T7ixcBFsGIXgEWzxUm1UCUf51ftl +MlOaf24cdyegi0y8hRdkWLoC7w0vuMfrgR6cmpbI2LSDSMaXXX2qDoofQsFUYaJN +Nn3uqRK0ixs/jzbzbAT9q2BWYwySUX4VEgADpmi0FyANDjEhmdktxQW9l6IGGzF8 +M9mA053hIgZwo+9qf9X3nfNUTWMvisMQtxm0mRYgvD53Oix08VLs6bodNTVOLQoc +H0uH3CTs+H3Z0CkcZaAJe/kwCLFhls9ee3M0nQIDAQABAoIBAQCsADPWUi3QOg6C +n79cE5AVsigHSlAMxYshTIjErs0LWZ4J0mk66bpdoXTd7Fp0szojYYGS8f1ZTXXj +jFv3g7lUgZ9d+UgN/rDy9dcLIgeJDozoFZUfTthF/LC0lXMtqw7ou8n1p51a+Y0T +ev2cS9J9R1G+0uPYSgdKgcRsqsLJQS4fu5CAk9d0aeTTl009uxcn9yfTUjwOaR5J +PuNmunAEvhE/DGSkt5oNXo7t8Q2L3mYSM0MwKdDFqoQdZAV6TMTv22Mjb6SxOOnJ +r5gNK2BmM6oNPWvzY0PoI0LcLgFNDWIMqIq4mg73MdzszakaNRDlOYtLAuKbTF3Q +SDq8OkZBAoGBAOn6B5jBxSa+5GcIIeGqtiRhDMExyblt3Gk2gaw6UIZpvVDXPWWm +r0tkGJrYecmqesN7DGmmdkyx8KONF+yzYLxSsIEGNesvVYe6PXTDZYYI57As4Z4W +DFlCDt2FaKuMXxyOlUCiXg94z8IJBJ2ldCmmG34gBSvuFe6V5x4XE3crAoGBANGG +P7AWw6uygfjog6B2dFT6n+9UhpyJlqwfPi5eD9V5JXtWlH6xWi3dRfuYAIafg95I +W8/OZGHrj44gNCgYjvZHud+H3NPJPZ7lftoay5KeShBAa/pCd67OMxp1SvvONYcp +7TSwm5s+hOJvQOpw2wg0cXnfrxGKpGLOFaRddp9XAoGAFdeXefUs2G8dl1i1AQIU +utSsgiSJtlvBJblG5bMT7VhVqgRN4P1sg9c2TM5EoETf7PvBruMxS/uYgUwcnaYp +M6tser7/rZLfoyoJrqrHAXo3VsT50u4v/O0jwh5AJTOXdW0CFeSSb1NR4cVBvw3B +CFpPWrjWgsFZHsqzpqV01b0CgYEAkDft4pDowmgumlvBLlQaotuX9q6hsWHrOjKP +JG9OSswGhq0DrWj5/5PNNe5cfk2SARChUZpo8hWoTFXSUL8GuHKKeFgWIhjkt1iU +RiAne5ZEuIb/S9UweDwqZM3TfRtlMNIlGh1uHh+cbBfUAQsJWM5wRUk4QcTCfdgI +gYhrvCUCgYBB6u8Q49RjrTBxWK8bcZOjVhYNrd3xrCunFVMt2QAXGGrRaXpqUMnp +xNUmGe9vGux+s0TRguZcLEX3vX+wFyBfFKwZY9hSU7PFY/da8echpu37JasKvAov +5+5XWI5RgF+SFVk+Q7St2BlSJa/vBAH8vtrX9Dt/hN/VSo2mAPAyMQ== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIIDjjCCAnagAwIBAgIJAIOEuwkahzkOMA0GCSqGSIb3DQEBBQUAMDgxCzAJBgNV +BAMTAm5zMQswCQYDVQQKEwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkR1Ymxp +bjAeFw0xNDA4MzAxOTA3NDNaFw0yNDA4MjcxOTA3NDNaMDgxCzAJBgNVBAMTAm5z +MQswCQYDVQQKEwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkR1YmxpbjCCASIw +DQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEBAL9/00kouYWQ8DChuN3qnvhs0dZU +TF1WLfSNTK/fO79Q0/fcUQ3KX6P92o+gf1fxuu6D8/s21WTf2T77wNWfNk+4sXAR +bBiF4BFs8VJtVAlH+dX7ZTJTmn9uHHcnoItMvIUXZFi6Au8NL7jH64EenJqWyNi0 +g0jGl119qg6KH0LBVGGiTTZ97qkStIsbP48282wE/atgVmMMklF+FRIAA6ZotBcg +DQ4xIZnZLcUFvZeiBhsxfDPZgNOd4SIGcKPvan/V953zVE1jL4rDELcZtJkWILw+ +dzosdPFS7Om6HTU1Ti0KHB9Lh9wk7Ph92dApHGWgCXv5MAixYZbPXntzNJ0CAwEA +AaOBmjCBlzAdBgNVHQ4EFgQUsz/nOHpjMkV8pk9dFpy41batoTcwaAYDVR0jBGEw +X4AUsz/nOHpjMkV8pk9dFpy41batoTehPKQ6MDgxCzAJBgNVBAMTAm5zMQswCQYD +VQQKEwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkR1YmxpboIJAIOEuwkahzkO +MAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADggEBAEDOtAl7bgAXgcL3HRlV +H71tkUaok589PIqsTE4d8s8tFBZ92CyWD8ZPU46HbbyJXMFoFxiN7PvCzOBlgoZM +r80HJWZc9tSlqK0NIbIyk1aeM06+F8qB+8/vw/spIkdYzDv3avwyOrc6fFnEzbwz +5BFFrF2G9JajRKAP5snAV9iM8I2TD4l+w75MXXl7/DBEohdMBsTeDrrXj4q4sgoB +L/yLeCoK6inkMTU5DwcGbiqvNnZA+9T654qlAlKjPMObGGPphK5/QKcOnV7Qtdju +DHzDsDimdVbz9G1cxXs/AI/35GD7IDTdNTtmBhkf4/tsQ7Ua80xpIowb1fFUHmo1 +UAo= +-----END CERTIFICATE----- diff --git a/test/ccgi-1.2/.clang-format b/test/ccgi-1.2/.clang-format new file mode 100644 index 00000000..e3845288 --- /dev/null +++ b/test/ccgi-1.2/.clang-format @@ -0,0 +1 @@ +DisableFormat: true diff --git a/test/ccgi-1.2/CHANGES b/test/ccgi-1.2/CHANGES new file mode 100644 index 00000000..9765026f --- /dev/null +++ b/test/ccgi-1.2/CHANGES @@ -0,0 +1,28 @@ +Changes for C CGI Library + +1.0 + Initial Release + +1.1 + Added hexadecimal encoding: CGI_encode_hex() CGI_decode_hex() + + Added simple cryptography: CGI_encrypt() CGI_decrypt() + + Added SCGI server: CGI_prefork_server() + + Modified CGI_decode_query() to parse query strings less + strictly. + +1.2 + Switched to the GNU Lesser General Public License + + Fixed a bug when testing CONTENT_TYPE for + application/x-www-form-urlencoded. Changed strcasecmp() + to strncasecmp(). + + Changed t/test.c to terminate variable length argument + list passed to CGI_encode_query() with (char *)0. Type + cast needed for 64 bit platforms. + + Changed doc.html to document that CGI_encode_query() + argument list must end with (char *)0. diff --git a/test/ccgi-1.2/COPYING b/test/ccgi-1.2/COPYING new file mode 100644 index 00000000..4362b491 --- /dev/null +++ b/test/ccgi-1.2/COPYING @@ -0,0 +1,502 @@ + GNU LESSER GENERAL PUBLIC LICENSE + Version 2.1, February 1999 + + Copyright (C) 1991, 1999 Free Software Foundation, Inc. + 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + Everyone is permitted to copy and distribute verbatim copies + of this license document, but changing it is not allowed. + +[This is the first released version of the Lesser GPL. It also counts + as the successor of the GNU Library Public License, version 2, hence + the version number 2.1.] + + Preamble + + The licenses for most software are designed to take away your +freedom to share and change it. By contrast, the GNU General Public +Licenses are intended to guarantee your freedom to share and change +free software--to make sure the software is free for all its users. + + This license, the Lesser General Public License, applies to some +specially designated software packages--typically libraries--of the +Free Software Foundation and other authors who decide to use it. You +can use it too, but we suggest you first think carefully about whether +this license or the ordinary General Public License is the better +strategy to use in any particular case, based on the explanations below. + + When we speak of free software, we are referring to freedom of use, +not price. Our General Public Licenses are designed to make sure that +you have the freedom to distribute copies of free software (and charge +for this service if you wish); that you receive source code or can get +it if you want it; that you can change the software and use pieces of +it in new free programs; and that you are informed that you can do +these things. + + To protect your rights, we need to make restrictions that forbid +distributors to deny you these rights or to ask you to surrender these +rights. These restrictions translate to certain responsibilities for +you if you distribute copies of the library or if you modify it. + + For example, if you distribute copies of the library, whether gratis +or for a fee, you must give the recipients all the rights that we gave +you. You must make sure that they, too, receive or can get the source +code. If you link other code with the library, you must provide +complete object files to the recipients, so that they can relink them +with the library after making changes to the library and recompiling +it. And you must show them these terms so they know their rights. + + We protect your rights with a two-step method: (1) we copyright the +library, and (2) we offer you this license, which gives you legal +permission to copy, distribute and/or modify the library. + + To protect each distributor, we want to make it very clear that +there is no warranty for the free library. Also, if the library is +modified by someone else and passed on, the recipients should know +that what they have is not the original version, so that the original +author's reputation will not be affected by problems that might be +introduced by others. + + Finally, software patents pose a constant threat to the existence of +any free program. We wish to make sure that a company cannot +effectively restrict the users of a free program by obtaining a +restrictive license from a patent holder. Therefore, we insist that +any patent license obtained for a version of the library must be +consistent with the full freedom of use specified in this license. + + Most GNU software, including some libraries, is covered by the +ordinary GNU General Public License. This license, the GNU Lesser +General Public License, applies to certain designated libraries, and +is quite different from the ordinary General Public License. We use +this license for certain libraries in order to permit linking those +libraries into non-free programs. + + When a program is linked with a library, whether statically or using +a shared library, the combination of the two is legally speaking a +combined work, a derivative of the original library. The ordinary +General Public License therefore permits such linking only if the +entire combination fits its criteria of freedom. The Lesser General +Public License permits more lax criteria for linking other code with +the library. + + We call this license the "Lesser" General Public License because it +does Less to protect the user's freedom than the ordinary General +Public License. It also provides other free software developers Less +of an advantage over competing non-free programs. These disadvantages +are the reason we use the ordinary General Public License for many +libraries. However, the Lesser license provides advantages in certain +special circumstances. + + For example, on rare occasions, there may be a special need to +encourage the widest possible use of a certain library, so that it becomes +a de-facto standard. To achieve this, non-free programs must be +allowed to use the library. A more frequent case is that a free +library does the same job as widely used non-free libraries. In this +case, there is little to gain by limiting the free library to free +software only, so we use the Lesser General Public License. + + In other cases, permission to use a particular library in non-free +programs enables a greater number of people to use a large body of +free software. For example, permission to use the GNU C Library in +non-free programs enables many more people to use the whole GNU +operating system, as well as its variant, the GNU/Linux operating +system. + + Although the Lesser General Public License is Less protective of the +users' freedom, it does ensure that the user of a program that is +linked with the Library has the freedom and the wherewithal to run +that program using a modified version of the Library. + + The precise terms and conditions for copying, distribution and +modification follow. Pay close attention to the difference between a +"work based on the library" and a "work that uses the library". The +former contains code derived from the library, whereas the latter must +be combined with the library in order to run. + + GNU LESSER GENERAL PUBLIC LICENSE + TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION + + 0. This License Agreement applies to any software library or other +program which contains a notice placed by the copyright holder or +other authorized party saying it may be distributed under the terms of +this Lesser General Public License (also called "this License"). +Each licensee is addressed as "you". + + A "library" means a collection of software functions and/or data +prepared so as to be conveniently linked with application programs +(which use some of those functions and data) to form executables. + + The "Library", below, refers to any such software library or work +which has been distributed under these terms. A "work based on the +Library" means either the Library or any derivative work under +copyright law: that is to say, a work containing the Library or a +portion of it, either verbatim or with modifications and/or translated +straightforwardly into another language. (Hereinafter, translation is +included without limitation in the term "modification".) + + "Source code" for a work means the preferred form of the work for +making modifications to it. For a library, complete source code means +all the source code for all modules it contains, plus any associated +interface definition files, plus the scripts used to control compilation +and installation of the library. + + Activities other than copying, distribution and modification are not +covered by this License; they are outside its scope. The act of +running a program using the Library is not restricted, and output from +such a program is covered only if its contents constitute a work based +on the Library (independent of the use of the Library in a tool for +writing it). Whether that is true depends on what the Library does +and what the program that uses the Library does. + + 1. You may copy and distribute verbatim copies of the Library's +complete source code as you receive it, in any medium, provided that +you conspicuously and appropriately publish on each copy an +appropriate copyright notice and disclaimer of warranty; keep intact +all the notices that refer to this License and to the absence of any +warranty; and distribute a copy of this License along with the +Library. + + You may charge a fee for the physical act of transferring a copy, +and you may at your option offer warranty protection in exchange for a +fee. + + 2. You may modify your copy or copies of the Library or any portion +of it, thus forming a work based on the Library, and copy and +distribute such modifications or work under the terms of Section 1 +above, provided that you also meet all of these conditions: + + a) The modified work must itself be a software library. + + b) You must cause the files modified to carry prominent notices + stating that you changed the files and the date of any change. + + c) You must cause the whole of the work to be licensed at no + charge to all third parties under the terms of this License. + + d) If a facility in the modified Library refers to a function or a + table of data to be supplied by an application program that uses + the facility, other than as an argument passed when the facility + is invoked, then you must make a good faith effort to ensure that, + in the event an application does not supply such function or + table, the facility still operates, and performs whatever part of + its purpose remains meaningful. + + (For example, a function in a library to compute square roots has + a purpose that is entirely well-defined independent of the + application. Therefore, Subsection 2d requires that any + application-supplied function or table used by this function must + be optional: if the application does not supply it, the square + root function must still compute square roots.) + +These requirements apply to the modified work as a whole. If +identifiable sections of that work are not derived from the Library, +and can be reasonably considered independent and separate works in +themselves, then this License, and its terms, do not apply to those +sections when you distribute them as separate works. But when you +distribute the same sections as part of a whole which is a work based +on the Library, the distribution of the whole must be on the terms of +this License, whose permissions for other licensees extend to the +entire whole, and thus to each and every part regardless of who wrote +it. + +Thus, it is not the intent of this section to claim rights or contest +your rights to work written entirely by you; rather, the intent is to +exercise the right to control the distribution of derivative or +collective works based on the Library. + +In addition, mere aggregation of another work not based on the Library +with the Library (or with a work based on the Library) on a volume of +a storage or distribution medium does not bring the other work under +the scope of this License. + + 3. You may opt to apply the terms of the ordinary GNU General Public +License instead of this License to a given copy of the Library. To do +this, you must alter all the notices that refer to this License, so +that they refer to the ordinary GNU General Public License, version 2, +instead of to this License. (If a newer version than version 2 of the +ordinary GNU General Public License has appeared, then you can specify +that version instead if you wish.) Do not make any other change in +these notices. + + Once this change is made in a given copy, it is irreversible for +that copy, so the ordinary GNU General Public License applies to all +subsequent copies and derivative works made from that copy. + + This option is useful when you wish to copy part of the code of +the Library into a program that is not a library. + + 4. You may copy and distribute the Library (or a portion or +derivative of it, under Section 2) in object code or executable form +under the terms of Sections 1 and 2 above provided that you accompany +it with the complete corresponding machine-readable source code, which +must be distributed under the terms of Sections 1 and 2 above on a +medium customarily used for software interchange. + + If distribution of object code is made by offering access to copy +from a designated place, then offering equivalent access to copy the +source code from the same place satisfies the requirement to +distribute the source code, even though third parties are not +compelled to copy the source along with the object code. + + 5. A program that contains no derivative of any portion of the +Library, but is designed to work with the Library by being compiled or +linked with it, is called a "work that uses the Library". Such a +work, in isolation, is not a derivative work of the Library, and +therefore falls outside the scope of this License. + + However, linking a "work that uses the Library" with the Library +creates an executable that is a derivative of the Library (because it +contains portions of the Library), rather than a "work that uses the +library". The executable is therefore covered by this License. +Section 6 states terms for distribution of such executables. + + When a "work that uses the Library" uses material from a header file +that is part of the Library, the object code for the work may be a +derivative work of the Library even though the source code is not. +Whether this is true is especially significant if the work can be +linked without the Library, or if the work is itself a library. The +threshold for this to be true is not precisely defined by law. + + If such an object file uses only numerical parameters, data +structure layouts and accessors, and small macros and small inline +functions (ten lines or less in length), then the use of the object +file is unrestricted, regardless of whether it is legally a derivative +work. (Executables containing this object code plus portions of the +Library will still fall under Section 6.) + + Otherwise, if the work is a derivative of the Library, you may +distribute the object code for the work under the terms of Section 6. +Any executables containing that work also fall under Section 6, +whether or not they are linked directly with the Library itself. + + 6. As an exception to the Sections above, you may also combine or +link a "work that uses the Library" with the Library to produce a +work containing portions of the Library, and distribute that work +under terms of your choice, provided that the terms permit +modification of the work for the customer's own use and reverse +engineering for debugging such modifications. + + You must give prominent notice with each copy of the work that the +Library is used in it and that the Library and its use are covered by +this License. You must supply a copy of this License. If the work +during execution displays copyright notices, you must include the +copyright notice for the Library among them, as well as a reference +directing the user to the copy of this License. Also, you must do one +of these things: + + a) Accompany the work with the complete corresponding + machine-readable source code for the Library including whatever + changes were used in the work (which must be distributed under + Sections 1 and 2 above); and, if the work is an executable linked + with the Library, with the complete machine-readable "work that + uses the Library", as object code and/or source code, so that the + user can modify the Library and then relink to produce a modified + executable containing the modified Library. (It is understood + that the user who changes the contents of definitions files in the + Library will not necessarily be able to recompile the application + to use the modified definitions.) + + b) Use a suitable shared library mechanism for linking with the + Library. A suitable mechanism is one that (1) uses at run time a + copy of the library already present on the user's computer system, + rather than copying library functions into the executable, and (2) + will operate properly with a modified version of the library, if + the user installs one, as long as the modified version is + interface-compatible with the version that the work was made with. + + c) Accompany the work with a written offer, valid for at + least three years, to give the same user the materials + specified in Subsection 6a, above, for a charge no more + than the cost of performing this distribution. + + d) If distribution of the work is made by offering access to copy + from a designated place, offer equivalent access to copy the above + specified materials from the same place. + + e) Verify that the user has already received a copy of these + materials or that you have already sent this user a copy. + + For an executable, the required form of the "work that uses the +Library" must include any data and utility programs needed for +reproducing the executable from it. However, as a special exception, +the materials to be distributed need not include anything that is +normally distributed (in either source or binary form) with the major +components (compiler, kernel, and so on) of the operating system on +which the executable runs, unless that component itself accompanies +the executable. + + It may happen that this requirement contradicts the license +restrictions of other proprietary libraries that do not normally +accompany the operating system. Such a contradiction means you cannot +use both them and the Library together in an executable that you +distribute. + + 7. You may place library facilities that are a work based on the +Library side-by-side in a single library together with other library +facilities not covered by this License, and distribute such a combined +library, provided that the separate distribution of the work based on +the Library and of the other library facilities is otherwise +permitted, and provided that you do these two things: + + a) Accompany the combined library with a copy of the same work + based on the Library, uncombined with any other library + facilities. This must be distributed under the terms of the + Sections above. + + b) Give prominent notice with the combined library of the fact + that part of it is a work based on the Library, and explaining + where to find the accompanying uncombined form of the same work. + + 8. You may not copy, modify, sublicense, link with, or distribute +the Library except as expressly provided under this License. Any +attempt otherwise to copy, modify, sublicense, link with, or +distribute the Library is void, and will automatically terminate your +rights under this License. However, parties who have received copies, +or rights, from you under this License will not have their licenses +terminated so long as such parties remain in full compliance. + + 9. You are not required to accept this License, since you have not +signed it. However, nothing else grants you permission to modify or +distribute the Library or its derivative works. These actions are +prohibited by law if you do not accept this License. Therefore, by +modifying or distributing the Library (or any work based on the +Library), you indicate your acceptance of this License to do so, and +all its terms and conditions for copying, distributing or modifying +the Library or works based on it. + + 10. Each time you redistribute the Library (or any work based on the +Library), the recipient automatically receives a license from the +original licensor to copy, distribute, link with or modify the Library +subject to these terms and conditions. You may not impose any further +restrictions on the recipients' exercise of the rights granted herein. +You are not responsible for enforcing compliance by third parties with +this License. + + 11. If, as a consequence of a court judgment or allegation of patent +infringement or for any other reason (not limited to patent issues), +conditions are imposed on you (whether by court order, agreement or +otherwise) that contradict the conditions of this License, they do not +excuse you from the conditions of this License. If you cannot +distribute so as to satisfy simultaneously your obligations under this +License and any other pertinent obligations, then as a consequence you +may not distribute the Library at all. For example, if a patent +license would not permit royalty-free redistribution of the Library by +all those who receive copies directly or indirectly through you, then +the only way you could satisfy both it and this License would be to +refrain entirely from distribution of the Library. + +If any portion of this section is held invalid or unenforceable under any +particular circumstance, the balance of the section is intended to apply, +and the section as a whole is intended to apply in other circumstances. + +It is not the purpose of this section to induce you to infringe any +patents or other property right claims or to contest validity of any +such claims; this section has the sole purpose of protecting the +integrity of the free software distribution system which is +implemented by public license practices. Many people have made +generous contributions to the wide range of software distributed +through that system in reliance on consistent application of that +system; it is up to the author/donor to decide if he or she is willing +to distribute software through any other system and a licensee cannot +impose that choice. + +This section is intended to make thoroughly clear what is believed to +be a consequence of the rest of this License. + + 12. If the distribution and/or use of the Library is restricted in +certain countries either by patents or by copyrighted interfaces, the +original copyright holder who places the Library under this License may add +an explicit geographical distribution limitation excluding those countries, +so that distribution is permitted only in or among countries not thus +excluded. In such case, this License incorporates the limitation as if +written in the body of this License. + + 13. The Free Software Foundation may publish revised and/or new +versions of the Lesser General Public License from time to time. +Such new versions will be similar in spirit to the present version, +but may differ in detail to address new problems or concerns. + +Each version is given a distinguishing version number. If the Library +specifies a version number of this License which applies to it and +"any later version", you have the option of following the terms and +conditions either of that version or of any later version published by +the Free Software Foundation. If the Library does not specify a +license version number, you may choose any version ever published by +the Free Software Foundation. + + 14. If you wish to incorporate parts of the Library into other free +programs whose distribution conditions are incompatible with these, +write to the author to ask for permission. For software which is +copyrighted by the Free Software Foundation, write to the Free +Software Foundation; we sometimes make exceptions for this. Our +decision will be guided by the two goals of preserving the free status +of all derivatives of our free software and of promoting the sharing +and reuse of software generally. + + NO WARRANTY + + 15. BECAUSE THE LIBRARY IS LICENSED FREE OF CHARGE, THERE IS NO +WARRANTY FOR THE LIBRARY, TO THE EXTENT PERMITTED BY APPLICABLE LAW. +EXCEPT WHEN OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR +OTHER PARTIES PROVIDE THE LIBRARY "AS IS" WITHOUT WARRANTY OF ANY +KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR +PURPOSE. THE ENTIRE RISK AS TO THE QUALITY AND PERFORMANCE OF THE +LIBRARY IS WITH YOU. SHOULD THE LIBRARY PROVE DEFECTIVE, YOU ASSUME +THE COST OF ALL NECESSARY SERVICING, REPAIR OR CORRECTION. + + 16. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN +WRITING WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY +AND/OR REDISTRIBUTE THE LIBRARY AS PERMITTED ABOVE, BE LIABLE TO YOU +FOR DAMAGES, INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR +CONSEQUENTIAL DAMAGES ARISING OUT OF THE USE OR INABILITY TO USE THE +LIBRARY (INCLUDING BUT NOT LIMITED TO LOSS OF DATA OR DATA BEING +RENDERED INACCURATE OR LOSSES SUSTAINED BY YOU OR THIRD PARTIES OR A +FAILURE OF THE LIBRARY TO OPERATE WITH ANY OTHER SOFTWARE), EVEN IF +SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH +DAMAGES. + + END OF TERMS AND CONDITIONS + + How to Apply These Terms to Your New Libraries + + If you develop a new library, and you want it to be of the greatest +possible use to the public, we recommend making it free software that +everyone can redistribute and change. You can do so by permitting +redistribution under these terms (or, alternatively, under the terms of the +ordinary General Public License). + + To apply these terms, attach the following notices to the library. It is +safest to attach them to the start of each source file to most effectively +convey the exclusion of warranty; and each file should have at least the +"copyright" line and a pointer to where the full notice is found. + + + Copyright (C) + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library 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 + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with this library; if not, write to the Free Software + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +Also add information on how to contact you by electronic and paper mail. + +You should also get your employer (if you work as a programmer) or your +school, if any, to sign a "copyright disclaimer" for the library, if +necessary. Here is a sample; alter the names: + + Yoyodyne, Inc., hereby disclaims all copyright interest in the + library `Frob' (a library for tweaking knobs) written by James Random Hacker. + + , 1 April 1990 + Ty Coon, President of Vice + +That's all there is to it! diff --git a/test/ccgi-1.2/Makefile b/test/ccgi-1.2/Makefile new file mode 100644 index 00000000..f9d9ed42 --- /dev/null +++ b/test/ccgi-1.2/Makefile @@ -0,0 +1,27 @@ +# Set OPENSSL_INCLUDE to the directory that contains the +# "openssl" include directory. + +OPENSSL_INCLUDE = /usr/include + +# To build without openssl comment out the "CRYPT =" line + +CRYPT = crypt.o + +CFLAGS = -I . -I $(OPENSSL_INCLUDE) + +libccgi.a: ccgi.o prefork.o $(CRYPT) + ar r libccgi.a ccgi.o prefork.o $(CRYPT) + ranlib libccgi.a + +ccgi.o: ccgi.c ccgi.h + +prefork.o: prefork.c + +crypt.o: crypt.c ccgi.h + +test: libccgi.a + cd t; make + +clean: + rm -f *.o *.a */*.o + rm -f t/test examples/dump.cgi diff --git a/test/ccgi-1.2/README b/test/ccgi-1.2/README new file mode 100644 index 00000000..5b17fb00 --- /dev/null +++ b/test/ccgi-1.2/README @@ -0,0 +1,11 @@ +You may need to edit Makefile and t/Makefile to specify where +your openssl include files and libraries are located. To build +without openssl see the comments in Makefile. + +To build: make + +To test: make test (requires openssl) + +To install: + copy libccgi.a to a lib directory such as /usr/local/lib + copy ccgi.h to an include directory such as /usr/local/include diff --git a/test/ccgi-1.2/ccgi.c b/test/ccgi-1.2/ccgi.c new file mode 100644 index 00000000..bb8c8dac --- /dev/null +++ b/test/ccgi-1.2/ccgi.c @@ -0,0 +1,1329 @@ +/* + * C CGI Library version 1.2 + * + * Author: Stephen C. Losen, University of Virginia + * + * Copyright 2015 Stephen C. Losen. Distributed under the terms + * of the GNU Lesser General Public License (LGPL 2.1) + * + * C CGI is a C language library for parsing, decoding, storing and + * retrieving data passed from a web browser to a CGI program. C CGI + * supports URL encoded form data (application/x-www-form-urlencoded) + * and multipart (mutlipart/form-data), including file uploads. HTML + * cookies can also be stored and retrieved. + * + * The library builds one or more "variable lists" of type + * CGI_varlist. A list entry consists of a name (null terminated + * string) and one or more values (also null terminated strings) + * associated with the name. We allow multiple values because 1) + * some HTML form elements (such as checkboxes and selections) + * allow the form user to select multiple values and 2) different + * form elements may be given the same name. + * + * The library function CGI_lookup_all() searches a variable list for + * an entry with the specified name and returns the values in a + * null terminated array of pointers to null terminated strings. + * + * Variable list entries can be obtained iteratively with + * CGI_first_name(), CGI_next_name() and CGI_lookup_all(). + * + * For a file upload, the user provides a filename template that + * is passed to mkstemp() to create a new file to hold the data. + */ + +#include + +#ifndef _WIN32 +#include +#endif + +#include +#include +#include +#include +#include + +/* CGI_val is an entry in a list of variable values */ + +typedef struct CGI_val CGI_val; + +struct CGI_val { + CGI_val *next; /* next entry on list */ + const char value[1]; /* variable value */ +}; + +/* + * CGI_varlist is an entry in a list of variables. The fields + * "iter" and "tail" are only used in the first list entry. + */ + +struct CGI_varlist { + CGI_varlist *next; /* next entry on list */ + CGI_varlist *tail; /* last entry on list */ + CGI_varlist *iter; /* list iteration pointer */ + int numvalue; /* number of values */ + CGI_val *value; /* linked list of values */ + CGI_val *valtail; /* last value on list */ + CGI_value *vector; /* array of values */ + const char varname[1]; /* variable name */ +}; + +/* strbuf is a string buffer of arbitrary size */ + +typedef struct { + int size; + char str[1]; +} +strbuf; + +/* mymalloc() is a malloc() wrapper that exits on failure */ + +static void * +mymalloc(int size) { + void *ret = malloc(size); + if (ret == 0) { + fputs("C CGI Library out of memory\n", stderr); + exit(1); + } + return ret; +} + +/* + * sb_get() creates or extends a strbuf + */ + +static strbuf * +sb_get(strbuf *sb, int len) { + int size; + for (size = 128; size < len; size += size) + ; + if (sb == 0) { + sb = (strbuf *) mymalloc(sizeof(*sb) + size); + } + else { + sb = (strbuf *) realloc(sb, sizeof(*sb) + size); + if (sb == 0) { + fputs("C CGI Library out of memory\n", stderr); + exit(1); + } + } + sb->size = size; + return sb; +} + +/* savechar() saves a character in a strbuf at index idx */ + +static strbuf * +savechar(strbuf *sb, int idx, int c) { + if (sb == 0 || idx >= sb->size) { + sb = sb_get(sb, idx + 1); + } + sb->str[idx] = c; + return sb; +} + +/* savestr() saves a string in a strbuf */ + +static strbuf * +savestr(strbuf *sb, const char *str) { + int len = strlen(str); + if (sb == 0 || len >= sb->size) { + sb = sb_get(sb, len + 1); + } + strcpy(sb->str, str); + return sb; +} + +/* + * findvar() searches variable list "v" for an entry whose name + * is "varname" and returns a pointer to the entry or else null + * if not found. If varname is null then we return v->iter, + * which is the "current" variable entry. + */ + +static CGI_varlist * +findvar(CGI_varlist *v, const char *varname) { + if (varname == 0 && v != 0) { + return v->iter; + } + for (; v != 0; v = v->next) { + if (v->varname[0] == varname[0] && + strcmp(v->varname, varname) == 0) + { + break; + } + } + return v; +} + +/* + * hex() returns the numeric value of hexadecimal digit "digit" + * or returns -1 if "digit" is not a hexadecimal digit. + */ + +static int +hex(int digit) { + switch(digit) { + + case '0': case '1': case '2': case '3': case '4': + case '5': case '6': case '7': case '8': case '9': + return digit - '0'; + + case 'A': case 'B': case 'C': case 'D': case 'E': case 'F': + return 10 + digit - 'A'; + + case 'a': case 'b': case 'c': case 'd': case 'e': case 'f': + return 10 + digit - 'a'; + + default: + return -1; + } +} + +/* urlcount() returns the number of bytes needed to URL encode a string */ + +static int +urlcount(const char *p, const char *keep) { + int k; + for (k = 0; *p != 0; p++) { + if (isalnum(*p) || *p == ' ' || + (keep != 0 && strchr(keep, *p) != 0)) + { + k++; + } + else { + k += 3; + } + } + return k; +} + +/* + * urlencode() URL encodes string "in" into string "out" and + * returns a pointer to the null byte at the end of "out" + */ + +static char * +urlencode(const char *in, char *out, const char *keep) { + const char hexdigit[] = "0123456789ABCDEF"; + + for (; *in != 0; in++) { + if (isalnum(*in) || + (keep != 0 && strchr(keep, *in) != 0)) + { + *out++ = *in; + } + else if (*in == ' ') { + *out++ = '+'; + } + else { + *out++ = '%'; + *out++ = hexdigit[(*in >> 4) & 0xf]; + *out++ = hexdigit[*in & 0xf]; + } + } + *out = 0; + return out; +} + +/* scanspaces() scans over spaces and tabs in a string */ + +static char * +scanspaces(char *p) { + while (*p == ' ' || *p == '\t') { + p++; + } + return p; +} + +/* + * scanattr() scans for an attribute such as name="value" or + * name=value; saving the attribute name in attr[0] and saving + * the attribute value in attr[1]. If successful, we return a + * pointer to where the scan ended, otherwise we return null. + * The input string is modified. + */ + +static char * +scanattr(char *p, char *attr[2]) { + int quote = 0; + + attr[0] = p = scanspaces(p); + while (*p != '=' && *p != 0) { + p++; + } + if (*p != '=' || p == attr[0]) { + return 0; + } + *p++ = 0; + if (*p == '"' || *p == '\'' || *p == '`') { + quote = *p++; + } + attr[1] = p; + if (quote != 0) { + while(*p != quote && *p != 0) { + p++; + } + if (*p != quote) { + return 0; + } + *p++ = 0; + if (*p == ';') { + p++; + } + } + else { + while (*p != ';' && *p != ' ' && *p != '\t' && + *p != '\r' && *p != '\n' && *p != 0) + { + p++; + } + if (*p != 0) { + *p++ = 0; + } + } + return p; +} + +/* + * scanheader() scans a line of input for a header name followed by + * a colon and the header value, such as: + * + * Content-Disposition: form-data; + * + * We place the header name in header[0] and the value in header[1]. + * If successful we return a pointer to the character where the scan + * ended, otherwise we return null. The input string is modified. + */ + +static char * +scanheader(char *p, char *header[2]) { + if (isalnum(*p) == 0) { + return 0; + } + header[0] = p; + while (*p != ':' && *p != 0) { + p++; + } + if (*p != ':') { + return 0; + } + *p++ = 0; + header[1] = p = scanspaces(p); + while (*p != ';' && *p != '\r' && *p != '\n' && *p != 0) { + p++; + } + if (*p != 0) { + *p++ = 0; + } + return p; +} + +/* + * readline() reads characters from open file "in" into strbuf "line". + * We stop reading when we encounter '\n' or end of file. We null + * terminate "line" and return it. We return null if we encounter + * end of file without reading any characters. + */ + +static strbuf * +readline(strbuf *line, FILE *in) { + int c, i = 0; + + while ((c = getc(in)) != EOF) { + line = savechar(line, i++, c); + if (c == '\n') { + break; + } + } + if (i == 0) { + if (line != 0) { + free(line); + } + return 0; + } + return savechar(line, i, 0); /* null terminate */ +} + +/* + * copyvalue() reads bytes from open file "in", which contains + * "multipart/form-data", delimited by "boundary". We read + * bytes until we encounter the boundary. If the flag "wantfile" + * is true then we write the bytes (minus the boundary) to open + * file pointer "out" (or discard the output if "out" is NULL). + * Otherwise we copy the bytes to "value". + */ + +static strbuf * +copyvalue(const char *boundary, FILE *in, const int wantfile, + strbuf *value, FILE *out) +{ + int c, i, k, matched; + + matched = k = 0; + + while ((c = getc(in)) != EOF) { + + /* + * If we partially match the boundary, then we copy the + * entire matching prefix to the output. We do not need to + * backtrack and look for shorter matching prefixes because + * they cannot exist. The boundary always begins with '\r' + * and never contains another '\r'. + */ + + if (matched > 0 && c != boundary[matched]) { + for (i = 0; i < matched; i++) { + if (wantfile == 0) { + value = savechar(value, k++, boundary[i]); + } + else if (out != 0) { + fputc(boundary[i], out); + } + } + matched = 0; + } + + /* check for full or partial boundary match */ + + if (c == boundary[matched]) { + if (boundary[++matched] == 0) { + break; /* full match */ + } + continue; /* partial match */ + } + + /* no match, so copy byte to output */ + + if (wantfile == 0) { + value = savechar(value, k++, c); + } + else if (out != 0) { + fputc(c, out); + } + } + if (wantfile == 0) { + return savechar(value, k, 0); + } + return 0; +} + +/* + * read_multipart() reads form data encoded as "multipart/form-data" + * and adds form field names and values to variable list "v" and + * returns "v". + * + * The input is split into parts delimited by a boundary string. + * Each part starts with this header: + * + * Content-Disposition: form-data; name="fieldname"; filename="filename" + * + * where "fieldname" is the name of the form field. The "filename=" + * attribute is only present when this part contains file data for + * a file upload. A "Content-type:" header line may also be present + * with a file upload. After the header lines comes a blank line + * followed by the field data (or file data) terminated by "\r\n--" + * followed by the boundary string. If the boundary string is + * followed by "--\r\n" then this is the last part. + */ + +static CGI_varlist * +read_multipart(CGI_varlist *v, const char *template) { + const char *ctype, *name, *filename; + char *p, *token[2], *boundary, *localname = 0; + strbuf *bbuf = 0, *nbuf = 0, *fbuf = 0; + strbuf *line = 0, *value = 0; + int len, fd; + FILE *out; + + /* + * get the boundary string from the environment and prepend + * "\r\n--" to it. + */ + + if ((ctype = getenv("CONTENT_TYPE")) == 0 || + strncasecmp(ctype, "multipart/form-data;", len = 20) != 0) + { + return v; + } + bbuf = savestr(bbuf, ctype + len); + if (scanattr(bbuf->str, token) == 0 || + strcasecmp(token[0], "boundary") != 0) + { + goto cleanup; + } + boundary = token[1] - 4; + memcpy(boundary, "\r\n--", 4); + + /* + * first line is the boundary string, but with "\r\n" + * at the end rather than the start. + */ + + len = strlen(boundary) - 2; + if ((line = readline(line, stdin)) == 0 || + strncmp(line->str, boundary + 2, len) != 0 || + line->str[len] != '\r' || line->str[len + 1] != '\n') + { + goto cleanup; + } + + /* read all the parts */ + + for (;;) { + + /* Scan header lines for the Content-Disposition: header */ + + name = filename = 0; + while ((line = readline(line, stdin)) != 0 && + (p = scanheader(line->str, token)) != 0) + { + if (strcasecmp(token[0], "Content-Disposition") != 0 || + strcasecmp(token[1], "form-data") != 0) + { + continue; + } + + /* Content-Disposition: has field name and file name */ + + while ((p = scanattr(p, token)) != 0) { + if (name == 0 && + strcasecmp(token[0], "name") == 0) + { + nbuf = savestr(nbuf, token[1]); + name = nbuf->str; + } + else if (filename == 0 && + strcasecmp(token[0], "filename") == 0) + { + fbuf = savestr(fbuf, token[1]); + filename = fbuf->str; + } + } + } + + /* after the headers is a blank line (just "\r\n") */ + + if (line == 0 || name == 0 || + line->str[0] != '\r' || line->str[1] != '\n') + { + break; + } + + /* + * If filename is non null (file upload) then we read file data, + * otherwise we read field data. In either case the data + * consists of everything up to, but not including the boundary. + */ + + if (filename != 0) { + + /* copy file data to newly created file */ + + out = 0; + if (template != 0 && *filename != 0) { + if (localname == 0) { + localname = (char *) mymalloc(strlen(template) + 1); + } + strcpy(localname, template); + if ((fd = mkstemp(localname)) >= 0) { + out = fdopen(fd, "wb"); + } + } + copyvalue(boundary, stdin, 1, 0, out); + if (out != 0) { + fclose(out); + v = CGI_add_var(v, name, localname); + v = CGI_add_var(v, name, filename); + } + } + else { + value = copyvalue(boundary, stdin, 0, value, 0); + v = CGI_add_var(v, name, value->str); + } + + /* + * read the rest of the line after the boundary. If we + * get "--\r\n" then this is the last field. Otherwise + * we presumably get "\r\n" and we continue. + */ + + if ((line = readline(line, stdin)) != 0 && + line->str[0] == '-' && line->str[1] == '-' && + line->str[2] == '\r' && line->str[3] == '\n') + { + break; + } + } + +cleanup: + if (bbuf != 0) { + free(bbuf); + } + if (nbuf != 0) { + free(nbuf); + } + if (fbuf != 0) { + free(fbuf); + } + if (line != 0) { + free(line); + } + if (value != 0) { + free(value); + } + if (localname != 0) { + free(localname); + } + return v; +} + +/* + * EXPORTED FUNCTIONS + * + * CGI_decode_url() returns a new string which is a copy of the input + * string with '+' converted to ' ' and %xx converted to the character + * whose hex numeric value is xx. + */ + +char * +CGI_decode_url(const char *p) { + char *out; + int i, k, L, R; + + if (p == 0) { + return 0; + } + out = (char *) mymalloc(strlen(p) + 1); + for (i = k = 0; p[i] != 0; i++) { + switch(p[i]) { + + case '+': + out[k++] = ' '; + continue; + + case '%': + if ((L = hex(p[i + 1])) >= 0 && + (R = hex(p[i + 2])) >= 0) + { + out[k++] = (L << 4) + R; + i += 2; + continue; + } + break; + } + out[k++] = p[i]; + } + out[k] = 0; + return out; +} + +/* + * CGI_encode_url() URL encodes a string and returns the result + * in memory from malloc(). + */ + +char * +CGI_encode_url(const char *p, const char *keep) { + char *out; + + if (p == 0) { + return 0; + } + out = mymalloc(urlcount(p, keep) + 1); + urlencode(p, out, keep); + return out; +} + +/* + * CGI_encode_query() takes a variable arg list of strings + * and encodes them into a URL query string of the form + * name1=value1&name2=value2 ... where each name and value + * is URL encoded. + */ + +char * +CGI_encode_query(const char *keep, ...) { + char *out, *p; + va_list ap; + const char *name, *value; + int k; + + /* calculate the size of the output string */ + + va_start(ap, keep); + k = 0; + while ((value = va_arg(ap, const char *)) != 0) { + k += urlcount(value, keep) + 1; + } + va_end(ap); + if (k == 0) { + return 0; + } + p = out = mymalloc(k); + + /* url encode each name=value pair */ + + va_start(ap, keep); + while ((name = va_arg(ap, const char *)) != 0 && + (value = va_arg(ap, const char *)) != 0) + { + if (p != out) { + *p++ = '&'; + } + p = urlencode(name, p, keep); + *p++ = '='; + p = urlencode(value, p, keep); + } + va_end(ap); + *p = 0; + return out; +} + +/* + * CGI_encode_varlist() encodes a CGI_varlist into a query + * string of the form name1=value1&name2=value2 ... where + * each name and value is URL encoded. + */ + +char * +CGI_encode_varlist(CGI_varlist *vlist, const char *keep) { + char *out, *p; + CGI_varlist *v; + CGI_val *value; + int k = 0; + + /* calculate size of the output string */ + + for (v = vlist; v != 0; v = v->next) { + for (value = v->value; value != 0; value = value->next) { + k += 2 + urlcount(v->varname, keep) + + urlcount(value->value, keep); + } + } + if (k == 0) { + return 0; + } + p = out = mymalloc(k); + + /* URL encode each name=value pair */ + + for (v = vlist; v != 0; v = v->next) { + for (value = v->value; value != 0; value = value->next) { + if (p != out) { + *p++ = '&'; + } + p = urlencode(v->varname, p, keep); + *p++ = '='; + p = urlencode(value->value, p, keep); + } + } + *p = 0; + return out; +} + +/* + * CGI_add_var() adds a new variable name and value to variable list + * "v" and returns the resulting list. If "v" is null or if the + * variable name is not on the list, then we create a new entry. + * We add the value to the appropriate list entry. + */ + +CGI_varlist * +CGI_add_var(CGI_varlist *v, const char *varname, const char *value) { + CGI_val *val; + CGI_varlist *v2; + + if (varname == 0 || value == 0) { + return v; + } + + /* create a new value */ + + val = (CGI_val *) mymalloc(sizeof(*val) + strlen(value)); + strcpy((char *) val->value, value); + val->next = 0; + + /* + * find the list entry or else create a new one. Add the + * new value. We use "tail" pointers to keep the lists + * in the same order as the input. + */ + + if ((v2 = findvar(v, varname)) == 0) { + v2 = (CGI_varlist *) mymalloc(sizeof(*v2) + strlen(varname)); + strcpy((char *) v2->varname, varname); + v2->value = val; + v2->numvalue = 1; + v2->next = v2->iter = v2->tail = 0; + v2->vector = 0; + if (v == 0) { + v = v2; + } + else { + v->tail->next = v2; + } + v->tail = v2; + } + else { + v2->valtail->next = val; + v2->numvalue++; + } + v2->valtail = val; + if (v2->vector != 0) { + free((void *)v2->vector); + v2->vector = 0; + } + v->iter = 0; + return v; +} + +/* + * CGI_decode_query() adds all the names and values in query string + * "query" to variable list "v" (which may be null) and returns the + * resulting variable list. The query string has the form + * + * name1=value1&name2=value2&name3=value3 + * + * We convert '+' to ' ' and convert %xx to the character whose + * hex numeric value is xx. + */ + +CGI_varlist * +CGI_decode_query(CGI_varlist *v, const char *query) { + char *buf; + const char *name, *value; + int i, k, L, R, done; + + if (query == 0) { + return v; + } + buf = (char *) mymalloc(strlen(query) + 1); + name = value = 0; + for (i = k = done = 0; done == 0; i++) { + switch (query[i]) { + + case '=': + if (name != 0) { + break; /* treat extraneous '=' as data */ + } + if (name == 0 && k > 0) { + name = buf; + buf[k++] = 0; + value = buf + k; + } + continue; + + case 0: + done = 1; /* fall through */ + + case '&': + buf[k] = 0; + if (name == 0 && k > 0) { + name = buf; + value = buf + k; + } + if (name != 0) { + v = CGI_add_var(v, name, value); + } + k = 0; + name = value = 0; + continue; + + case '+': + buf[k++] = ' '; + continue; + + case '%': + if ((L = hex(query[i + 1])) >= 0 && + (R = hex(query[i + 2])) >= 0) + { + buf[k++] = (L << 4) + R; + i += 2; + continue; + } + break; /* treat extraneous '%' as data */ + } + buf[k++] = query[i]; + } + free(buf); + return v; +} + +/* + * CGI_get_cookie() adds all the cookie names and values from the + * environment variable HTTP_COOKIE to variable list "v" (which + * may be null) and returns the resulting variable list. + */ + +CGI_varlist * +CGI_get_cookie(CGI_varlist *v) { + const char *env; + char *buf, *p, *cookie[2]; + + if ((env = getenv("HTTP_COOKIE")) == 0) { + return v; + } + buf = (char *) mymalloc(strlen(env) + 1); + p = strcpy(buf, env); + while ((p = scanattr(p, cookie)) != 0) { + v = CGI_add_var(v, cookie[0], cookie[1]); + } + free(buf); + return v; +} + +/* + * CGI_get_query() adds all the field names and values from the + * environment variable QUERY_STRING to variable list "v" (which + * may be null) and returns the resulting variable list. + */ + +CGI_varlist * +CGI_get_query(CGI_varlist *v) { + return CGI_decode_query(v, getenv("QUERY_STRING")); +} + +/* + * CGI_get_post() reads field names and values from stdin and adds + * them to variable list "v" (which may be null) and returns the + * resulting variable list. We accept input encoded as + * "application/x-www-form-urlencoded" or as "multipart/form-data". + * In the case of a file upload, we write to a new file created + * with mkstemp() and "template". If the template is null or if + * mkstemp() fails then we silently discard the uploaded file data. + * The local name of the file (created by mkstemp()) and the remote + * name (as specified by the user) can be obtained with + * CGI_lookup_all(v, fieldname). + */ + +CGI_varlist * +CGI_get_post(CGI_varlist *v, const char *template) { + const char *env; + char *buf; + int len; + + if ((env = getenv("CONTENT_TYPE")) != 0 && + strncasecmp(env, "application/x-www-form-urlencoded", 33) == 0 && + (env = getenv("CONTENT_LENGTH")) != 0 && + (len = atoi(env)) > 0) + { + buf = (char *) mymalloc(len + 1); + if (fread(buf, 1, len, stdin) == len) { + buf[len] = 0; + v = CGI_decode_query(v, buf); + } + free(buf); + } + else { + v = read_multipart(v, template); + } + return v; +} + +/* + * CGI_get_all() returns a variable list that contains a combination of the + * following: cookie names and values from HTTP_COOKIE, field names and + * values from QUERY_STRING, and POSTed field names and values from stdin. + * File uploads are handled using "template" (see CGI_get_post()) + */ + +CGI_varlist * +CGI_get_all(const char *template) { + CGI_varlist *v = 0; + + v = CGI_get_cookie(v); + v = CGI_get_query(v); + v = CGI_get_post(v, template); + return v; +} + +/* CGI_free_varlist() frees all memory used by variable list "v" */ + +void +CGI_free_varlist(CGI_varlist *v) { + CGI_val *val, *valnext; + + if (v != 0) { + if (v->vector != 0) { + free((void *)v->vector); + } + for (val = v->value; val != 0; val = valnext) { + valnext = val->next; + free(val); + } + CGI_free_varlist(v->next); + free(v); + } +} + + +/* + * CGI_lookup() searches variable list "v" for an entry named + * "varname" and returns null if not found. Otherwise we return the + * first value associated with "varname", which is a null terminated + * string. If varname is null then we return the first value of the + * "current entry", which was set using the iterating functions + * CGI_first_name() and CGI_next_name(). + */ + +const char * +CGI_lookup(CGI_varlist *v, const char *varname) { + return (v = findvar(v, varname)) == 0 ? 0 : v->value->value; +} + +/* + * CGI_lookup_all() searches variable list "v" for an entry named + * "varname" and returns null if not found. Otherwise we return + * a pointer to a null terminated array of string pointers (see + * CGI_value) where each string is a value of the variable. If + * varname is null then we return the values of the "current entry", + * which was set using the iterating functions CGI_first_name() and + * CGI_next_name(). + */ + +CGI_value * +CGI_lookup_all(CGI_varlist *v, const char *varname) { + CGI_val *val; + int i; + + if ((v = findvar(v, varname)) == 0) { + return 0; + } + if (v->vector == 0) { + v->vector = (CGI_value *) + mymalloc(sizeof(CGI_value) * (v->numvalue + 1)); + i = 0; + + /* to initialize v->vector we must cast away const */ + + for (val = v->value; val != 0 && i < v->numvalue; + val = val->next) + { + ((const char **)v->vector)[i++] = val->value; + } + ((const char **)v->vector)[i] = 0; + } + return v->vector; +} + +/* + * CGI_first_name() returns the name of the first entry in + * variable list "v", or null if "v" is null. + */ + +const char * +CGI_first_name(CGI_varlist *v) { + return v == 0 ? 0 : (v->iter = v)->varname; +} + +/* + * CGI_next_name() returns the name of the next entry in variable list + * "v" after the most recent call to CGI_first_name() or CGI_next_name(). + * We return null if there are no more entries + */ + +const char * +CGI_next_name(CGI_varlist *v) { + return v == 0 || v->iter == 0 || (v->iter = v->iter->next) == 0 ? + 0 : v->iter->varname; +} + +/* + * CGI_encode_entity() converts null terminated string "in" to + * HTML entity encoding where > become > and < become < + * and & becomes & etc., and returns the result. Allocates + * memory for the result with malloc(). + */ + +char * +CGI_encode_entity(const char *in) { + char *out, *p; + int i, k; + + if (in == 0) { + return 0; + } + for (i = k = 0; in[i] != 0; i++) { + switch(in[i]) { + + case '<': + case '>': + k += 4; + break; + case '&': + case '\'': + case '\r': + case '\n': + k += 5; + break; + case '"': + k += 6; + break; + default: + k++; + break; + } + } + out = p = mymalloc(k + 1); + + for (i = 0; in[i] != 0; i++) { + switch(in[i]) { + + case '<': + *p++ = '&'; + *p++ = 'l'; + *p++ = 't'; + *p++ = ';'; + break; + case '>': + *p++ = '&'; + *p++ = 'g'; + *p++ = 't'; + *p++ = ';'; + break; + case '&': + *p++ = '&'; + *p++ = 'a'; + *p++ = 'm'; + *p++ = 'p'; + *p++ = ';'; + break; + case '\'': + *p++ = '&'; + *p++ = '#'; + *p++ = '3'; + *p++ = '9'; + *p++ = ';'; + break; + case '\r': + *p++ = '&'; + *p++ = '#'; + *p++ = '1'; + *p++ = '3'; + *p++ = ';'; + break; + case '\n': + *p++ = '&'; + *p++ = '#'; + *p++ = '1'; + *p++ = '0'; + *p++ = ';'; + break; + case '"': + *p++ = '&'; + *p++ = 'q'; + *p++ = 'u'; + *p++ = 'o'; + *p++ = 't'; + *p++ = ';'; + break; + default: + *p++ = in[i]; + break; + } + } + *p = 0; + return out; +} + +/* base64 conversion */ + +static const char b64encode[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" + "abcdefghijklmnopqrstuvwxyz0123456789+/"; + +#define BAD 100 + +static const unsigned char b64decode[] = { + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, 62, BAD, BAD, BAD, 63, + 52, 53, 54, 55, 56, 57, 58, 59, + 60, 61, BAD, BAD, BAD, BAD, BAD, BAD, + + BAD, 0, 1, 2, 3, 4, 5, 6, + 7, 8, 9, 10, 11, 12, 13, 14, + 15, 16, 17, 18, 19, 20, 21, 22, + 23, 24, 25, BAD, BAD, BAD, BAD, BAD, + BAD, 26, 27, 28, 29, 30, 31, 32, + 33, 34, 35, 36, 37, 38, 39, 40, + 41, 42, 43, 44, 45, 46, 47, 48, + 49, 50, 51, BAD, BAD, BAD, BAD, BAD, + + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD, + BAD, BAD, BAD, BAD, BAD, BAD, BAD, BAD +}; + +/* + * CGI_decode_base64() decodes null terminated base64 encoded string p + * and returns the result. We store the length of the result in + * *len and we write a null byte after the last byte of the result. + * We allocate memory for the result with malloc(); + */ + +void * +CGI_decode_base64(const char *p, int *len) { + const unsigned char *in = (const unsigned char *) p; + unsigned char *out; + int save = 0, nbits = 0, sixbits; + int i, k; + + if (p == 0) { + return 0; + } + out = mymalloc(3 + 3 * strlen(p) / 4); + + /* every four base64 input characters becomes three output bytes */ + + for (i = k = 0; in[i] != 0; i++) { + if ((sixbits = b64decode[in[i]]) == BAD) { + continue; + } + save |= sixbits << (18 - nbits); /* 4 x 6 bits in */ + if ((nbits += 6) == 24) { + out[k++] = save >> 16; /* 3 x 8 bits out */ + out[k++] = save >> 8; + out[k++] = save; + nbits = 0; + save = 0; + } + } + + /* convert leftover bits */ + + for (i = 16; i >= 0 && nbits >= 8; i -= 8) { + out[k++] = save >> i; + nbits -= 8; + } + out[k] = 0; + if (len != 0) { + *len = k; + } + return out; +} + +/* + * CGI_encode_base64() base64 encodes bytes in array p of length len + * and returns the result, which is a null terminated base64 encoded + * string. We allocate memory for the result with malloc(). + */ + +char * +CGI_encode_base64(const void *p, int len) { + const unsigned char *in = p; + char *out; + int save = 0, nbits = 0; + int i, k = 0; + + if (in == 0 || len <= 0) { + return 0; + } + out = mymalloc(4 + 4 * len / 3); + + /* every three input bytes becomes 4 base64 output characters */ + + for (i = 0; i < len; i++) { + save |= in[i] << (16 - nbits); /* 3 x 8 bits in */ + if ((nbits += 8) == 24) { + out[k++] = b64encode[(save >> 18) & 077]; /* 4 x 6 bits out */ + out[k++] = b64encode[(save >> 12) & 077]; + out[k++] = b64encode[(save >> 6) & 077]; + out[k++] = b64encode[ save & 077]; + nbits = 0; + save = 0; + } + } + + /* convert leftover bits */ + + if (nbits > 0) { + for (i = 18; i >= 0; i -= 6) { + if (nbits > 0) { + out[k++] = b64encode[(save >> i) & 077]; + nbits -= 6; + } + else { + out[k++] = '='; + } + } + } + out[k] = 0; + return out; +} + +/* hex conversion */ + +/* + * CGI_decode_hex() decodes null terminated hex encoded string p + * and returns the result. We store the length of the result in + * *len and we write a null byte after the last byte of the result. + * We allocate memory for the result with malloc(); + */ + +void * +CGI_decode_hex(const char *p, int *len) { + unsigned char *out; + int i, k, n, L, R; + + if (p == 0 || ((n = strlen(p)) & 1)) { + return 0; /* length of input must be even */ + } + out = mymalloc(n / 2 + 1); + for (i = k = 0; i < n; i += 2) { + if ((L = hex(p[i])) >= 0 && (R = hex(p[i + 1])) >= 0) { + out[k++] = (L << 4) + R; + } + else { + free(out); + return 0; + } + } + out[k] = 0; + if (len != 0) { + *len = k; + } + return out; +} + +/* + * CGI_encode_hex() hex encodes bytes in array p of length len + * and returns the result, which is a null terminated hex encoded + * string. We allocate memory for the result with malloc(). + */ + +char * +CGI_encode_hex(const void *p, int len) { + const unsigned char *in = p; + int i, k; + char *out; + const char hexdigit[] = "0123456789ABCDEF"; + + if (in == 0 || len <= 0) { + return 0; + } + out = mymalloc(len * 2 + 1); + for (i = k = 0; i < len; i++) { + out[k++] = hexdigit[in[i] >> 4]; + out[k++] = hexdigit[in[i] & 0xf]; + } + out[k] = 0; + return out; +} diff --git a/test/ccgi-1.2/ccgi.h b/test/ccgi-1.2/ccgi.h new file mode 100644 index 00000000..dae1607f --- /dev/null +++ b/test/ccgi-1.2/ccgi.h @@ -0,0 +1,64 @@ +/* + * C CGI Library version 1.2 + * + * Copyright 2015 Stephen C. Losen. Distributed under the terms + * of the GNU Lesser General Public License (LGPL 2.1) + */ + +#ifndef _CCGI_H +#define _CCGI_H + +typedef struct CGI_varlist CGI_varlist; + +typedef const char * const CGI_value; + +char *CGI_decode_url(const char *p); + +char *CGI_encode_url(const char *p, const char *keep); + +char *CGI_encode_entity(const char *p); + +char *CGI_encode_base64(const void *p, int len); + +void *CGI_decode_base64(const char *p, int *len); + +char *CGI_encode_hex(const void *p, int len); + +void *CGI_decode_hex(const char *p, int *len); + +char *CGI_encrypt(const void *p, int len, const char *password); + +void *CGI_decrypt(const char *p, int *len, const char *password); + +char *CGI_encode_query(const char *keep, ...); + +char *CGI_encode_varlist(CGI_varlist *v, const char *keep); + +CGI_varlist *CGI_decode_query(CGI_varlist *v, const char *query); + +CGI_varlist *CGI_get_cookie(CGI_varlist *v); + +CGI_varlist *CGI_get_query(CGI_varlist *v); + +CGI_varlist *CGI_get_post(CGI_varlist *v, const char *template); + +CGI_varlist *CGI_get_all(const char *template); + +CGI_varlist *CGI_add_var(CGI_varlist *v, const char *varname, + const char *value); + +void CGI_free_varlist(CGI_varlist *v); + +CGI_value *CGI_lookup_all(CGI_varlist *v, const char *varname); + +const char *CGI_lookup(CGI_varlist *v, const char *varname); + +const char *CGI_first_name(CGI_varlist *v); + +const char *CGI_next_name(CGI_varlist *v); + +void CGI_prefork_server(const char *host, int port, const char *pidfile, + int maxproc, int minidle, int maxidle, int maxreq, + void (*callback)(void)); + +#endif diff --git a/test/ccgi-1.2/crypt.c b/test/ccgi-1.2/crypt.c new file mode 100644 index 00000000..0db7a3bb --- /dev/null +++ b/test/ccgi-1.2/crypt.c @@ -0,0 +1,170 @@ +/* + * C CGI Library version 1.2 + * + * Author: Stephen C. Losen, University of Virginia + * + * Copyright 2015 Stephen C. Losen. Distributed under the terms + * of the GNU Lesser General Public License (LGPL 2.1) + * + * CGI_encrypt() converts arbitrary input data to a secure, + * encrypted string, using a password provided by the user. + * CGI_decrypt() reverses the process and returns the original + * data. The secure string includes a message digest for + * verifying the data. + */ + +#include +#include +#include +#include +#include +#include + +#define SALT_SIZE 8 +#define DIGEST_SIZE 20 + +/* + * init_salt() places 8 random bytes into salt with RAND_bytes() + * or else uses SHA1 digest of current time and process ID + */ + +static void +init_salt(unsigned char *salt) { + EVP_MD_CTX ctx; + unsigned char md[DIGEST_SIZE]; + struct timeval tv; + pid_t pid; + unsigned int rlen; + + if (RAND_bytes(salt, SALT_SIZE) == 1) { + return; + } + gettimeofday(&tv, 0); + pid = getpid(); + EVP_DigestInit(&ctx, EVP_sha1()); + EVP_DigestUpdate(&ctx, &tv, sizeof(tv)); + EVP_DigestUpdate(&ctx, &pid, sizeof(pid)); + EVP_DigestFinal(&ctx, md, &rlen); + memcpy(salt, md, SALT_SIZE); +} + +/* + * digest() feeds input p of length len, the password and + * the salt to SHA1 and returns the digest in md. + */ + +static void +digest(const void *p, int len, const char *password, + const unsigned char *salt, unsigned char *md) +{ + EVP_MD_CTX ctx; + unsigned int rlen; + + EVP_DigestInit(&ctx, EVP_sha1()); + EVP_DigestUpdate(&ctx, salt, SALT_SIZE); + EVP_DigestUpdate(&ctx, password, strlen(password)); + EVP_DigestUpdate(&ctx, p, len); + EVP_DigestFinal(&ctx, md, &rlen); +} + +/* + * CGI_encrypt() returns an encrypted base64 string using input p + * of length len and the password. We generate a random 8 byte + * salt. We compute a SHA1 message digest using p, the password, + * and the salt. We generate a cipher key using the password and + * salt. We encrypt the digest and input with the AES-256-CBC cipher + * and key. We base64 encode the return string, which consists of + * the unencrypted salt, encrypted digest and encrypted input. + */ + +char * +CGI_encrypt(const void *p, int len, const char *password) { + EVP_CIPHER_CTX ctx; + unsigned char md[DIGEST_SIZE]; + unsigned char iv[EVP_MAX_IV_LENGTH]; + unsigned char key[EVP_MAX_KEY_LENGTH]; + unsigned char *out; + char *b64; + int offset, rlen; + + if (p == 0 || len <= 0 || password == 0 || *password == 0) { + return 0; + } + out = malloc(SALT_SIZE + DIGEST_SIZE + len + EVP_MAX_BLOCK_LENGTH); + init_salt(out); + digest(p, len, password, out, md); + EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), out, + (unsigned char *) password, strlen(password), 1, key, iv); + EVP_EncryptInit(&ctx, EVP_aes_256_cbc(), key, iv); + offset = SALT_SIZE; + EVP_EncryptUpdate(&ctx, out + offset, &rlen, md, DIGEST_SIZE); + offset += rlen; + EVP_EncryptUpdate(&ctx, out + offset, &rlen, p, len); + offset += rlen; + EVP_EncryptFinal(&ctx, out + offset, &rlen); + b64 = CGI_encode_base64(out, offset + rlen); + free(out); + return b64; +} + +/* + * CGI_decrypt() returns a pointer to data decrypted from + * base64 string p and the password. p was previously encrypted + * with CGI_encrypt(). We base64 decode p and the first 8 bytes + * is the salt followed by the encrypted message digest and + * encrypted data. We generate the cipher key using the salt + * and the password. We decrypt the message digest and data + * using the AES-256-CBC cipher and key. We re-compute the + * message digest using the decrypted data, password, and salt. + * If the two digests match then we return a pointer to the data + * and place its length in *len. Otherwise we return null. + */ + +void * +CGI_decrypt(const char *p, int *len, const char *password) { + EVP_CIPHER_CTX ctx; + unsigned char md[DIGEST_SIZE]; + unsigned char iv[EVP_MAX_IV_LENGTH]; + unsigned char key[EVP_MAX_KEY_LENGTH]; + unsigned char *ret, *out; + int offset, rlen = 0; + + if (p == 0 || *p == 0 || password == 0 || *password == 0) { + return 0; + } + ret = CGI_decode_base64(p, &rlen); + if (rlen <= DIGEST_SIZE + SALT_SIZE) { + free(ret); + return 0; + } + out = malloc(rlen + EVP_MAX_BLOCK_LENGTH); + EVP_BytesToKey(EVP_aes_256_cbc(), EVP_sha1(), ret, + (unsigned char *) password, strlen(password), 1, key, iv); + EVP_DecryptInit(&ctx, EVP_aes_256_cbc(), key, iv); + EVP_DecryptUpdate(&ctx, out, &offset, ret + SALT_SIZE, + rlen - SALT_SIZE); + EVP_DecryptFinal(&ctx, out + offset, &rlen); + rlen += offset - DIGEST_SIZE; + + /* + * The salt is in ret, the decrypted digest and decrypted + * data are in out, and the data length is rlen. + */ + + if (rlen > 0) { + digest(out + DIGEST_SIZE, rlen, password, ret, md); + } + if (rlen > 0 && memcmp(out, md, DIGEST_SIZE) == 0) { + memcpy(ret, out + DIGEST_SIZE, rlen); + ret[rlen] = 0; + if (len != 0) { + *len = rlen; + } + } + else { + free(ret); + ret = 0; + } + free(out); + return ret; +} diff --git a/test/ccgi-1.2/doc.html b/test/ccgi-1.2/doc.html new file mode 100644 index 00000000..4a4e7e6c --- /dev/null +++ b/test/ccgi-1.2/doc.html @@ -0,0 +1,921 @@ + + + + + + + C CGI Library 1.2 + + + + +

C CGI Library 1.2

+ +

Contents

+ + + +

Introduction

+ +

C CGI is a C language library for decoding, storing, + and retrieving CGI data passed by the web server via the CGI + interface. The library also has several handy data conversion + functions.

+ +

Author: Stephen C. Losen, University of Virginia

+ +

C CGI Library Features

+ +
    +
  • Decodes and stores CGI variables in application/x-www-form-urlencoded format, which + may come from the QUERY_STRING environment variable or + from standard input.
  • + +
  • Decodes and stores CGI variables in + multipart/form-data format from standard input.
  • + +
  • Handles file uploads.
  • + +
  • Parses and stores HTTP cookies from the + HTTP_COOKIE environment variable.
  • + +
  • Stores CGI data in lookup tables, which can be accessed + directly by variable name, or accessed iteratively.
  • + +
  • Allows strings to be stored in lookup tables.
  • + +
  • Encodes/decodes strings in application/x-www-form-urlencoded format.
  • + +
  • Encodes/decodes data in base64 format.
  • + +
  • Encodes/decodes data in hexadecimal format.
  • + +
  • Encodes text strings using HTML entity encodings such as + &lt; and &amp;.
  • + +
  • Provides openssl cryptography + to encrypt/decrypt and verify data.
  • + +
  • Provides a pre forking SCGI + server.
  • +
+ +

Query Strings and URL + Encoding

+ +

A query string is a list of CGI variable names and + values in application/x-www-form-urlencoded format, which + looks like this: + name1=value1&name2=value2&name3=value3 ... + Each name and value is URL encoded as follows. Letters and + digits are not changed. Each space is converted to + +. Most other characters become %xx (percent + followed by two hexadecimal digits) where xx is the + numeric code of the character. For example, Help Me! + is URL encoded as Help+Me%21. Note that in a query string + the = and & characters between names and + values are not URL encoded. However, if a name or value + itself contains a = or &, then it is URL + encoded using %xx.

+ +

When decoding query strings, the C CGI Library is tolerant of + lax %xx encoding. It accepts most literal punctuation + characters except for + and &, which must be + encoded. It accepts literal = in variable values and it + accepts literal % when not followed by two hexadecimal + digits. For example, var=20%=1/5 is treated like + var=20%25%3D1%2F5 and sets the value of var to + 20%=1/5. A string that is not followed by = is a + variable name whose value is "", a zero length string. For + example, str1&str2& ... is the same as + str1=&str2=& ..., where str1 and + str2 are variable names whose value is "".

+ +

CGI Data Representation and Conversion

+ +

For simplicity and ease of use, most C CGI Library functions + accept and/or return null terminated strings. You can easily + convert a string to a numeric data type with the standard C + library functions atoi(), atof(), strtol(), + strtod(), etc. And you can convert a numeric data type to + a string with sprintf().

+ +

Unfortunately, null terminated strings are not suitable for + storing raw binary data, because a null byte in the data is + mistaken for the string terminator. URL encoded strings + containing %00 do not decode correctly because %00 + results in a null byte. You can still manipulate binary data if + you encode it beforehand, and the C CGI library has functions for + encoding/decoding base64 and + hexadecimal.

+ +

C CGI Library Data Types

+ +

In your C source you include the ccgi.h header file, + which declares these data types.

+ +
+
CGI_varlist
+ +
is a list (lookup table) of CGI variables and/or cookies. + Each list entry is a name and one or more values, where names + and values are all null terminated strings. A name may have + multiple values because 1) some HTML form elements, such as + checkboxes and selections allow the user to + choose multiple values and 2) the same name can be given to + multiple form input elements. A CGI_varlist lists variable + names and values in the same order that they are stored. In + practice this ends up being the order of the input tags in the + HTML form, but there is no requirement that browsers must + preserve this ordering.
+ +
CGI_value
+ +
is a read only pointer to a read only null terminated + string (const char * const). The CGI_lookup_all() function returns a null + terminated array of these pointers.
+
+ +

C CGI Library Functions

+ +

The C CGI library provides these functions.

+ + + +

Except for CGI_prefork_server(), the C CGI library + functions are reentrant because they do not modify any global + variables or use any static local variables, so you can use these + functions with threads.

+ +

Some functions accept null terminated string parameters of + type const char *. These functions make copies + of strings as necessary so that after the function returns you + can safely do anything you want with any string that you have + passed as a parameter. Some functions return null terminated + strings of type const char * and you should not + modify these strings.

+ +
+
CGI_varlist + *CGI_get_query (CGI_varlist *varlist);
+ +
CGI_get_query() decodes CGI variables in the + QUERY_STRING environment variable and adds them to + variable list varlist. QUERY_STRING is presumed + to be in application/x-www-form-urlencoded + format. If varlist is null then a new variable list is + created and returned, otherwise varlist is returned. + Null is returned if varlist is null and + QUERY_STRING does not exist or contains no CGI + variables.
+ +
CGI_varlist + *CGI_get_post(CGI_varlist *varlist, const char *template);
+ +
CGI_get_post() reads and decodes CGI variables from + standard input and adds them to variable list varlist. + If varlist is null then a new variable list is created + and returned, otherwise varlist is returned. Null is + returned if varlist is null and standard input is empty + or contains no CGI variables. The template parameter + (which may be null) is a file name template string that is + passed to the standard C library function mkstemp() when + uploading a file. (See the file upload + section for more information.) CGI_get_post() checks the + CONTENT_TYPE environment variable to get the data + encoding, which is either application/x-www-form-urlencoded or + multipart/form-data.
+ +
+ CGI_varlist *CGI_get_cookie(CGI_varlist *varlist);
+ +
CGI_get_cookie() parses HTTP cookies from the + HTTP_COOKIE environment variable and adds them to + variable list varlist. If varlist is null then a + new variable list is created and returned, otherwise + varlist is returned. Returns null if varlist is + null and HTTP_COOKIE does not exist or contains no + cookies.
+ +
CGI_varlist + *CGI_get_all(const char *template);
+ +
CGI_get_all() calls CGI_get_cookie(), CGI_get_query() and CGI_get_post(), returning all of the CGI + variables and cookies in one variable list. The template + parameter (which may be null) is passed on to + CGI_get_post().
+ +
+ CGI_varlist *CGI_decode_query(CGI_varlist *varlist, const char + *query);
+ +
CGI_decode_query() decodes CGI variables in null + terminated query string query + (which is in application/x-www-urlencoded format) and + adds the CGI variables to varlist. If varlist is + null then a new variable list is created and returned, + otherwise varlist is returned. Returns null if + varlist is null and query is null or has no CGI + variables.
+ +
CGI_varlist + *CGI_add_var(CGI_varlist *varlist, const char *name, const char + *value);
+ +
CGI_add_var() adds an entry named name with + value value to variable list varlist. If + varlist is null, then a new variable list is created and + returned, otherwise varlist is returned. If the variable + list already has an entry named name, then the value is + added to that entry. This function is provided so that you can + add data to a variable list by hand, or create a variable list + for other purposes.
+ +
CGI_value + *CGI_lookup_all(CGI_varlist *varlist, const char *name);
+ +
CGI_lookup_all() searches varlist for an + entry whose name matches name case sensitively and + returns all the values of the entry, or returns null if no + entry is found. If name is null, then the values of the + entry most recently visited by CGI_first_name() or CGI_next_name() are returned. The return + value is a null terminated array of pointers to null terminated + strings. The array and strings are stored in memory allocated + to varlist, which you should not modify. The return type + CGI_value * + (const char * const *) declares the + array and strings to be read only to discourage + modification.
+ +
const char + *CGI_lookup(CGI_varlist *varlist, const char *name);
+ +
CGI_lookup() searches varlist for an entry + whose name matches name case sensitively and returns the + first (or only) value of the entry, or returns null if no entry + is found. If name is null, then the first value of the + entry most recently visited by CGI_first_name() or CGI_next_name() is returned. If you expect + an entry to have a single value, then this function is easier + to use than CGI_lookup_all() and it is more efficient + because it doesn't construct an array to return multiple + values. You should not modify the string returned by this + function.
+ +
const + char *CGI_first_name(CGI_varlist *varlist);
+ +
CGI_first_name() begins an iteration of + varlist and returns the name of the first entry, or + returns null if varlist is null. You can get all the + values of this entry with + CGI_lookup_all(varlist, 0); You should not modify + the string returned by this function.
+ +
const char + *CGI_next_name(CGI_varlist *varlist);
+ +
CGI_next_name() continues an iteration of + varlist and returns the name of the next entry. You can + get all the values of this entry with + CGI_lookup_all(varlist, 0); Returns null if 1) + there are no more entries, or 2) varlist is null, or 3) + no iteration was started with CGI_first_name(), or 4) + new data was added to varlist during the iteration. You + should not modify the string returned by this function.
+ +
void + CGI_free_varlist(CGI_varlist *varlist);
+ +
CGI_free_varlist() frees all memory used by variable + list varlist.
+ +
char + *CGI_encode_query(const char *keep, const char *name1, const + char *value1, ..., (char *)0);
+ +
CGI_encode_query() returns a query string in + application/x-www-form-urlencoded format + that is built from a null terminated list of null terminated + string arguments. The first argument keep (which may be + null) is a null terminated string that specifies characters + that you do not want to URL encode with + %xx. You do not need to specify letters or digits + because they are never encoded. The first two arguments after + keep are a name and value pair, the next two are a + second name and value pair, etc. Be sure to terminate the + argument list with (char *)0. (When passing a variable + length argument list, the C compiler does not automatically + cast 0 to (char *)0, which is necessary + on 64 bit platforms.) In the result the names + and values are URL encoded and separated with literal + & and = characters like this: + name1=value1&name2=value2 ... Memory is + allocated with malloc() to hold the result, which you + should free with free().
+ +
+ char *CGI_encode_varlist(CGI_varlist *varlist, const char + *keep);
+ +
CGI_encode_varlist() returns a query string in + application/x-www-form-urlencoded format + that is built from CGI_varlist varlist. The argument + keep (which may be null) is a null terminated string + that specifies characters that you do not want to URL encode with %xx. You do not need to + specify letters or digits because they are never encoded. The + names and values in varlist are URL encoded and + separated with literal & and = characters + like this: name1=value1&name2=value2 ... Memory + is allocated with malloc() to hold the result, which you + should free with free(). You can use CGI_add_var() to build varlist.
+ +
char + *CGI_encrypt(const void *p, int len, const char + *password);
+ +
CGI_encrypt() encrypts input p of length + len bytes using password to generate the cipher + key. Also computes a message digest using the input + data. Returns the encrypted digest and encrypted data in a + base64 encoded string, which must be decrypted with CGI_decrypt() and the same password. Returns + null if p is null or if len is less than one or + if password is null or zero length. Memory is allocated + with malloc() to hold the result, which you should free + with free(). (See the cryptography + section for more information.)
+ +
void + *CGI_decrypt(const char *p, int *len, const char + *password);
+ +
CGI_decrypt() decrypts input p, which was + encrypted with CGI_encrypt(), using + password to generate the cipher key. The output is a + message digest and decrypted data bytes. Verifies the + data using the message digest and returns the data. Returns the + length of the data in *len. Returns null if p + cannot be decrypted and verified. Also returns null if p + or password is null or zero length. Memory is allocated + with malloc() to hold the result, which you should free + with free(). (See the cryptography + section for more information.)
+ +
char + *CGI_decode_url(const char *p);
+ +
CGI_decode_url() returns a URL + decoded copy of input string p. Memory is allocated + with malloc() to hold the result, which you should free + with free().
+ +
char + *CGI_encode_url(const char *p, const char *keep);
+ +
CGI_encode_url() returns a URL + encoded copy of input string p. Memory is allocated + with malloc() to hold the result, which you should free + with free(). The keep argument (which may be + null) is a null terminated string that specifies characters + that you do not want to URL encode with %xx. You do not + need to specify letters or digits because they are never + encoded.
+ +
+ char *CGI_encode_entity(const char *p);
+ +
CGI_encode_entity() returns a HTTP entity encoded + copy of input string p. Memory is allocated with + malloc() to hold the result, which you should free with + free(). CGI_encode_entity() makes the following + conversions: < becomes &lt;, > + becomes &gt;, & becomes &amp;, + " becomes &quot;, ' becomes + &#39;, newline becomes &#10;, and + return becomes &#13;.
+ +
+ char *CGI_encode_base64(const void *p, int len);
+ +
CGI_encode_base64() encodes input p of length + len bytes, and returns the result, which is a null + terminated base64 encoded string. Memory is allocated for the + result with malloc(), which you should free with + free(). Base64 is a commonly used encoding that + represents arbitrary bytes of data using the following + printable characters: upper case, lower case, digits, +, + /, and =.
+ +
+ void *CGI_decode_base64(const char *p, int *len);
+ +
CGI_decode_base64() decodes p, which is a + null terminated base64 encoded string, and returns the result. + The length of the result is stored in *len and a null + byte is written just after the last byte of the result. Memory + is allocated with malloc() to hold the result, which you + should free with free().
+ +
char + *CGI_encode_hex(const void *p, int len);
+ +
CGI_encode_hex() encodes input p, of length + len bytes, and returns the result, which is a null + terminated hexadecimal encoded string. Memory is allocated for + the result with malloc(), which you should free with + free(). Hexadecimal is a commonly used encoding that + represents arbitrary bytes of data using two hexadecimal digits + for each byte.
+ +
void + *CGI_decode_hex(const char *p, int *len);
+ +
CGI_decode_hex() decodes p, which is a null + terminated hexadecimal encoded string, and returns the result. + The length of the result is stored in *len and a null + byte is written just after the last byte of the result. Memory + is allocated with malloc() to hold the result, which you + should free with free(). Returns null if p is + null, or if the length of p is odd, or if p + contains characters other than hexadecimal digits.
+ +
+ void CGI_prefork_server(const char *host, int port, const char + *pidfile, int maxproc, int minidle, int maxidle, int maxreq, + void (*callback)(void));
+ +
CGI_prefork_server() implements a SCGI (Simple + CGI) pre forking server. The host specifies a local + network address, either by hostname or dotted decimal IP + address, and port specifies a TCP port number. The SCGI + server listens for requests on the specified address and port. + If host is null, then the server listens on all local + addresses. The pidfile (which may be null) is an + optional file name where the server writes its process ID. The + SCGI server forks up to maxproc child processes to + handle requests. It forks and destroys processes to maintain + between minidle and maxidle idle processes. Each + process exits after handling maxreq requests. If + maxreq is less than one, then it is unlimited. You + provide the callback function, which the SCGI server + calls to process each web request. CGI_prefork_server() + does not return unless it fails. (See the SCGI server section for more information.)
+
+ +

Using the C CGI Library

+ +

Here is an example program that outputs all of its CGI data. + In your C source, include ccgi.h and link your program + with libccgi.a. (If you use CGI_encrypt() or + CGI_decrypt() then you must also link with the + openssl library libcrypto.) The simplest way to + obtain your CGI data is with CGI_get_all(). If you are not + uploading any files, then just pass it a null argument.

+
+#include <stdio.h>
+#include <ccgi.h>
+
+int main(int argc, char **argv) {
+    CGI_varlist *varlist;
+    const char *name;
+    CGI_value  *value;
+    int i;
+
+    fputs("Content-type: text/plain\r\n\r\n", stdout);
+    if ((varlist = CGI_get_all(0)) == 0) {
+        printf("No CGI data received\r\n");
+        return 0;
+    }
+
+    /* output all values of all variables and cookies */
+
+    for (name = CGI_first_name(varlist); name != 0;
+        name = CGI_next_name(varlist))
+    {
+        value = CGI_lookup_all(varlist, 0);
+
+        /* CGI_lookup_all(varlist, name) could also be used */
+
+        for (i = 0; value[i] != 0; i++) {
+            printf("%s [%d] = %s\r\n", name, i, value[i]);
+        }
+    }
+    CGI_free_varlist(varlist);  /* free variable list */
+    return 0;
+}
+
+ +

File Uploads

+ +

To upload files to your CGI program, your HTML form must use + the post method and must specify + multipart/form-data encoding, so the form tag looks like + this:

+
+<form method="POST" enctype="multipart/form-data"
+    action="url-for-your-CGI">
+
+ +

Within the HTML form a file upload tag looks like this:

+
+<input type="file" name="uploadfield" />
+
+ +

Most browsers render this tag with a file browse button and a + text field to enter and/or display the name of the file being + uploaded.

+ +

When the user submits the form, the browser sends the file + data together with any CGI form variables using + multipart/form-data encoding. To receive uploaded file + data you must call CGI_get_post() or + CGI_get_all(), and pass a file name + template string, a copy of which is passed on to standard C + function mkstemp(). The final six characters of the + template string must be XXXXXX and mkstemp() + replaces these with random characters to create a new file with a + unique name. If you pass a null or invalid template string, then + uploaded file data is silently discarded.

+ +

CGI_get_post() or CGI_get_all() stores two names + for the uploaded file in the variable list, which you can + retrieve with

+
+value = CGI_lookup_all(varlist, "uploadfield");
+
+ +

This returns an array of two strings (provided no other form + input tags are named uploadfield). In value[0] is + the name of the uploaded file on the web server, which is derived + from the template string. In value[1] is the name of the + file specified by the user in the browser. If the user has not + uploaded a file, then varlist has no entry named + uploadfield and CGI_lookup_all() returns null.

+ +

We use mkstemp() to guarantee unique file names because + a form may have multiple file upload fields, resulting in + multiple files. Furthermore, multiple users can upload files to + multiple instances of the CGI at the same time. Here is an + example CGI program that uploads a file.

+
+#include <ccgi.h>
+#include <stdio.h>
+
+int main(int argc, char **argv) {
+    CGI_varlist *varlist;
+    CGI_value *value;
+
+    fputs("Content-type: text/plain\r\n\r\n", stdout);
+    varlist = CGI_get_all("/tmp/cgi-upload-XXXXXX");
+    value = CGI_lookup_all(varlist, "uploadfield");
+    if (value == 0 || value[1] == 0) {
+        fputs("No file was uploaded\r\n", stdout);
+    }
+    else {
+        printf("Your file \"%s\" was uploaded to my file \"%s\"\r\n",
+            value[1], value[0]);
+
+        /* Do something with the file here */
+
+        unlink(value[0]);
+    }
+    CGI_free_varlist(varlist);
+    return 0;
+}
+
+ +

Simple Cryptography + Support

+ +

HTML and HTTP provide no native support for protecting and + verifying CGI data. Many web applications pass state data to the + browser in cookies or form variables. When the browser passes + this data back, the web application cannot tell if it has been + tampered with. An attacker can easily handcraft a web request + that includes forged cookies or forged form data.

+ +

The C CGI Library addresses this problem with CGI_encrypt() and CGI_decrypt(). CGI_encrypt() computes a + SHA1 message digest from the input data, encrypts the + digest and the input data, and returns the result in a base64 + encoded string. (Raw encrypted output is binary.) + CGI_decrypt() reverses the process. It decrypts the digest + and the data, and recomputes the digest. If the two digests + match, then it returns the data. Otherwise it returns null to + indicate failure. CGI_encrypt() and CGI_decrypt() + use a password that you provide, which is a null terminated + string of arbitrary length (the longer the better). It is + essentially impossible to tamper with the data without knowing + the password. If the output of CGI_encrypt() is modified + in any way, then CGI_decrypt() computes a message digest + that does not match and returns null.

+ +

To protect state data, simply encrypt it with + CGI_encrypt() and a password before passing it to the + browser. When the browser passes the encrypted data back, decrypt + with CGI_decrypt() and the same password. If + CGI_decrypt() succeeds, then you know that the data has + not changed. Of course the security of the data depends on the + security of the password, which should be very difficult to guess + and very difficult to steal.

+ +

CGI_encrypt() uses the openssl library + libcrypto and has these features:

+ +
    +
  • Uses the AES-256-CBC cipher.
  • + +
  • Generates the cipher key by feeding the password and a + random salt to a hash function. One password results in + a huge number of different cipher keys.
  • + +
  • Uses the SHA1 message digest algorithm to verify the + input data. Feeds the salt, the password, and the input data to + SHA1 to generate the message digest.
  • + +
  • Encrypts the message digest and the input data. (Does not + encrypt the salt because decryption needs it.)
  • + +
  • Returns a base64 encoded string consisting of the salt, + encrypted digest and encrypted input data.
  • +
+ +

Pre Forking SCGI + Server

+ +

Usually when a web server receives a request for a CGI + resource, the web server executes a CGI program, which handles + the request and exits. This does not perform well under high + load. SCGI (Simple + CGI) is a protocol for running a persistent CGI server. When a + web server receives a request for a SCGI resource, the web server + connects to a SCGI Server and forwards the request using + the SCGI protocol. The SCGI server responds to the web server, + which forwards the response back to the user's browser. This is + much more efficient than executing a CGI program for each + request. To configure the Apache httpd web server to use SCGI, + see the mod_scgi + module.

+ +

Under high load a SCGI server must be able to handle multiple + requests concurrently. The SCGI server provided here pre + forks a specified number of child processes that all wait for + requests. The parent process monitors how many child processes + are busy and creates more if necessary. If too many processes are + idle, then the parent terminates some of them.

+ +

You provide the code that handles web requests in a + callback function, which is called once for each request. + The environment, standard input, and standard output are set up + so that your callback can be written very much like a + traditional CGI program. All the functions in the C CGI library + work as specified when using the SCGI server.

+ +

To start the SCGI server, call CGI_prefork_server() and pass it a + pointer to your callback function. The SCGI server puts + itself into the background, and forks child processes to handle + web requests. If CGI_prefork_server() returns, then it has + failed. The web server does not automatically start the SCGI + server program, so you must start it. You control which user + account runs the SCGI server and what privileges it has.

+ +

To terminate the SCGI server, send the SIGTERM signal + to the parent process and it sends the signal on to its child + processes and exits. The parent process writes its process ID in + a file if you pass the file name to CGI_prefork_server() in + pidfile.

+ +

The callback function operates very much like a + traditional CGI program, except that it gets called multiple + times. When writing your callback consider the + following.

+ +
    +
  • The callback function should not call exit() + (unless it encounters a serious error). You defeat the purpose + of a persistent process if you exit. The parent process + replaces exited children, so calling exit() does not + cause the SCGI server to fail.
  • + +
  • Be careful to free memory that you have allocated and close + files that you have opened inside the callback function. + Otherwise the process will consume memory and/or file + descriptors with each call to callback, and eventually + fail. It may be too difficult to track down all memory leaks. + You can call CGI_prefork_server() with + maxreq greater than zero, which causes the child process + to exit after handling maxreq requests, which releases + all its resources.
  • + +
  • You may need to handle initialization code differently. + Initializations made before you call + CGI_prefork_server() are inherited by all child + processes. If you open any files or sockets, then the + corresponding file descriptors are inherited and shared. You + may not get the behavior you want when multiple processes share + a file descriptor.
  • + +
  • To initialize each child process individually, place + initialization code in callback inside an if + statement that executes the first time callback is + called. The if statement can test a static + variable and reset it to prevent executing the body of the + if statement again.
  • + +
  • Each SCGI request includes a full set of environment + variables. The SCGI server replaces the entire environment with + these variables before each call to callback. If you + need anything from the original environment, then you should + save it before calling CGI_prefork_server(). If you need + to manipulate the environment, then the standard C function + putenv() allows you to add or modify environment + variables, and the global C variable + extern char **environ; is a pointer to the + current environment.
  • + +
  • If you read standard input directly (rather than using + CGI_get_all() or CGI_get_post()) then use + stdio library functions. In particular you should not + use the read() system call because the SCGI server reads + environment data from standard input using stdio before + calling callback. Use fread() instead.
  • + +
  • The SCGI server uses syslog() to log error messages. + If you do not want the default syslog parameters, then + initialize the logging system with openlog() before + calling CGI_prefork_server(). Use syslog() inside + callback to log any errors.
  • +
+ +

Here is an example SCGI server.

+
+#include <ccgi.h>
+#include <stdio.h>
+#include <syslog.h>
+
+static void
+cgi_callback() {
+    static int first_call = 1;
+    CGI_varlist *varlist;
+
+    if (first_call) {
+        first_call = 0;
+
+        /* initializations for each child process go here */
+
+    }
+    varlist = CGI_get_all(0);
+
+    fputs("Content-type: text/html\r\n\r\n", stdout);
+
+    /* write the rest of the web response to stdout */
+
+    /* free memory and close open files */
+
+    CGI_free_varlist(varlist);
+}
+
+int
+main(int argc, char **argv) {
+
+    /* initializations before forking child processes go here */
+
+    openlog("my-scgi-server", 0, LOG_DAEMON);
+
+    CGI_prefork_server("localhost", 4000, "/var/run/my-scgi-server.pid",
+        /* maxproc */ 100,  /* minidle */ 8,  /* maxidle */ 16,
+        /* maxreq */ 1000, cgi_callback);
+
+    /* if CGI_prefork_server() returns, then it failed */
+
+    return 0;
+}
+
+ + diff --git a/test/ccgi-1.2/examples/Makefile b/test/ccgi-1.2/examples/Makefile new file mode 100644 index 00000000..84fe14e0 --- /dev/null +++ b/test/ccgi-1.2/examples/Makefile @@ -0,0 +1,6 @@ +CFLAGS = -I .. + +dump.cgi: dump.cgi.o + $(CC) -o dump.cgi dump.cgi.o -L .. -lccgi + +dump.cgi.o: dump.cgi.c diff --git a/test/ccgi-1.2/examples/README b/test/ccgi-1.2/examples/README new file mode 100644 index 00000000..cb9b955a --- /dev/null +++ b/test/ccgi-1.2/examples/README @@ -0,0 +1,9 @@ +dump.html is a sample web form that POSTs to dump.cgi. + +dump.cgi displays all the form data and uploads +files to /tmp/cgi-upload-* + +To build dump.cgi: make + +Install dump.cgi and dump.html on a web server and +browse dump.html. diff --git a/test/ccgi-1.2/examples/dump.cgi.c b/test/ccgi-1.2/examples/dump.cgi.c new file mode 100644 index 00000000..91523212 --- /dev/null +++ b/test/ccgi-1.2/examples/dump.cgi.c @@ -0,0 +1,34 @@ +#include +#include + +int +main(int argc, char **argv, char **env) { + CGI_varlist *vl; + const char *name; + CGI_value *value; + int i; + + fputs("Content-type: text/plain\r\n\r\n", stdout); + fputs("Environment:\r\n", stdout); + for (i = 0; env[i] != 0; i++) { + fputs(env[i], stdout); + fputs("\r\n", stdout); + } + + if ((vl = CGI_get_all("/tmp/cgi-upload-XXXXXX")) == 0) { + fputs("CGI_get_all() failed\r\n", stdout); + return 0; + } + fputs("\r\nCGI Variables:\r\n", stdout); + + for (name = CGI_first_name(vl); name != 0; + name = CGI_next_name(vl)) + { + value = CGI_lookup_all(vl, 0); + for (i = 0; value[i] != 0; i++) { + printf("%s [%d] >>%s<<\r\n", name, i, value[i]); + } + } + CGI_free_varlist(vl); + return 0; +} diff --git a/test/ccgi-1.2/examples/dump.html b/test/ccgi-1.2/examples/dump.html new file mode 100644 index 00000000..266bdc22 --- /dev/null +++ b/test/ccgi-1.2/examples/dump.html @@ -0,0 +1,73 @@ + + +C CGI Test Form + + + + +

C CGI Library Test Form

+ +
+ +

+Text Input: +

+ +

+File Uploads: +

+

+File 1:
+File 2: +

+ +

+Check Boxes: +cb1 +cb2 +cb3 +cb4 +

+ +

+Radio Buttons: +rb1 +rb2 +rb3 +rb4 +NONE: +

+ +

+Selection: + +

+ +

+Text Area: + +

+ +

+ + +

+
+ + diff --git a/test/ccgi-1.2/prefork.c b/test/ccgi-1.2/prefork.c new file mode 100644 index 00000000..8b3bff74 --- /dev/null +++ b/test/ccgi-1.2/prefork.c @@ -0,0 +1,468 @@ +/* + * C CGI Library version 1.2 + * + * Author: Stephen C. Losen, University of Virginia + * + * Copyright 2015 Stephen C. Losen. Distributed under the terms + * of the GNU Lesser General Public License (LGPL 2.1) + * + * CGI_prefork_server() is a pre forking SCGI server that handles web + * requests by calling a callback function provided by the user. + * The parent process forks a configurable number of child processes + * to accept web requests. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SCORE_MAX_PROC 10000 +#define SCORE_IDLE 1 +#define SCORE_BUSY 2 + +/* scoreboard structs to keep track of child process state */ + +struct score_state { + pid_t pid; + int state; +}; + +struct score_board { + int numproc; + int numidle; + int minidle; + int maxidle; + int maxproc; + struct score_state proc[1]; +}; + +/* score_new() creates new scoreboard */ + +static struct score_board * +score_new(int maxproc, int minidle, int maxidle) { + struct score_board *scb; + int size = sizeof(*scb) + maxproc * sizeof(scb->proc); + scb = (struct score_board *) malloc(size); + memset(scb, 0, size); + scb->maxproc = maxproc; + scb->minidle = minidle; + scb->maxidle = maxidle; + return scb; +} + +/* score_find() finds process entry and returns its index */ + +static int +score_find(struct score_board *scb, pid_t pid) { + int i; + for (i = 0; i < scb->maxproc; i++) { + if (scb->proc[i].pid == pid) { + return i; + } + } + return -1; +} + +/* score_add() adds a new pid to the scoreboard */ + +static void +score_add(struct score_board *scb, pid_t pid) { + int i = score_find(scb, 0); + if (i >= 0) { + scb->proc[i].pid = pid; + scb->proc[i].state = SCORE_IDLE; + scb->numproc++; + scb->numidle++; + } +} + +/* score_remove() removes a pid from the scoreboard */ + +static void +score_remove(struct score_board *scb, pid_t pid) { + int i = score_find(scb, pid); + if (i >= 0) { + scb->proc[i].pid = 0; + scb->numproc--; + if (scb->proc[i].state == SCORE_IDLE) { + scb->numidle--; + } + } +} + +/* score_update() changes pid state to busy or idle */ + +static void +score_update(struct score_board *scb, struct score_state *st) { + int i = score_find(scb, st->pid); + if (i >= 0 && scb->proc[i].state != st->state) { + scb->proc[i].state = st->state; + if (st->state == SCORE_IDLE) { + scb->numidle++; + } + else { + scb->numidle--; + } + } +} + +/* score_kill() finds an idle process and kills it */ + +static void +score_kill(struct score_board *scb) { + int i; + for (i = 0; i < scb->maxproc; i++) { + if (scb->proc[i].pid != 0 && scb->proc[i].state == SCORE_IDLE) { + kill(scb->proc[i].pid, SIGTERM); + return; + } + } +} + +/* + * setup_sock() opens a TCP socket, binds it to a local address + * specified by host and port, calls listen(), and returns the + * socket. + */ + +static int +setup_sock(const char *host, int port) { + struct sockaddr_in local; + struct hostent *h; + int sock; + + memset(&local, 0, sizeof(local)); + local.sin_family = AF_INET; + local.sin_port = htons(port); + if (host == 0 || *host == 0) { + local.sin_addr.s_addr = INADDR_ANY; + } + else if (!isdigit(host[0]) || + inet_aton(host, &local.sin_addr) == 0) + { + if ((h = gethostbyname(host)) != 0) { + memcpy(&local.sin_addr, h->h_addr_list[0], h->h_length); + } + else { + local.sin_addr.s_addr = INADDR_ANY; + } + } + if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0) { + return sock; + } + if (bind(sock, (struct sockaddr *) &local, sizeof(local)) < 0 || + listen(sock, 5) < 0) + { + close(sock); + return -1; + } + return sock; +} + +/* + * read_env() reads the environment from the SCGI client (httpd). + * We read the length of the environment, which is an ASCII decimal + * string terminated by ':'. We read the environment, which + * is a sequence of null terminated strings where each pair of strings + * is an environment variable name and value. After the environment + * is a comma. (Any remaining input is POST data, which we leave + * unread.) We replace the null between each variable name and value + * with '=' and we return an array of char * pointers to the + * environment variable strings. + */ + +static char ** +read_env(void) { + char **env; + char *buf, *p; + int i, k, len; + + /* read length of environment data, eg, "175:" */ + + for (len = 0; isdigit(k = getc(stdin)); ) { + len = len * 10 + k - '0'; + } + if (k != ':' || len < 4) { + return 0; + } + + /* read the environment strings */ + + buf = (char *) malloc(len); + if (fread(buf, 1, len, stdin) != len || getc(stdin) != ',') { + free(buf); + return 0; + } + + /* replace null between each name and value with '=' */ + + for (i = k = 0; i < len; i++) { + if (buf[i] == 0 && (++k & 1)) { + buf[i] = '='; + } + } + + /* build environment array */ + + k = k / 2 + 1; + env = (char **) malloc(k * sizeof(*env)); + p = buf; + for (i = k = 0; i < len; i++) { + if (buf[i] == 0) { + env[k++] = p; + p = buf + i + 1; + } + } + env[k] = 0; + return env; +} + +/* terminate() sets a flag when we get SIGTERM */ + +static int terminate_flag = 0; + +static void +terminate(int sig) { + terminate_flag = 1; +} + +/* child_handler() sets a flag when a child exits */ + +static int child_exited = 0; + +static void +child_handler(int sig) { + child_exited = 1; +} + +/* + * set_handler() sets a signal handler. We use this instead of + * signal() because it is more portable and system calls tend to + * be restarted when signal() is used. In particular we need + * read() to return if we catch a signal. + */ + +static int +set_handler(int sig, void (*handler)(int sig)) { + struct sigaction action; + action.sa_handler = handler; + action.sa_flags = 0; + sigemptyset(&action.sa_mask); + return sigaction(sig, &action, 0); +} + +/* + * CGI_prefork_server() pre forks child processes to handle SCGI requests. + * The parent process opens a TCP socket that listens on a local + * address specified by "host" and "port". Each child handles "maxreq" + * requests and exits. If maxreq <= 0 then maxreq is unlimited. The + * parent process creates or destroys child processes to maintain between + * "minidle" and "maxidle" idle children, and no more than "maxproc" + * total children. To handle a request, a child 1) accepts a connection + * on the listen socket from the SCGI client, 2) redirects stdin and + * stdout to the socket, 3) reads the environment from the SCGI client, + * 4) calls the "callback" function, 5) closes the socket and restores + * the environment. Child processes notify the parent when they + * become busy or idle by writing messages to a pipe. + */ + +void +CGI_prefork_server(const char *host, int port, const char *pidfile, + int maxproc, int minidle, int maxidle, int maxreq, + void (*callback)(void)) +{ + int i, sock, fd; + struct score_state message; + struct score_board *scb; + pid_t pid; + FILE *fp; + int pfd[2]; + char **realenv, **tmpenv; + char *tmpbuf; + extern char **environ; + + /* sanity check arguments */ + + if (callback == 0) { + syslog(LOG_ERR, "CGI_prefork_server(): null callback " + "function pointer"); + return; + } + + if (minidle <= 0) { + minidle = 2; + } + if (maxidle <= minidle) { + maxidle = minidle + 2; + } + if (maxproc <= 0) { + maxproc = maxidle; + } + if (maxproc > SCORE_MAX_PROC) { + maxproc = SCORE_MAX_PROC; + } + syslog(LOG_INFO, "CGI_prefork_server(): maxproc = %d, minidle = %d, " + "maxidle = %d, maxreq = %d", maxproc, minidle, maxidle, maxreq); + + /* parent puts self into the background */ + + if (fork() != 0) { + _exit(0); + } + setsid(); + set_handler(SIGTERM, terminate); + set_handler(SIGCHLD, child_handler); + freopen("/dev/null", "r", stdin); + freopen("/dev/null", "w", stdout); + + /* write our pid to pidfile */ + + if (pidfile != 0 && *pidfile != 0 && + (fp = fopen(pidfile, "w")) != 0) + { + fprintf(fp, "%d\n", getpid()); + fclose(fp); + } + + /* create child process scoreboard */ + + scb = score_new(maxproc, minidle, maxidle); + + /* parent opens the listen socket, children accept() connections */ + + if ((sock = setup_sock(host, port)) < 0) { + syslog(LOG_ERR, "CGI_prefork_server(): setup_sock() failed: %m"); + return; + } + + /* open pipe to receive messages from child processes */ + + pipe(pfd); + + /* parent manages child processes */ + + for (;;) { + + /* fork child if necessary */ + + if (scb->numidle < scb->minidle && scb->numproc < scb->maxproc) { + if ((pid = fork()) == 0) { + break; + } + else if (pid > 0) { + score_add(scb, pid); + continue; + } + else { + syslog(LOG_ERR, "CGI_prefork_server(): fork() failed: %m"); + if (scb->numproc == 0) { + return; + } + } + } + + /* + * read status message from child. The read() call returns with + * an error if we catch SIGCHLD or SIGTERM. + */ + + if (child_exited == 0 && terminate_flag == 0 && + read(pfd[0], &message, sizeof(message)) == sizeof(message)) + { + score_update(scb, &message); + } + + /* kill everything and exit if we got SIGTERM */ + + if (terminate_flag != 0) { + set_handler(SIGTERM, SIG_IGN); + kill(0, SIGTERM); /* kill process group */ + while(wait(0) >= 0) + ; + exit(0); + } + + /* kill idle child if necessary */ + + if (scb->numidle > scb->maxidle) { + score_kill(scb); + } + + /* wait for exited children */ + + child_exited = 0; + while ((pid = waitpid(-1, 0, WNOHANG)) > 0) { + score_remove(scb, pid); + } + } + + /* child handles maxreq requests and exits */ + + set_handler(SIGTERM, SIG_DFL); + set_handler(SIGCHLD, SIG_DFL); + close(pfd[0]); + message.pid = getpid(); + realenv = environ; + + for (i = 0; i < maxreq || maxreq <= 0; i++) { + + /* accept connection from SCGI client (httpd) */ + + if ((fd = accept(sock, 0, 0)) < 0) { + syslog(LOG_ERR, "CGI_prefork_server(): accept() failed: %m"); + break; + } + + /* notify parent we are busy */ + + message.state = SCORE_BUSY; + write(pfd[1], &message, sizeof(message)); + + /* redirect stdin and stdout to socket */ + + dup2(fd, 0); + dup2(fd, 1); + close(fd); + + /* read environment and call callback */ + + if ((tmpenv = read_env()) != 0) { + tmpbuf = tmpenv[0]; + environ = tmpenv; + callback(); + } + else { + fputs("Content-type: text/plain\r\n\r\n" + "CGI_prefork_server() could not read environment.\r\n", + stdout); + syslog(LOG_ERR, "CGI_prefork_server(): could not read " + "environment"); + } + + /* close socket and restore environment */ + + freopen("/dev/null", "r", stdin); /* closes socket */ + freopen("/dev/null", "w", stdout); + environ = realenv; + if (tmpenv != 0) { + free(tmpbuf); + free(tmpenv); + } + + /* notify parent we are idle */ + + message.state = SCORE_IDLE; + write(pfd[1], &message, sizeof(message)); + } + _exit(0); +} diff --git a/test/ccgi-1.2/t/Makefile b/test/ccgi-1.2/t/Makefile new file mode 100644 index 00000000..541e72f3 --- /dev/null +++ b/test/ccgi-1.2/t/Makefile @@ -0,0 +1,14 @@ +# set OPENSSL_LIB to the directory containing openssl libcrypto + +OPENSSL_LIB = /usr/lib + +CFLAGS = -I .. +LDFLAGS = -L .. -L $(OPENSSL_LIB) + +all: test.sh test + ./test.sh + +test: test.o ../libccgi.a + $(CC) -o test $(LDFLAGS) test.o -lccgi -lcrypto + +test.o: test.c ../ccgi.h diff --git a/test/ccgi-1.2/t/test.c b/test/ccgi-1.2/t/test.c new file mode 100644 index 00000000..f7ef10b4 --- /dev/null +++ b/test/ccgi-1.2/t/test.c @@ -0,0 +1,141 @@ +#include +#include +#include + +static void +dumplist(CGI_varlist *vl) { + const char *name; + CGI_value *value; + int i; + + for (name = CGI_first_name(vl); name != 0; + name = CGI_next_name(vl)) + { + value = CGI_lookup_all(vl, 0); + for (i = 0; value[i] != 0; i++) { + printf("%s [%d] >>%s<<\n", name, i, value[i]); + } + } +} + +int +main(int argc, char **argv) { + CGI_varlist *vl; + char *p; + CGI_value *value; + int i; + + if (strcmp(argv[1], "CGI_get_cookie") == 0) { + vl = CGI_get_cookie(0); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_get_query") == 0) { + vl = CGI_get_query(0); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_decode_query") == 0) { + vl = CGI_decode_query(0, argv[2]); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_get_post") == 0) { + vl = CGI_get_post(0, 0); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "upload") == 0) { + vl = CGI_get_post(0, "./cgi-upload-XXXXXX"); + value = CGI_lookup_all(vl, "upload"); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_add_var") == 0) { + vl = 0; + for (i = 2; i + 1 < argc; i += 2) { + vl = CGI_add_var(vl, argv[i], argv[i + 1]); + } + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_encode_varlist") == 0) { + vl = 0; + for (i = 2; i + 1 < argc; i += 2) { + vl = CGI_add_var(vl, argv[i], argv[i + 1]); + } + dumplist(vl); + p = CGI_encode_varlist(vl, ".-_"); + fputs(p, stdout); + fputc('\n', stdout); + return 0; + } + + + if (strcmp(argv[1], "CGI_encode_url") == 0) { + p = CGI_encode_url(argv[2], ".-_"); + fputs(p, stdout); + fputc('\n', stdout); + p = CGI_decode_url(p); + fputs(p, stdout); + fputc('\n', stdout); + return 0; + } + + if (strcmp(argv[1], "CGI_encode_query") == 0) { + p = CGI_encode_query(".-_", argv[2], argv[3], argv[4], argv[5], + argv[6], argv[7], argv[8], argv[9], argv[10], argv[11], + argv[12], argv[13], argv[14], argv[15], argv[16], argv[17], + argv[18], argv[19], argv[20], argv[21], (char *)0); + fputs(p, stdout); + fputc('\n', stdout); + vl = CGI_decode_query(0, p); + dumplist(vl); + return 0; + } + + if (strcmp(argv[1], "CGI_encode_base64") == 0) { + p = argv[2]; + p = CGI_encode_base64(p, strlen(p)); + fputs(p, stdout); + fputc('\n', stdout); + p = (char *) CGI_decode_base64(p, &i); + fwrite(p, i, 1, stdout); + fputc('\n', stdout); + return 0; + } + + if (strcmp(argv[1], "CGI_encode_hex") == 0) { + p = argv[2]; + p = CGI_encode_hex(p, strlen(p)); + fputs(p, stdout); + fputc('\n', stdout); + p = (char *) CGI_decode_hex(p, &i); + fwrite(p, i, 1, stdout); + fputc('\n', stdout); + return 0; + } + + if (strcmp(argv[1], "CGI_encode_entity") == 0) { + p = CGI_encode_entity(argv[2]); + fputs(p, stdout); + fputc('\n', stdout); + return 0; + } + if (strcmp(argv[1], "CGI_encrypt") == 0) { + const char *pw = "This is my C CGI test password"; + p = argv[2]; + p = CGI_encrypt(p, strlen(p), pw); + printf("enc len = %d\n", strlen(p)); + p = CGI_decrypt(p , &i, pw); + printf("dec len = %d %s\n", i, p); + return 0; + } + return 0; +} diff --git a/test/ccgi-1.2/t/test.sh b/test/ccgi-1.2/t/test.sh new file mode 100755 index 00000000..df70a2c0 --- /dev/null +++ b/test/ccgi-1.2/t/test.sh @@ -0,0 +1,337 @@ +#! /bin/sh + +PATH=/bin:/usr/bin + +# Regression tests for the CGI library +# Usage: ./test.sh [ -r ] +# The -r option causes the script to edit itself and renumber +# the tests in case any are added or removed. + +# renumber the tests in this script + +renumber() { + cp $0 $0.old && + awk '/^TEST=[0-9].* ######/ { + printf("TEST=%d ########################################\n", ++i); + next } + { print }' $0.old > $0 && + /bin/rm $0.old + exit +} + +check () { + echo + if diff -c expected result; then + echo "Test $TEST success" + else + echo "Test $TEST failure" + fi +} + +test "X$1" = X-r && renumber + +TEST=1 ######################################## + +# Testing CGI_get_query + +Q='one=This+is+a+test&two=%22Bob+Smith%22+%3Cbob%40gmail.com%3E&' +Q=$Q'varnovalue&eq=un=encoded=eq==&one=second+value&the+end' + +QUERY_STRING=$Q ./test CGI_get_query > result 2>&1 + +cat > expected <<'E-O-F' +one [0] >>This is a test<< +one [1] >>second value<< +two [0] >>"Bob Smith" << +varnovalue [0] >><< +eq [0] >>un=encoded=eq==<< +the end [0] >><< +E-O-F + +check + +TEST=2 ######################################## + +# Testing CGI_get_post + +CONTENT_TYPE=application/x-www-form-urlencoded \ +CONTENT_LENGTH=60 \ +./test CGI_get_post > result 2>&1 <<'E-O-F' +one=This+is+a+test&two=%22Bob+Smith%22+%3Cbob%40gmail.com%3E +E-O-F + +cat > expected <<'E-O-F' +one [0] >>This is a test<< +two [0] >>"Bob Smith" << +E-O-F + +check + +TEST=3 ######################################## + +# Testing CGI_get_post multipart/form-data + +CONTENT_TYPE='multipart/form-data; boundary=----12345' \ +./test CGI_get_post > result 2>&1 <<'E-O-F' +------12345 +Content-Disposition: form-data; name="textvar1" + + +------12345 +Content-Disposition: form-data; name="doublevar" + +Text data here +------12345 +Content-Disposition: form-data; name="cbvar1" + +cb #1 +------12345 +Content-Disposition: form-data; name="cbvar1" + +cb #3 +------12345 +Content-Disposition: form-data; name="rbvar1" + +rb #3 +------12345 +Content-Disposition: form-data; name="selvar1" + +selval1 +------12345 +Content-Disposition: form-data; name="selvar1" + +selval3 +------12345 +Content-Disposition: form-data; name="selvar1" + +selval4 +------12345 +Content-Disposition: form-data; name="selvar1" + +selval7 +------12345 +Content-Disposition: form-data; name="textarea1" + +Initial Line 1 +Initial Line 2 +Added Line +Another Line +------12345 +Content-Disposition: form-data; name="doublevar" + +OK +------12345 +Content-Disposition: form-data; name="hidden" + +This is three +lines of hidden +form data +------12345-- +E-O-F + +cat > expected <<'E-O-F' +textvar1 [0] >><< +doublevar [0] >>Text data here<< +doublevar [1] >>OK<< +cbvar1 [0] >>cb #1<< +cbvar1 [1] >>cb #3<< +rbvar1 [0] >>rb #3<< +selvar1 [0] >>selval1<< +selvar1 [1] >>selval3<< +selvar1 [2] >>selval4<< +selvar1 [3] >>selval7<< +textarea1 [0] >>Initial Line 1 +Initial Line 2 +Added Line +Another Line<< +hidden [0] >>This is three +lines of hidden +form data<< +E-O-F + +check + +TEST=4 ######################################## + +# Testing CGI_get_cookie + +HTTP_COOKIE='cookie1=cookie1data; cookie2=cookie2data; cookie3=more-stuff;' \ +./test CGI_get_cookie > result 2>&1 + +cat > expected <<'E-O-F' +cookie1 [0] >>cookie1data<< +cookie2 [0] >>cookie2data<< +cookie3 [0] >>more-stuff<< +E-O-F + +check + +TEST=5 ######################################## + +# Testing upload + +/bin/rm -f cgi-upload-?????? +CONTENT_TYPE='multipart/form-data; boundary=----12345' \ +./test upload > result 2> result <<'E-O-F' +------12345 +Content-Disposition: form-data; name="var1" + +Value # 1 +------12345 +Content-Disposition: form-data; name="upload"; filename="input" + +This is a test uploaded file +Here is the second line. +The quick brown fox jumped over the lazy dog +(or was that a hog?) +Now is the time for +all good men +to come to the aid of +their party! + +------12345-- +E-O-F + +F=` echo ./cgi-upload-?????? ` + +cat $F >> result + +/bin/rm -f $F + +cat > expected << E-O-F +var1 [0] >>Value # 1<< +upload [0] >>$F<< +upload [1] >>input<< +This is a test uploaded file +Here is the second line. +The quick brown fox jumped over the lazy dog +(or was that a hog?) +Now is the time for +all good men +to come to the aid of +their party! +E-O-F + +check + +TEST=6 ######################################## + +# Testing CGI_add_var + +./test CGI_add_var one 'first value' two 'value # 2' \ + three '3rd-var' two 200% > result 2>&1 + +cat > expected <<'E-O-F' +one [0] >>first value<< +two [0] >>value # 2<< +two [1] >>200%<< +three [0] >>3rd-var<< +E-O-F + +check + +TEST=7 ######################################## + +# Testing CGI_encode_varlist + +./test CGI_encode_varlist one 'first value' two 'value # 2' \ + three '3rd-var' two 200% > result 2>&1 + +cat > expected <<'E-O-F' +one [0] >>first value<< +two [0] >>value # 2<< +two [1] >>200%<< +three [0] >>3rd-var<< +one=first+value&two=value+%23+2&two=200%25&three=3rd-var +E-O-F + +check + +TEST=8 ######################################## + +# Testing CGI_encode_query + +./test CGI_encode_query one 'first value' two 'value # 2' \ + three '3rd-var' two 200% > result 2>&1 + +cat > expected <<'E-O-F' +one=first+value&two=value+%23+2&three=3rd-var&two=200%25 +one [0] >>first value<< +two [0] >>value # 2<< +two [1] >>200%<< +three [0] >>3rd-var<< +E-O-F + +check + +TEST=9 ######################################## + +# Testing CGI_encode_url + +./test CGI_encode_url '/etc/passwd holds Unix(TM) accounts.' > result 2>&1 + +cat > expected <<'E-O-F' +%2Fetc%2Fpasswd+holds+Unix%28TM%29+accounts. +/etc/passwd holds Unix(TM) accounts. +E-O-F + +check + + +TEST=10 ######################################## + +# Testing CGI_encode_entity + +./test CGI_encode_entity \ +'if (i > 20 && i < 200) { + fputs("OK\n", stdout); +}' > result 2>&1 +cat > expected <<'E-O-F' +if (i > 20 && i < 200) { fputs("OK\n", stdout); } +E-O-F + +check + +TEST=11 ######################################## + +# Testing CGI_encode_base64 + +./test CGI_encode_base64 'Now is the time for the quick brown fox, ...' \ + > result 2>&1 +cat > expected <<'E-O-F' +Tm93IGlzIHRoZSB0aW1lIGZvciB0aGUgcXVpY2sgYnJvd24gZm94LCAuLi4= +Now is the time for the quick brown fox, ... +E-O-F + +check + +TEST=12 ######################################## + +# Testing CGI_encode_hex + +./test CGI_encode_hex 'Now is the time for the quick brown fox, ...' \ + > result 2>&1 +cat > expected <<'E-O-F' +4E6F77206973207468652074696D6520666F722074686520717569636B2062726F776E20666F782C202E2E2E +Now is the time for the quick brown fox, ... +E-O-F + +check + +TEST=13 ######################################## + +# Testing CGI_encrypt + +./test CGI_encrypt 'Now is the time for the quick brown fox, ...' \ + > result 2>&1 +cat > expected <<'E-O-F' +enc len = 120 +dec len = 44 Now is the time for the quick brown fox, ... +E-O-F + +check + + +# clean up + +/bin/rm -f result expected cgi-upload-?????? diff --git a/test/client.pem b/test/client.pem new file mode 100644 index 00000000..f0a7885e --- /dev/null +++ b/test/client.pem @@ -0,0 +1,45 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEAwV5xaK7ez2/TX7vSgJ0a3YbZj2l1VQ2rMzqO1Id01xlWbF/U +rebwhAdVtWcT9R6RaBTPDGaILkV38u77M2BxIHX4MSnR6WezoA2bGMgvt3+tq2N6 +q+xkj57vwBEqedBjscVtFkoWtsX8pKwtNlMB1NvTa8p5+BNsvpvzaDX+51+FotId +wvieQfQYgFg36HpOtOyyIV31rZ/5+qtoce8gU6wApHxmovTnQPoduNM6fOUJCHDd +Lz90EeBREtoTVgoWcKvQoCEwJQSBmeDZgkA8Q1OYmbYoS12tIyi8rTkseRj5BvPH +iXfNmHFKliAjvlsml5qI44I9DoagPubTf6qR5wIDAQABAoIBACZ6VZTgH0Ql22jU +ZhnjqUHloIsyEAABvUxvXZaa8bwPtavREfAc4UVUdFCpl0YSdBrC8URlbrnOZwT3 +WxMpILm139JgoP2R/iNeMbunsh8QkA1nuTRW0NfnZ4vPnqUou33XbFKgIY7zLMfT +3xdNQzMJHzP20Xh03RG81J2rCPMfLScTRo2XxcSxmhhS/p2WLk6pnmMHiNgYGGwX +gcdK5lIVjMMNxgcltC30x90v0o0GDRM8/+wua+/vfn8rr3iudv9IHzL8xIzpi6NY +CXJ8Kxd6Jtgsr3Boj5i6Mqi3Q/Trxt+rIA4bKAFXxwcp4+GmRIJtQFFiTWXpLCPC +tLT4CHECgYEA7iCbrGjWHJ4QsUWUGrGlw1/sQ0SIv9BdZm8RydHzpRVtQOi+YOuU +i6raVaXWzUBKgKcs/htVjAMTiePs/yhlU/MGXivz6uTX/nrD7ISJImmK2K50hgUe ++UBnFKmBMVaNxD9RFWPJkfmNXfW7nBkqSa9CxlBcYPuOcPtZDqRl+gkCgYEAz+HX +8wh3SHKb1cAI+o4caclpUTpGa9/zW4k+7gOh72WCKaqxTNvBvNyZGdXc9t5ToDSf +xxsDXWG10lcHBIGLj4QBEoSWp9I43lid5swY3mCo7CjTl+1l03IfDNaC6CYQFp5p +ZnKlsQUwR38t/uiyZpnnicCAZjqIfJbeQ5jD6G8CgYB8ufmwQa08ihJmN/KOVNRl +VF31EfWquqHhYHXpxx2eL23tXLszGtHQoioASIANPAqJ/oaTho+1aXsXc5oUP/1r +DlUciFsXgswb0APFY9pMewmt2xrPg+koVvJnIS25QQO6cguvb3gKDLNeLrMY3RmI +RNNt+nOYnMqMJSsNf1CmuQKBgQCiCZxWaCbyZcNqncFh7BvhqYlaM15o/6ulkhln +VZWIEUugRtjk2/bry9fa94TBORNeMSbKABhjVaJwTj2+GWw7dd2QHaGBNq/1QIX0 +POq1jAqf6kLkjbttUes6CosHgYPQ3bGylXLpxO2ZDV1A8Qj+SMDd8xsilEWHN+IQ +NqeeKQKBgQDe4c7VVG+WvRRKshTh8+tjzc9nXKE2AWgwnw729SMFZO/WqX2FPp2C +7C99XJTVBsCBy8VzuyaojeTKkag0YL3v6UTZYUeyu0YTHGQ33WVPaqdCAo840nmG +ttwHVqshB9c67HHiYOOFt1VmT3xW6x6yympUyRqR0L+BZ1wOS3h2vQ== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIIC6DCCAdACBRQJQlZlMA0GCSqGSIb3DQEBBQUAMDgxCzAJBgNVBAMTAm5zMQsw +CQYDVQQKEwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkR1YmxpbjAeFw0xNDA4 +MzAxOTA3NDRaFw0yNDA4MjcxOTA3NDRaMDgxCzAJBgNVBAMTAm5zMQswCQYDVQQK +EwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkdhbHdheTCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBAMFecWiu3s9v01+70oCdGt2G2Y9pdVUNqzM6jtSH +dNcZVmxf1K3m8IQHVbVnE/UekWgUzwxmiC5Fd/Lu+zNgcSB1+DEp0elns6ANmxjI +L7d/ratjeqvsZI+e78ARKnnQY7HFbRZKFrbF/KSsLTZTAdTb02vKefgTbL6b82g1 +/udfhaLSHcL4nkH0GIBYN+h6TrTssiFd9a2f+fqraHHvIFOsAKR8ZqL050D6HbjT +OnzlCQhw3S8/dBHgURLaE1YKFnCr0KAhMCUEgZng2YJAPENTmJm2KEtdrSMovK05 +LHkY+Qbzx4l3zZhxSpYgI75bJpeaiOOCPQ6GoD7m03+qkecCAwEAATANBgkqhkiG +9w0BAQUFAAOCAQEAJ+wZ/IgAF5LIu0yOfJlaFRJLunKHZENigiVjYvkTdM7NI3O2 +1AZGY4O8H5Fs3YT5ZY3vas/n6IwWTk3o/JSPXojMFo82XkbI1k2cm3oLtwgEGN3p +s5yFsjZE3H7fQJ9wHIzESBPHFY6dwwgMsNENuAM2zkwFpbAkisKhjK+EyUCXauok +7zJY6RVPMaNojsje4iE/SBtSOnK/9WDBAgpCznHrSChJmKs4FsU7ZTO+Dg+0vQln +l8/yBcEGAFe0GA2D9NvZKH5IoNmitvtU9zdNDK4dzC3Q+C28IjW5jE8peDFtdGs1 +P0u4kRxmb4UH1DchgoWlZjL2lSFScJ7L4xY2aQ== +-----END CERTIFICATE----- diff --git a/test/data/auth/a.txt b/test/data/auth/a.txt new file mode 100644 index 00000000..45b983be --- /dev/null +++ b/test/data/auth/a.txt @@ -0,0 +1 @@ +hi diff --git a/test/data/auth/passwords.txt b/test/data/auth/passwords.txt new file mode 100644 index 00000000..9266a5a9 --- /dev/null +++ b/test/data/auth/passwords.txt @@ -0,0 +1 @@ +joe:foo.com:77bbd68c7bc5cd7b6bb33a19e2fc7007 diff --git a/test/data/dav/a.txt b/test/data/dav/a.txt new file mode 100644 index 00000000..45b983be --- /dev/null +++ b/test/data/dav/a.txt @@ -0,0 +1 @@ +hi diff --git a/test/data/dav/hidden_file.txt b/test/data/dav/hidden_file.txt new file mode 100644 index 00000000..e69de29b diff --git a/test/data/dir_no_index/some_file b/test/data/dir_no_index/some_file new file mode 100644 index 00000000..e69de29b diff --git a/test/data/dir_with_index/index.html b/test/data/dir_with_index/index.html new file mode 100644 index 00000000..19102815 --- /dev/null +++ b/test/data/dir_with_index/index.html @@ -0,0 +1 @@ +foo \ No newline at end of file diff --git a/test/data/dummy.xml b/test/data/dummy.xml new file mode 100644 index 00000000..46711958 --- /dev/null +++ b/test/data/dummy.xml @@ -0,0 +1 @@ +:-) diff --git a/test/data/mg_init.js b/test/data/mg_init.js new file mode 100644 index 00000000..4b11ed4a --- /dev/null +++ b/test/data/mg_init.js @@ -0,0 +1,12 @@ +Http = { + onrcv: function(c, req) { + if (req.uri === '/data/js') { + // Send reply. We need to construct everything, headers and body + mg_send(c, 'HTTP/1.0 200 OK\r\n\r\n'); + mg_send(c, JSON.stringify(req)); + + // Let Mongoose know we've handled the connection + return true; + } + } +} diff --git a/test/data/multipart.txt b/test/data/multipart.txt new file mode 100644 index 00000000..e6784af6 --- /dev/null +++ b/test/data/multipart.txt @@ -0,0 +1,13 @@ +------WebKitFormBoundaryh3pBS3C5nni6XxR4 +Content-Disposition: form-data; name="file1"; filename="keyword.c" +Content-Type: application/octet-stream + +file1 data + +------WebKitFormBoundaryh3pBS3C5nni6XxR4 +Content-Disposition: form-data; name="file2"; filename="stdarg.c" +Content-Type: application/octet-stream + +file2 data + +------WebKitFormBoundaryh3pBS3C5nni6XxR4-- diff --git a/test/data/range.txt b/test/data/range.txt new file mode 100644 index 00000000..e5047362 --- /dev/null +++ b/test/data/range.txt @@ -0,0 +1,9 @@ +Faith of consciousness is freedom +Faith of feeling is weakness +Faith of body is stupidity. +Love of consciousness evokes the same in response +Love of feeling evokes the opposite +Love of body depends only on type and polarity. +Hope of consciousness is strength +Hope of feelings is slavery +Hope of body is disease. diff --git a/test/data/rewrites/foo.com/index.html b/test/data/rewrites/foo.com/index.html new file mode 100644 index 00000000..5524d4a4 --- /dev/null +++ b/test/data/rewrites/foo.com/index.html @@ -0,0 +1 @@ +foo_root diff --git a/test/data/rewrites/msg.txt b/test/data/rewrites/msg.txt new file mode 100644 index 00000000..153d1940 --- /dev/null +++ b/test/data/rewrites/msg.txt @@ -0,0 +1 @@ +works diff --git a/test/data/ssi/f1.txt b/test/data/ssi/f1.txt new file mode 100644 index 00000000..78981922 --- /dev/null +++ b/test/data/ssi/f1.txt @@ -0,0 +1 @@ +a diff --git a/test/data/ssi/index.shtml b/test/data/ssi/index.shtml new file mode 100644 index 00000000..8695e673 --- /dev/null +++ b/test/data/ssi/index.shtml @@ -0,0 +1 @@ + diff --git a/test/data/ssi/nested.shtml b/test/data/ssi/nested.shtml new file mode 100644 index 00000000..f5e2a3e6 --- /dev/null +++ b/test/data/ssi/nested.shtml @@ -0,0 +1,3 @@ + + + diff --git a/test/index_cgi.c b/test/index_cgi.c new file mode 100644 index 00000000..86caa661 --- /dev/null +++ b/test/index_cgi.c @@ -0,0 +1,85 @@ +#include +#include +#include + +#include + +extern char **environ; + +#ifdef _WIN32 +#define EOL "\n" +#else +#define EOL "\r\n" +#endif + +int alpha_cmp(const void *a, const void *b) { + return strcmp(*(const char **) a, *(const char **) b); +} + +int main(void) { + puts("Content-Type: text/html" EOL "Status: 201 Created" EOL EOL); + + puts("
" EOL "

Environment

" EOL); + { + const char *sorted_env[500]; + size_t i, num_env; + + for (num_env = 0; environ[num_env] != 0; num_env++) { + sorted_env[num_env] = environ[num_env]; + } + qsort(sorted_env, num_env, sizeof(const char *), alpha_cmp); + + for (i = 0; i < num_env; i++) { + printf("E: %s" EOL, sorted_env[i]); + } + } + puts(EOL "

Query string

" EOL); + { + const char *k; + CGI_varlist *vl = CGI_get_query(NULL); + + for (k = CGI_first_name(vl); k != NULL; k = CGI_next_name(vl)) { + printf("Q: %s=%s" EOL, k, CGI_lookup(vl, k)); + } + + CGI_free_varlist(vl); + } + puts(EOL "

Form variables

" EOL); + { + const char *k; + CGI_varlist *vl = CGI_get_post(NULL, NULL); + + for (k = CGI_first_name(vl); k != NULL; k = CGI_next_name(vl)) { + printf("P: %s=%s" EOL, k, CGI_lookup(vl, k)); + } + + CGI_free_varlist(vl); + } + puts(EOL "
" EOL); + return 0; +} + +/* Some functions for libccgi that are missing on Windows (VC6). */ +#ifdef _WIN32 +static int lowercase(const char *s) { + return tolower(*(const unsigned char *) s); +} + +int strncasecmp(const char *s1, const char *s2, size_t len) { + int diff = 0; + + if (len > 0) do { + diff = lowercase(s1++) - lowercase(s2++); + } while (diff == 0 && s1[-1] != '\0' && --len > 0); + + return diff; +} + +int strcasecmp(const char *s1, const char *s2) { + return strncasecmp(s1, s2, (size_t) ~0); +} + +int mkstemp(char *template) { + return -1; /* Not used by us. */ +} +#endif diff --git a/test/server.pem b/test/server.pem new file mode 100644 index 00000000..5ae2c837 --- /dev/null +++ b/test/server.pem @@ -0,0 +1,45 @@ +-----BEGIN RSA PRIVATE KEY----- +MIIEpAIBAAKCAQEA1mONQ0hAXOL9lb15Pz4fqXXNHREsF3a7/NoMJdQDclx0+a32 +MhuHcO6R7Fhsc0mZMuzbmAFLMmIIgXPMKQBZLoA12yCBlZPyKFoWUhFrLa3gUjO6 +CZlBKqkUVEACpVrQ41ihapeeUHa0uryt3tXwMn2/853yzi1uciGYi4ULTy3yTE/n +qRIVJLiBDSC9WNFLg26f/W4YRW7tANOk2b/W/Ws9B/n7vNDgHG7Lpd38YTpFhhXT +n3xlt/VcczkQhW79Moh6/lY6sLg6H15EjHKHeTn8t9BRm+qYi/CvC258YF/Qz/qK +agSsLT/3FrQ6+aQgg/Eyao0IWAql49PQNxuwPQIDAQABAoIBAQC5y3S1BnyhAyb5 +Ckd1g4U0+x5TPnqTqxanvuAgOGj0RyQo7ZYbPrhWKqrTxJ3YG8Rk2dhFF3nvo/3z +EkOwlNi07++8g6NJ2flW9xu469eSsslg8+saPnK3Yeh4SzD/1ICLRlg9ZECTQwzF +eJbGM2oCl/AuVIgEHmNFDdCBuT9f0b7j3/Z3aK3lKzqzBYQgZ5fd8UxT+Kn4oAuS +cLr3lQT1s6xZOAYn7O2GvXEC+yMMbvm0a97MdwSpQez1WcE9YxtCgAWwn5EmSXlh +296iLtbaM1wgYOykJUOUoSgijf8pUfotk4Zj/y1KPHXePgAlyGCtE1zasiYb5K+5 +LuajD++BAoGBAPpKWLNzQBwQLiFaJgt6bLOxlEUR+EnjdFePDPJtyCCCiKJiKO5c +Z5s/FT1JDQawouhjQwYqT48hbGBPjWRHkSkzB7+cg6FVSKkQRYX2TsSFvN+KCu32 +oSgDV9cFo68v1csoZIQ41TtHC82db4OTv9MPUe3Glujnep1TOTwspAM1AoGBANtH +i+HWKOxOm7f/R2VX1ys9UjkAK+msac512XWSLAzBs7NFnB7iJ7m3Bh3ydb1ZiTgW +l6bIdoT8TLPYNIXJ6uohhxPU5h3v81PHqIuJMBtmHCQjq3nxeH9mOsfjOFvS1cQa +At45F9pK/5sQpOkkaBGSv8jXUFIKBEDBErourVHpAoGAK0gSAK4sZu3xXDkfnRqF +k6lgr3UFD5nys3V8UqvjUKPiBtqco2N9Ux5ciOWKCB8hfLg1jephKaoo+JqpI68w +jgRSEbN6G7sIvpueuiS2yEssNyfC7hWZFrdFSFykSpYmDWSlxSuizAZkJyFTeFhj +cpcSnuCZlhr5XB1ZJ2u8zQUCgYEAke5QgpCDFZjO+ynR+vj1gppBwRuDHfUXSUaW +3S7VT/wNOq6F0uvRYkASuxVkFAqlToWCkYVxktlRtpKZibwyMXT0r1cNejj5Z/VF +Du/S6zkOW2K9uN7hwW9oiSSHmlx61RI2fGvkmus0pp7yERKgi6ltJx1cH+z4nZug +efWcdRkCgYBy+XdmsxgNZOunlSC6VZiD0Ve/VFrCtKPWUivKDAZZPKl0T/1tbTwb +I/N4zTF82jx88rDz+6jN5nOy9qbSR5TeCy6WlBesTvXm49awr5jSK3WkcLgmO+JI +Zr2ozCBhUG6RvVsUPp2kXEsmwZMV/e9faFAlIXeJhKum6hZmfOgodg== +-----END RSA PRIVATE KEY----- +-----BEGIN CERTIFICATE----- +MIIC6DCCAdACBRQJQlZkMA0GCSqGSIb3DQEBBQUAMDgxCzAJBgNVBAMTAm5zMQsw +CQYDVQQKEwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkR1YmxpbjAeFw0xNDA4 +MzAxOTA3NDNaFw0yNDA4MjcxOTA3NDNaMDgxCzAJBgNVBAMTAm5zMQswCQYDVQQK +EwJuczELMAkGA1UEBhMCSUUxDzANBgNVBAcTBkdhbHdheTCCASIwDQYJKoZIhvcN +AQEBBQADggEPADCCAQoCggEBANZjjUNIQFzi/ZW9eT8+H6l1zR0RLBd2u/zaDCXU +A3JcdPmt9jIbh3DukexYbHNJmTLs25gBSzJiCIFzzCkAWS6ANdsggZWT8ihaFlIR +ay2t4FIzugmZQSqpFFRAAqVa0ONYoWqXnlB2tLq8rd7V8DJ9v/Od8s4tbnIhmIuF +C08t8kxP56kSFSS4gQ0gvVjRS4Nun/1uGEVu7QDTpNm/1v1rPQf5+7zQ4Bxuy6Xd +/GE6RYYV0598Zbf1XHM5EIVu/TKIev5WOrC4Oh9eRIxyh3k5/LfQUZvqmIvwrwtu +fGBf0M/6imoErC0/9xa0OvmkIIPxMmqNCFgKpePT0DcbsD0CAwEAATANBgkqhkiG +9w0BAQUFAAOCAQEAoVXK97WA24tp3JyPBJKr28gFSUtOBNDPdY8atWaqw7PwUIIM +qhs3BTag96tgSoaISRwRphz2LM1Cl+QlItYXySAnxPKrUsA0S6DlxnA6Hq3s2wTR +6yIT7oDUDKcWkVQcQmuNGdfxCvZXkCih9lnQn++xHcuVn9mZmjXW2xk42ljDTZCp +CM29betpcmuho6sFXsBhY7WjQWg7UpRZat0bOwleS4fsePebMKrnr/6cq4bVw59U +XvhSFBlLoGMYteJ82fOYH6pUO1hiPr6ww5d819LPcJEcRpcxCdQZqIq680Kp7+GY +0wkyOYr0gkNwWVP7IUZ0FExaQ/s54g71Kd0OgA== +-----END CERTIFICATE----- diff --git a/test/test.mk b/test/test.mk new file mode 100644 index 00000000..13695aea --- /dev/null +++ b/test/test.mk @@ -0,0 +1,129 @@ +# Copyright (c) 2014 Cesanta Software Limited +# All rights reserved +# +# = Requires: +# +# - SRC_DIR = path to directory with source files +# - AMALGAMATED_SOURCES = path to amalgamated C file(s) +# - PROG = name of main unit test source file +# +# - CFLAGS = default compiler flags +# - LDFLAGS = default linker flags +# +# = Parameters: +# +# - V=1 -> show commandlines of executed commands +# - TEST_FILTER -> test name (substring match) +# - CMD -> run wrapped in cmd (e.g. make test_cxx CMD=lldb) +# +# = Useful targets +# +# - compile: perform a very fast syntax check for all dialects +# - presubmit: suggested presubmit tests +# - cpplint: run the linter +# - lcov: generate coverage HTML in test/lcov/index.html +# - test_asan: run with AddressSanitizer +# - test_valgrind: run with valgrind + +# OSX clang doesn't build ASAN. Use brew: +# $ brew tap homebrew/versions +# $ brew install llvm36 --with-clang --with-asan +CLANG:=clang-3.6 + +PEDANTIC=$(shell gcc --version 2>/dev/null | grep -q clang && echo -pedantic) + +### + +DIALECTS=cxx ansi c99 c11 +SPECIALS=asan gcov valgrind + +# Each test target might require either a different compiler name +# a compiler flag, or a wrapper to be invoked before executing the test +# they can be overriden here with _ +# +# Vars are: +# - CC: compiler +# - CFLAGS: flags passed to the compiler, useful to set dialect and to disable incompatible tests +# - LDFLAGS: flags passed to the compiler only when linking (e.g. not in syntax only) +# - SOURCES: non-test source files. To be overriden if needs to build on non amalgamated files +# - CMD: command wrapper or env variables required to run the test binary + +CMD=MallocLogFile=/dev/null + +CC_cxx=$(CXX) +CFLAGS_cxx=-x c++ + +CFLAGS_ansi=-ansi +CFLAGS_c99=$(PEDANTIC) -std=c99 + +# Path to `gcov` binary +GCOV?=gcov + +CFLAGS_gcov=$(PEDANTIC) -std=c99 -fprofile-arcs -ftest-coverage +#SOURCES_gcov=$(addprefix $(SRC_DIR)/, $(SOURCES)) +SOURCES_gcov=$(AMALGAMATED_SOURCES) + +CC_asan=$(CLANG) +CFLAGS_asan=-fsanitize=address -fcolor-diagnostics -std=c99 -DNO_DNS_TEST -UMG_ENABLE_SSL +CMD_asan=ASAN_SYMBOLIZER_PATH=/usr/bin/llvm-symbolizer ASAN_OPTIONS=allocator_may_return_null=1,symbolize=1 $(CMD) + +CMD_valgrind=valgrind + +### + +SHELL := /bin/bash + +SUFFIXES=$(DIALECTS) $(SPECIALS) + +ALL_PROGS=$(foreach p,$(SUFFIXES),$(PROG)-$(p)) +ALL_TESTS=$(foreach p,$(SUFFIXES),test_$(p)) +SHORT_TESTS=$(foreach p,$(DIALECTS),test_$(p)) + +all: clean compile $(SHORT_TESTS) coverage +alltests: $(ALL_TESTS) lcov cpplint + +# currently both valgrind and asan tests are failing for some test cases +# it's still useful to be able to run asan/valgrind on some specific test cases +# but we don't enforce them for presubmit until they are stable again. +presubmit: $(SHORT_TESTS) cpplint + +.PHONY: clean clean_coverage lcov valgrind docker cpplint +ifneq ($(V), 1) +.SILENT: $(ALL_PROGS) $(ALL_TESTS) +endif + +compile: + @make $(foreach p,$(DIALECTS),$(PROG)-$(p)) CFLAGS_EXTRA="$(CFLAGS_EXTRA) -fsyntax-only" LDFLAGS= + +# HACK: cannot have two underscores +$(PROG)-%: Makefile $(TEST_SOURCES) $(or $(SOURCES_$*), $(AMALGAMATED_SOURCES)) data/cgi/index.cgi + @echo -e "CC\t$(PROG)_$*" + $(or $(CC_$*), $(CC)) $(CFLAGS_$*) $(TEST_SOURCES) $(or $(SOURCES_$*), $(AMALGAMATED_SOURCES)) -o $(PROG)_$* $(CFLAGS) $(LDFLAGS) + +$(ALL_TESTS): test_%: Makefile $(PROG)-% + @echo -e "RUN\t$(PROG)_$* $(TEST_FILTER)" + @$(or $(CMD_$*), $(CMD)) ./$(PROG)_$* $(TEST_FILTER) + +coverage: Makefile clean_coverage test_gcov + @echo -e "RUN\tGCOV" + @$(GCOV) -p $(notdir $(TEST_SOURCES)) $(notdir $(GCOV_SOURCES)) >/dev/null + +test_leaks: Makefile + $(MAKE) test_valgrind CMD_valgrind="$(CMD_valgrind) --leak-check=full" + +lcov: clean coverage + @echo -e "RUN\tlcov" + @lcov -q -o lcov.info -c -d . 2>/dev/null + @genhtml -q -o lcov lcov.info + +cpplint: + @echo -e "RUN\tcpplint" + @cpplint.py --verbose=0 --extensions=c,h $(SRC_DIR)/*.[ch] 2>&1 >/dev/null| grep -v "Done processing" | grep -v "file excluded by" + +clean: clean_coverage + @echo -e "CLEAN\tall" + @rm -rf $(PROG) $(PROG)_* lcov.info *.txt *.exe *.obj *.o a.out *.pdb *.opt $(EXTRA_CLEAN_TARGETS) + +clean_coverage: + @echo -e "CLEAN\tcoverage" + @rm -rf *.gc* *.dSYM index.html diff --git a/test/unit_test.c b/test/unit_test.c new file mode 100644 index 00000000..084afa18 --- /dev/null +++ b/test/unit_test.c @@ -0,0 +1,5617 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see . + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in . + */ + +#include "mongoose/mongoose.h" +#include "mongoose/src/internal.h" +#include "unit_test.h" +#include "common/test_util.h" +#include "common/cs_md5.h" +#include "tuna/dispatcher.h" + +#if defined(_MSC_VER) && _MSC_VER >= 1900 +#include +#endif + +#if defined(__STDC_VERSION__) && __STDC_VERSION__ < 199901L && !defined(WIN32) +#define __func__ "" +#endif + +#define FETCH_BUF_SIZE (1024 * 16) + +#define HTTP_PORT "45772" +#define LOOPBACK_IP "127.0.0.1" +#define LISTENING_ADDR LOOPBACK_IP ":" HTTP_PORT + +static const char *s_argv_0 = NULL; +static struct mg_serve_http_opts s_http_server_opts; +static int s_listening_port = 23456; + +#define TEST_MG_MALLOC malloc +#define TEST_MG_CALLOC calloc + +#ifndef intptr_t +#define intptr_t long +#endif + +void *(*test_malloc)(size_t) = TEST_MG_MALLOC; +void *(*test_calloc)(size_t, size_t) = TEST_MG_CALLOC; + +void *failing_malloc(size_t size) { + (void) size; + return NULL; +} + +void *failing_calloc(size_t count, size_t size) { + (void) count; + (void) size; + return NULL; +} + +static char *read_file(const char *path, size_t *size) { + FILE *fp; + struct stat st; + char *data = NULL; + if ((fp = mg_fopen(path, "rb")) != NULL && !fstat(fileno(fp), &st)) { + *size = st.st_size; + data = (char *) malloc(*size); + fread(data, 1, *size, fp); + fclose(fp); + } + return data; +} + +void msleep(int millis) { +#if defined(_WIN32) + Sleep(millis); +#else + usleep(millis * 1000); +#endif +} + +static struct mg_iface *test_iface = NULL; + +static void init_test_connection(struct mg_connection *nc) { + memset(nc, 0, sizeof(*nc)); + nc->iface = test_iface; +} + +static struct mg_connection *create_test_connection() { + struct mg_connection *nc = (struct mg_connection *) calloc(1, sizeof(*nc)); + init_test_connection(nc); + return nc; +} + +void mg_destroy_conn(struct mg_connection *conn, int destroy_if); +static void destroy_test_connection(struct mg_connection *nc) { + mg_destroy_conn(nc, 0 /* destroy_if */); +} + +static const char *test_mbuf(void) { + struct mbuf io; + const char *data = "TEST"; + const char *prefix = "MY"; + const char *big_prefix = "Some long prefix: "; + size_t old_size; + + mbuf_init(&io, 0); + ASSERT(io.buf == NULL); + ASSERT_EQ(io.len, 0); + ASSERT_EQ(io.size, 0); + mbuf_free(&io); + ASSERT(io.buf == NULL); + ASSERT_EQ(io.len, 0); + ASSERT_EQ(io.size, 0); + + mbuf_init(&io, 10); + ASSERT(io.buf != NULL); + ASSERT_EQ(io.len, 0); + ASSERT_EQ(io.size, 10); + mbuf_free(&io); + ASSERT(io.buf == NULL); + ASSERT_EQ(io.len, 0); + ASSERT_EQ(io.size, 0); + + mbuf_init(&io, 10); + ASSERT_EQ(mbuf_append(&io, NULL, 0), 0); + /* test allocation failure */ + ASSERT_EQ(mbuf_append(&io, NULL, 1125899906842624), 0); + + ASSERT_EQ(mbuf_append(&io, data, strlen(data)), strlen(data)); + + mbuf_resize(&io, 2); + ASSERT_EQ(io.size, 10); + ASSERT_EQ(io.len, strlen(data)); + + ASSERT_EQ(mbuf_insert(&io, 0, prefix, strlen(prefix)), strlen(prefix)); + ASSERT_EQ(io.size, 10); + ASSERT_EQ(io.len, strlen(data) + strlen(prefix)); + + ASSERT_EQ(mbuf_insert(&io, 0, big_prefix, strlen(big_prefix)), + strlen(big_prefix)); + ASSERT_EQ(io.size, MBUF_SIZE_MULTIPLIER * + (strlen(big_prefix) + strlen(prefix) + strlen(data))); + ASSERT_STREQ_NZ(io.buf, "Some long prefix: MYTEST"); + + old_size = io.size; + ASSERT_EQ(mbuf_insert(&io, strlen(big_prefix), data, strlen(data)), + strlen(data)); + ASSERT_EQ(io.size, old_size); + ASSERT_STREQ_NZ(io.buf, "Some long prefix: TESTMYTEST"); + + /* test allocation failure */ + ASSERT_EQ(mbuf_insert(&io, 0, NULL, 1125899906842624), 0); + + /* test overflow */ + ASSERT_EQ(mbuf_insert(&io, 0, NULL, -1), 0); + mbuf_free(&io); + return NULL; +} + +static void eh1(struct mg_connection *nc, int ev, void *ev_data) { + struct mbuf *io = &nc->recv_mbuf; + + switch (ev) { + case MG_EV_CONNECT: { + int res = *((int *) ev_data); + if (res == 0) { + mg_printf(nc, "%d %s there", *(int *) ev_data, "hi"); + } else { + sprintf((char *) nc->user_data, "connect failed! %d", res); + } + break; + } + case MG_EV_RECV: { + if (nc->listener != NULL) { + mg_printf(nc, "%d", (int) io->len); + mbuf_remove(io, io->len); + } else if (io->len > 0) { + sprintf((char *) nc->user_data, "%sok!", + (io->len == 2 && memcmp(io->buf, "10", 2) == 0) ? "" : "NOT "); + DBG(("%s", (const char *) nc->user_data)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } + break; + } + default: + break; + } +} + +#define S_PEM "server.pem" +#define C_PEM "client.pem" +#define CA_PEM "ca.pem" + +static const char *test_mgr_with_ssl(int use_ssl) { + char addr[100] = "127.0.0.1:0", ip[sizeof(addr)], buf[100] = "", + short_addr[5]; + struct mg_mgr mgr; + struct mg_connection *nc; + struct mg_bind_opts bopts; + int port, port2; +#if !MG_ENABLE_SSL + (void) use_ssl; +#endif + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + + memset(&bopts, 0, sizeof(bopts)); +#if MG_ENABLE_SSL + if (use_ssl) { + bopts.ssl_cert = S_PEM; + bopts.ssl_ca_cert = CA_PEM; + } +#endif + ASSERT((nc = mg_bind_opt(&mgr, addr, eh1, bopts)) != NULL); + port2 = htons(nc->sa.sin.sin_port); + ASSERT(port2 > 0); + + mg_sock_to_str(nc->sock, addr, sizeof(addr), 3); + ASSERT_EQ(sscanf(addr, "%[^:]:%d", ip, &port), 2); + ASSERT_STREQ(ip, "127.0.0.1"); + ASSERT_EQ(port, port2); + + mg_sock_to_str(nc->sock, short_addr, sizeof(short_addr), 3); +#ifndef _WIN32 + ASSERT_STREQ(short_addr, ""); +#else + /* We use `inet_ntoa` in Windows, so, can get partial result */ + ASSERT_STREQ(short_addr, "127."); +#endif + + ASSERT((nc = mg_connect(&mgr, addr, eh1)) != NULL); +#if MG_ENABLE_SSL + if (use_ssl) { + ASSERT(mg_set_ssl(nc, C_PEM, CA_PEM) == NULL); + } +#endif + nc->user_data = buf; + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "ok!"); + buf[0] = '\0'; + +#if MG_ENABLE_SSL + if (use_ssl) { + struct mg_connect_opts opts; + memset(&opts, 0, sizeof(opts)); + opts.user_data = buf; + opts.ssl_cert = C_PEM; + opts.ssl_ca_cert = CA_PEM; + opts.ssl_server_name = "*"; /* TODO(rojer): Test this too. */ + ASSERT((nc = mg_connect_opt(&mgr, addr, eh1, opts)) != NULL); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "ok!"); + buf[0] = '\0'; + } +#endif + + mg_mgr_free(&mgr); + return NULL; +} + +static const char *test_mgr(void) { + return test_mgr_with_ssl(0); +} + +#if MG_ENABLE_SSL +void *run_manager(void *param) { + struct mg_mgr *mgr = (struct mg_mgr *) param; + while (((intptr_t) mgr->user_data) == 0) { + mg_mgr_poll(mgr, 1000); + } + mg_mgr_free(mgr); + free(mgr); + return NULL; +} + +static const char *test_ssl(void) { + return test_mgr_with_ssl(1); +} + +/* + * unfortunately krypton has no BIO so it's hard to test this way + * We still set MG_ENABLE_SSL so that vc6 has a binary to build + */ +#ifdef OPENSSL_VERSION_NUMBER +static void eh_hello_server(struct mg_connection *nc, int ev, void *ev_data) { + (void) ev_data; + if (ev == MG_EV_ACCEPT) mg_printf(nc, "hello"); +} + +static const char *test_modern_crypto(void) { + char addr[100] = "127.0.0.1:8000"; + struct mg_mgr *mgr = (struct mg_mgr *) malloc(sizeof(*mgr)); + struct mg_connection *nc; + int port; + + mg_mgr_init(mgr, NULL); + + ASSERT((nc = mg_bind(mgr, addr, eh_hello_server)) != NULL); + port = htons(nc->sa.sin.sin_port); + ASSERT(port > 0); + ASSERT(mg_set_ssl(nc, S_PEM, NULL /* no client certs */) == NULL); + mg_sock_to_str(nc->sock, addr, sizeof(addr), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + mg_start_thread(run_manager, mgr); + + { + char buf[100]; +/* For older OpenSSL version we have to allow older digests. Then it still tests + * DH. */ +#if OPENSSL_VERSION_NUMBER < 0x10000000 + const char *ciphers = "DH:!ADH:AES:MD5:SHA1"; +#else + const char *ciphers = "DH:!ADH:AES:!MD5:!SHA1"; +#endif + + SSL_CTX *ctx = NULL; + BIO *bio = NULL; + SSL *ssl = NULL; + + ctx = SSL_CTX_new(SSLv23_client_method()); + ASSERT(ctx != NULL); + SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL); + SSL_CTX_set_verify_depth(ctx, 1); + /* + * These are pretty restrictive settings and should satisfy e.g. + * Chrome's "modern cryptography" requirements. + */ + ASSERT_EQ(SSL_CTX_set_cipher_list(nc->ssl_ctx, ciphers), 1); + ASSERT_EQ(SSL_CTX_load_verify_locations(ctx, CA_PEM, NULL), 1); + bio = BIO_new_ssl_connect(ctx); + ASSERT(bio != NULL); + BIO_get_ssl(bio, &ssl); + ASSERT(ssl != NULL); + SSL_set_mode(ssl, SSL_MODE_AUTO_RETRY); + BIO_set_conn_hostname(bio, addr); + ASSERT_EQ(BIO_do_connect(bio), 1); + ASSERT_EQ(BIO_do_handshake(bio), 1); + ASSERT_EQ(SSL_get_verify_result(ssl), X509_V_OK); + ASSERT_EQ(BIO_read(bio, buf, 5), 5); + ASSERT_STREQ_NZ(buf, "hello"); + BIO_free_all(bio); + SSL_CTX_free(ctx); + } + + mgr->user_data = (void *) 1; + return NULL; +} +#endif /* OPENSSL_VERSION_NUMBER */ +#endif /* MG_ENABLE_SSL */ + +static const char *test_to64(void) { + ASSERT_EQ(to64("0"), 0); + ASSERT_EQ(to64(""), 0); + ASSERT_EQ(to64("123"), 123); + ASSERT_EQ(to64("-34"), -34); + ASSERT_EQ(to64("3566626116"), 3566626116U); + return NULL; +} + +static const char *test_check_ip_acl(void) { + uint32_t ip = 0x01020304; + ASSERT_EQ(mg_check_ip_acl(NULL, ip), 1); + ASSERT_EQ(mg_check_ip_acl("", ip), 1); + ASSERT_EQ(mg_check_ip_acl("invalid", ip), -1); + ASSERT_EQ(mg_check_ip_acl("-0.0.0.0/0", ip), 0); + ASSERT_EQ(mg_check_ip_acl("-0.0.0.0/0,+1.0.0.0/8", ip), 1); + ASSERT_EQ(mg_check_ip_acl("-0.0.0.0/0,+1.2.3.4", ip), 1); + ASSERT_EQ(mg_check_ip_acl("-0.0.0.0/0,+1.0.0.0/16", ip), 0); + return NULL; +} + +static const char *test_parse_uri(void) { + struct mg_str uri_out; + struct mg_str scheme, user_info, host, path, query, fragment; + unsigned int port; + { + struct mg_str uri = MG_MK_STR("foo"); + ASSERT_EQ(mg_parse_uri(uri, NULL, NULL, NULL, NULL, NULL, NULL, NULL), 0); + } + + { + struct mg_str uri = MG_MK_STR("http://user:pw@host:80/foo?bar#baz"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, "user:pw"); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_MG_STREQ(query, "bar"); + ASSERT_MG_STREQ(fragment, "baz"); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host:80/foo?bar"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_MG_STREQ(query, "bar"); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host:80/foo#baz"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, "baz"); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host:80/foo"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host/foo"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("http://host:80"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "http"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("tcp://1.2.3.4:5678"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "tcp"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "1.2.3.4"); + ASSERT_EQ(port, 5678); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("tcp://[::1]:234"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "tcp"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "[::1]"); + ASSERT_EQ(port, 234); + ASSERT_EQ(path.len, 0); + ASSERT_EQ(query.len, 0); + ASSERT_EQ(fragment.len, 0); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("tcp://[::1]"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "tcp"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "[::1]"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("tcp://[::1"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + -1); + } + + { + struct mg_str uri = MG_MK_STR("host:80"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, ""); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 80); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("1.2.3.4:56789"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, ""); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "1.2.3.4"); + ASSERT_EQ(port, 56789); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("[::1]:2345"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, ""); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "[::1]"); + ASSERT_EQ(port, 2345); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + const struct mg_str uri = MG_MK_STR("[::1]"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, ""); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "[::1]"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, ""); + ASSERT_MG_STREQ(query, ""); + ASSERT_MG_STREQ(fragment, ""); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("host/foo"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, ""); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "host"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, "/foo"); + ASSERT_EQ(query.len, 0); + ASSERT_EQ(fragment.len, 0); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + { + struct mg_str uri = MG_MK_STR("https://a.com/foo/x@y.z/fw.zip"); + ASSERT_EQ(mg_parse_uri(uri, &scheme, &user_info, &host, &port, &path, + &query, &fragment), + 0); + ASSERT_MG_STREQ(scheme, "https"); + ASSERT_MG_STREQ(user_info, ""); + ASSERT_MG_STREQ(host, "a.com"); + ASSERT_EQ(port, 0); + ASSERT_MG_STREQ(path, "/foo/x@y.z/fw.zip"); + ASSERT_EQ(query.len, 0); + ASSERT_EQ(fragment.len, 0); + + ASSERT_EQ(mg_assemble_uri(&scheme, &user_info, &host, port, &path, &query, + &fragment, 0, &uri_out), + 0); + ASSERT_MG_STREQ(uri_out, uri.p); + free((void *) uri_out.p); + } + + return NULL; +} + +static const char *test_assemble_uri(void) { + struct mg_str scheme, path, uri; + { + ASSERT_EQ(mg_assemble_uri(NULL, NULL, NULL, 0, NULL, NULL, NULL, 0, &uri), + 0); + ASSERT_MG_STREQ(uri, ""); + } + + { + scheme = mg_mk_str("file"); + path = mg_mk_str("/foo/bar"); + ASSERT_EQ( + mg_assemble_uri(&scheme, NULL, NULL, 0, &path, NULL, NULL, 0, &uri), 0); + ASSERT_MG_STREQ(uri, "file:///foo/bar"); + free((void *) uri.p); + } + + { + scheme = mg_mk_str("file"); + path = mg_mk_str("/foo/.././bar/baz"); + ASSERT_EQ( + mg_assemble_uri(&scheme, NULL, NULL, 0, &path, NULL, NULL, 0, &uri), 0); + ASSERT_MG_STREQ(uri, "file:///foo/.././bar/baz"); + free((void *) uri.p); + ASSERT_EQ( + mg_assemble_uri(&scheme, NULL, NULL, 0, &path, NULL, NULL, 1, &uri), 0); + ASSERT_MG_STREQ(uri, "file:///bar/baz"); + free((void *) uri.p); + } + + return NULL; +} + +/* TODO(mkm) port these test cases to the new async parse_address */ +static const char *test_parse_address(void) { + static const char *valid[] = { + "1", + "1.2.3.4:1", + "tcp://123", + "udp://0.0.0.0:99", +#ifndef _WIN32 /* No /etc/hosts on Windows. */ + "tcp://localhost:99", +#endif + ":8080", +#if MG_ENABLE_IPV6 + "udp://[::1]:123", + "[3ffe:2a00:100:7031::1]:900", +#endif + NULL + }; + static const int protos[] = { + SOCK_STREAM, + SOCK_STREAM, + SOCK_STREAM, + SOCK_DGRAM, + SOCK_STREAM, + SOCK_STREAM +#if MG_ENABLE_IPV6 + , + SOCK_DGRAM, + SOCK_STREAM +#endif + }; + static const char *need_lookup[] = {"udp://a.com:53", "locl_host:12", NULL}; + static const char *invalid[] = { + "99999", "1k", "1.2.3", "1.2.3.4:", "1.2.3.4:2p", "blah://12", ":123x", + "veeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeery.long:12345", + "udp://missingport", NULL}; + char host[50]; + union socket_address sa; + int i, proto; + + for (i = 0; valid[i] != NULL; i++) { + ASSERT(mg_parse_address(valid[i], &sa, &proto, host, sizeof(host)) > 0); + ASSERT_EQ(proto, protos[i]); + } + + for (i = 0; invalid[i] != NULL; i++) { + ASSERT_EQ(mg_parse_address(invalid[i], &sa, &proto, host, sizeof(host)), + -1); + } + + for (i = 0; need_lookup[i] != NULL; i++) { + ASSERT_EQ(mg_parse_address(need_lookup[i], &sa, &proto, host, sizeof(host)), + 0); + } + + return NULL; +} + +#define CHECK_MGNP(in, exp_rv, exp_out) \ + do { \ + int rv; \ + struct mg_str mgs; \ + char *in_copy = strdup(in); \ + mgs.p = in_copy; \ + mgs.len = strlen(in); \ + rv = mg_normalize_uri_path(&mgs, &mgs); \ + ASSERT_EQ(rv, exp_rv); \ + if (rv > 0) { \ + ASSERT_MG_STREQ(mgs, exp_out); \ + } \ + MG_FREE(in_copy); \ + } while (0) + +static const char *test_mg_normalize_uri_path(void) { + CHECK_MGNP("", 0, ""); + CHECK_MGNP("x", 0, ""); + CHECK_MGNP("/", 1, "/"); + CHECK_MGNP("//", 1, "//"); + CHECK_MGNP("/.", 1, "/"); + CHECK_MGNP("//.", 1, "//"); + CHECK_MGNP("/..", 1, "/"); + CHECK_MGNP("//..", 1, "/"); + CHECK_MGNP("///..", 1, "//"); + CHECK_MGNP("/./", 1, "/"); + CHECK_MGNP("/.//", 1, "//"); + CHECK_MGNP("/./.", 1, "/"); + CHECK_MGNP("/.//.", 1, "//"); + CHECK_MGNP("/foo", 1, "/foo"); + CHECK_MGNP("/foo/", 1, "/foo/"); + CHECK_MGNP("/foo/.", 1, "/foo/"); + CHECK_MGNP("/foo/./", 1, "/foo/"); + CHECK_MGNP("/foo/..", 1, "/"); + CHECK_MGNP("/foo/../", 1, "/"); + CHECK_MGNP("/foo/../bar", 1, "/bar"); + CHECK_MGNP("/foo/../bar/", 1, "/bar/"); + CHECK_MGNP("/foo/../../bar", 1, "/bar"); + CHECK_MGNP("/foo////bar/", 1, "/foo////bar/"); + /* No percent-decoding. */ + CHECK_MGNP("/fo%2fo/%2e%2e/%2e/bar", 1, "/fo%2fo/%2e%2e/%2e/bar"); + return NULL; +} + +#define CHECK_U2LP(u, exp_rv, exp_lp, exp_rem) \ + do { \ + int rv; \ + char *lp = NULL; \ + struct mg_str r; \ + hm.uri.p = u; \ + hm.uri.len = strlen(u); \ + rv = mg_uri_to_local_path(&hm, &opts, &lp, &r); \ + ASSERT_EQ(rv, exp_rv); \ + if (rv == 1) { \ + ASSERT_STREQ(lp, exp_lp); \ + ASSERT_MG_STREQ(r, exp_rem); \ + MG_FREE(lp); \ + } \ + } while (0) + +#ifdef _WIN32 +#define DS "\\" +#else +#define DS "/" +#endif + +static const char *test_mg_uri_to_local_path(void) { + struct http_message hm; + struct mg_serve_http_opts opts; + memset(&hm, 0, sizeof(hm)); + memset(&opts, 0, sizeof(opts)); + opts.document_root = "."; + CHECK_U2LP("/", 1, ".", ""); + CHECK_U2LP("//", 1, ".", ""); + CHECK_U2LP("/.", 1, ".", ""); + CHECK_U2LP("/%2e", 1, ".", ""); + CHECK_U2LP("//.", 1, ".", ""); + CHECK_U2LP("/..", 1, ".", ""); + CHECK_U2LP("//..", 1, ".", ""); + CHECK_U2LP("/./", 1, ".", ""); + CHECK_U2LP("/%2E/", 1, ".", ""); + CHECK_U2LP("/.//", 1, ".", ""); + CHECK_U2LP("/./.", 1, ".", ""); + CHECK_U2LP("/.//.", 1, ".", ""); + CHECK_U2LP("/data", 1, "." DS "data", ""); + CHECK_U2LP("/data/", 1, "." DS "data", ""); + CHECK_U2LP("/data/.", 1, "." DS "data", ""); + CHECK_U2LP("/data/./", 1, "." DS "data", ""); + CHECK_U2LP("/data/..", 1, ".", ""); + CHECK_U2LP("/data/../", 1, ".", ""); + CHECK_U2LP("/data/../data", 1, "." DS "data", ""); + CHECK_U2LP("/data/../../data", 1, "." DS "data", ""); + CHECK_U2LP("/no/such/file", 1, "." DS "no" DS "such" DS "file", ""); + CHECK_U2LP("/data/no/such/file", 1, "." DS "data" DS "no" DS "such" DS "file", + ""); + CHECK_U2LP("/data/dummy.xml/", 1, "." DS "data" DS "dummy.xml", "/"); + CHECK_U2LP("/./data/./%2E/dummy.xml", 1, "." DS "data" DS "dummy.xml", ""); + /* No DIRSEPs in path. */ + CHECK_U2LP("/data%2Fdummy%2Exml/in%2Ffo/", 0, "", ""); + /* Ok in PATH_INFO. */ + CHECK_U2LP("/data/dummy%2Exml/in%2Ffo/", 1, "." DS "data" DS "dummy.xml", + "/in%2Ffo/"); + /* Test rewrites */ + opts.url_rewrites = "/foo=/bar,/=/xxx"; + CHECK_U2LP("/", 1, "/xxx", ""); + CHECK_U2LP("/yyy", 1, "/xxx" DS "yyy", ""); + CHECK_U2LP("/foo/baz", 1, "/bar" DS "baz", ""); + /* /foo does not match - must fall on the boundary, / matches. */ + CHECK_U2LP("/foobario/baz", 1, "/xxx" DS "foobario" DS "baz", ""); + opts.url_rewrites = "**=/xxx"; + CHECK_U2LP("/", 1, "/xxx", ""); + CHECK_U2LP("/yyy", 1, "/xxx", ""); + hm.header_names[0] = mg_mk_str("Host"); + hm.header_values[0] = mg_mk_str("example.org"); + opts.url_rewrites = "@EXAMPLE.org=/test"; + CHECK_U2LP("/hello", 1, "/test" DS "hello", ""); + return NULL; +} + +static const char *test_mg_url_encode(void) { + const struct mg_str encode_me = + MG_MK_STR("I'm a.little_tea-pot,here's$my;spout~oink(oink)oink/!"); + struct mg_str encoded = mg_url_encode(encode_me); + ASSERT_MG_STREQ( + encoded, "I%27m%20a.little_tea-pot,here%27s$my;spout~oink(oink)oink/%21"); + free((void *) encoded.p); + return NULL; +} + +static void connect_fail_cb(struct mg_connection *nc, int ev, void *p) { + switch (ev) { + case MG_EV_CONNECT: + /* On connection success, set flag 1, else set 4 */ + *(int *) nc->user_data |= *(int *) p == 0 ? 1 : 4; + break; + case MG_EV_CLOSE: + *(int *) nc->user_data |= 2; + break; + } +} + +static void ev_handler_empty(struct mg_connection *nc, int ev, void *p) { + (void) nc; + (void) ev; + (void) p; +} + +static const char *test_connection_errors(void) { + struct mg_mgr mgr; + struct mg_bind_opts bopts; + struct mg_connect_opts copts; + struct mg_connection *nc; + const char *error_string; + int data = 0; + + mg_mgr_init(&mgr, NULL); + + memset(&bopts, 0, sizeof(bopts)); + bopts.error_string = &error_string; + + ASSERT(mg_bind_opt(&mgr, "blah://12", NULL, bopts) == NULL); + ASSERT_STREQ(error_string, "handler is required"); + + ASSERT(mg_bind_opt(&mgr, "blah://12", ev_handler_empty, bopts) == NULL); + ASSERT_STREQ(error_string, "cannot parse address"); + + ASSERT(mg_bind_opt(&mgr, "tcp://8.8.8.8:88", ev_handler_empty, bopts) == + NULL); + ASSERT_STREQ(error_string, "failed to open listener"); + +#if MG_ENABLE_SSL + bopts.ssl_cert = S_PEM; + ASSERT(mg_bind_opt(&mgr, "udp://:0", ev_handler_empty, bopts) == NULL); + ASSERT_STREQ(error_string, "SSL for UDP is not supported"); + bopts.ssl_cert = "no_such_file"; + ASSERT(mg_bind_opt(&mgr, "tcp://:0", ev_handler_empty, bopts) == NULL); + ASSERT_STREQ(error_string, "Invalid SSL cert"); + bopts.ssl_cert = NULL; + bopts.ssl_ca_cert = "no_such_file"; + ASSERT(mg_bind_opt(&mgr, "tcp://:0", ev_handler_empty, bopts) == NULL); + ASSERT_STREQ(error_string, "Invalid SSL CA cert"); +#endif + + memset(&copts, 0, sizeof(copts)); + copts.error_string = &error_string; + +#if 0 + copts.error_string = &error_string; + ASSERT(mg_connect_opt(&mgr, "tcp://255.255.255.255:0", NULL, copts) == NULL); + ASSERT_STREQ(error_string, "cannot connect to socket"); + + copts.user_data = &data; + ASSERT(mg_connect_opt(&mgr, "tcp://255.255.255.255:0", connect_fail_cb, + copts) == NULL); + ASSERT_STREQ(error_string, "cannot connect to socket"); + /* handler isn't invoked when it fails synchronously */ + ASSERT_EQ(data, 0); +#endif + + data = 0; + copts.user_data = &data; + ASSERT((nc = mg_connect_opt(&mgr, "tcp://does.not.exist:8080", + connect_fail_cb, copts)) != NULL); + + /* handler is invoked when it fails asynchronously */ + poll_until(&mgr, 5, c_int_eq, &data, (void *) 6); + ASSERT_EQ(data, 6); + + /* mg_bind() does not use MG_CALLOC, but async resolver does */ + test_calloc = failing_calloc; +#ifndef _WIN32 + ASSERT(mg_connect(&mgr, "some.domain.needs.async.resolv:777", NULL) == NULL); + ASSERT(mg_bind(&mgr, ":4321", NULL) == NULL); +#endif + test_calloc = TEST_MG_CALLOC; + +#if MG_ENABLE_SSL + copts.ssl_cert = "no_such_file"; + ASSERT((nc = mg_connect_opt(&mgr, "tcp://google.com:80", connect_fail_cb, + copts)) == NULL); + ASSERT_STREQ(error_string, "Invalid SSL cert"); + copts.ssl_cert = NULL; + copts.ssl_ca_cert = "no_such_file"; + ASSERT((nc = mg_connect_opt(&mgr, "tcp://google.com:80", connect_fail_cb, + copts)) == NULL); + ASSERT_STREQ(error_string, "Invalid SSL CA cert"); +#endif + + mg_mgr_free(&mgr); + return NULL; +} + +static int avt(char **buf, size_t buf_size, const char *fmt, ...) { + int result; + va_list ap; + va_start(ap, fmt); + result = mg_avprintf(buf, buf_size, fmt, ap); + va_end(ap); + return result; +} + +static const char *test_alloc_vprintf(void) { + char buf[5], *p = buf; + + ASSERT_EQ(avt(&p, sizeof(buf), "%d", 123), 3); + ASSERT(p == buf); + ASSERT_STREQ(p, "123"); + + ASSERT_EQ(avt(&p, sizeof(buf), "%d", 123456789), 9); + ASSERT(p != buf); + ASSERT_STREQ(p, "123456789"); + free(p); + + return NULL; +} + +static const char *test_socketpair(void) { + sock_t sp[2]; + static const char foo[] = "hi there"; + char buf[20]; + + ASSERT_EQ(mg_socketpair(sp, SOCK_DGRAM), 1); + ASSERT(sizeof(foo) < sizeof(buf)); + + /* Send string in one direction */ + ASSERT_EQ(send(sp[0], foo, sizeof(foo), 0), sizeof(foo)); + ASSERT_EQ(recv(sp[1], buf, sizeof(buf), 0), sizeof(foo)); + ASSERT_STREQ(buf, "hi there"); + + /* Now in opposite direction */ + ASSERT_EQ(send(sp[1], foo, sizeof(foo), 0), sizeof(foo)); + ASSERT_EQ(recv(sp[0], buf, sizeof(buf), 0), sizeof(foo)); + ASSERT_STREQ(buf, "hi there"); + + closesocket(sp[0]); + closesocket(sp[1]); + + return NULL; +} + +static void ev_timer_handler(struct mg_connection *c, int ev, void *ev_data) { + (void) ev_data; + switch (ev) { + case MG_EV_TIMER: + (*(int *) c->user_data)++; + break; + case MG_EV_CLOSE: + /* Make sure we're alive. Make the ASSERTs fail if we're here. */ + (*(int *) c->user_data) += 100; + break; + } +} + +static const char *test_timer(void) { + struct mg_mgr m; + struct mg_connection *c; + double begin, end; + int n = 0, i; + + mg_mgr_init(&m, NULL); + ASSERT((c = mg_add_sock(&m, INVALID_SOCKET, ev_timer_handler)) != NULL); + c->user_data = &n; + + /* MG_EV_TIMER should not fire - we did not set it up */ + mg_mgr_poll(&m, 1); + ASSERT_EQ(n, 0); + + /* + * Now, set it up. Make sure MG_EV_TIMER event fires. + * Also, it brings forward the poll timeout. + */ + ASSERT_EQ(mg_set_timer(c, mg_time() + 0.1), 0.0); + begin = mg_time(); + /* + * Windows is a bit sloppy about select() timeouts, so it may take + * a couple iterations for the timer to actually fire. + */ + for (i = 0; n != 1 && i < 5; i++) { + mg_mgr_poll(&m, 1000); + } + end = mg_time(); + ASSERT_EQ(n, 1); + ASSERT_LT(end - begin, 0.9); + ASSERT_GT(end - begin, 0.09); + + /* Make sure that timer is reset - second time it does not fire */ + ASSERT_EQ(c->ev_timer_time, 0.0); + mg_mgr_poll(&m, 1); + ASSERT_EQ(n, 1); + + c->flags |= MG_F_CLOSE_IMMEDIATELY; + mg_mgr_poll(&m, 1); + ASSERT_EQ(n, 101); + + /* Async resolver codepath */ + n = 0; + ASSERT((c = mg_connect(&m, "awful.sad:1234", ev_timer_handler)) != NULL); + c->user_data = &n; + mg_set_timer(c, 1); + mg_mgr_poll(&m, 1); + ASSERT_EQ(n, 101); + + mg_mgr_free(&m); + + return NULL; +} + +struct simple_data { + int num_accept; + int num_connect; + int num_recv; + int num_send; + int num_close; + int num_timer; + char to_send[100]; + char data_rcvd[100]; + char fail[200]; + struct simple_data *sclient_data; + struct mg_connection *sclient_nc; +}; + +#ifndef __APPLE__ +static void count_events(struct simple_data *d, int ev) { + switch (ev) { + case MG_EV_POLL: + break; + case MG_EV_ACCEPT: + d->num_accept++; + break; + case MG_EV_CONNECT: + d->num_connect++; + break; + case MG_EV_RECV: + d->num_recv++; + break; + case MG_EV_SEND: + d->num_send++; + break; + case MG_EV_CLOSE: + d->num_close++; + break; + case MG_EV_TIMER: + d->num_timer++; + break; + default: { + char msg[100]; + sprintf(msg, "(unexpected event: %d)", ev); + strcat(d->fail, msg); + } + } +} + +static void do_send(struct simple_data *d, struct mg_connection *nc) { + if (d->to_send[0] != '\0') { + mg_printf(nc, "%s", d->to_send); + } +} + +static void do_recv(struct simple_data *d, struct mg_connection *nc, + void *ev_data) { + if (*((int *) ev_data) != (int) nc->recv_mbuf.len) { + char msg[100]; + sprintf(msg, "(num recv wrong: %d vs %d)", *((int *) ev_data), + (int) strlen(d->to_send)); + strcat(d->fail, msg); + return; + } + strncat(d->data_rcvd, nc->recv_mbuf.buf, nc->recv_mbuf.len); + mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len); +} + +static void check_sent(struct simple_data *d, struct mg_connection *nc, + void *ev_data) { + if (*((int *) ev_data) != (int) strlen(d->to_send)) { + char msg[100]; + sprintf(msg, "(num sent wrong: %d vs %d)", *((int *) ev_data), + (int) strlen(d->to_send)); + strcat(d->fail, msg); + } else { + d->to_send[0] = '\0'; + } + if (nc->send_mbuf.len != 0) strcat(d->fail, "(send buf not empty)"); +} + +static void cb_client(struct mg_connection *nc, int ev, void *ev_data) { + struct simple_data *d = (struct simple_data *) nc->user_data; + count_events(d, ev); + switch (ev) { + case MG_EV_CONNECT: + do_send(d, nc); + break; + case MG_EV_SEND: + check_sent(d, nc, ev_data); + break; + case MG_EV_RECV: + do_recv(d, nc, ev_data); + break; + } +} + +static void cb_sclient(struct mg_connection *nc, int ev, void *ev_data) { + struct simple_data *d = (struct simple_data *) nc->user_data; + count_events(d, ev); + switch (ev) { + case MG_EV_RECV: { + do_recv(d, nc, ev_data); + do_send(d, nc); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + } + case MG_EV_SEND: { + check_sent(d, nc, ev_data); + break; + } + } +} + +static void cb_server(struct mg_connection *nc, int ev, void *ev_data) { + struct simple_data *d = (struct simple_data *) nc->user_data; + (void) ev_data; + if (ev == MG_EV_ACCEPT) { + d->sclient_nc = nc; + nc->user_data = d = d->sclient_data; + nc->handler = cb_sclient; + } + count_events(d, ev); +} +#endif + +/* The following test is only reliable on Linux. */ +#ifdef __linux__ +static const char *test_simple(void) { + struct mg_mgr mgr; + struct mg_connection *nc_server, *nc_client, *nc_sclient; + const char *address = "tcp://127.0.0.1:8910"; + struct simple_data client_data, server_data, sclient_data; + + (void) nc_sclient; + mg_mgr_init(&mgr, NULL); + + ASSERT((nc_server = mg_bind(&mgr, address, cb_server)) != NULL); + nc_server->user_data = &server_data; + memset(&server_data, 0, sizeof(server_data)); + server_data.sclient_data = &sclient_data; + memset(&sclient_data, 0, sizeof(sclient_data)); + + mg_mgr_poll(&mgr, 1); /* 1 - nothing */ + + ASSERT((nc_client = mg_connect(&mgr, address, cb_client)) != NULL); + nc_client->user_data = &client_data; + memset(&client_data, 0, sizeof(client_data)); + strcpy(client_data.to_send, "hi"); + + mg_mgr_poll(&mgr, 1); /* 2 - client connects and sends, server accepts */ + ASSERT_EQ(server_data.num_accept, 0); + ASSERT_EQ(sclient_data.num_accept, 1); + ASSERT_EQ(client_data.num_connect, 1); + ASSERT_EQ(client_data.num_send, 1); + ASSERT_STREQ(client_data.fail, ""); + ASSERT_STREQ(server_data.fail, ""); + + ASSERT(server_data.sclient_nc != NULL); + nc_sclient = server_data.sclient_nc; + ASSERT_EQ(sclient_data.num_send, 0); + ASSERT_EQ(sclient_data.num_recv, 0); + + strcpy(sclient_data.to_send, "hello"); + mg_mgr_poll(&mgr, 1); /* 3 - server receives, buffers response, closes */ + ASSERT_STREQ(sclient_data.fail, ""); + ASSERT_EQ(sclient_data.num_recv, 1); + ASSERT_EQ(sclient_data.num_send, 0); + ASSERT_EQ(client_data.num_recv, 0); + ASSERT_STREQ(sclient_data.data_rcvd, "hi"); + + mg_mgr_poll(&mgr, 1); /* 4 - server sends */ + ASSERT_STREQ(sclient_data.fail, ""); + ASSERT_EQ(sclient_data.num_send, 1); + + mg_mgr_poll(&mgr, 1); /* 5 - client receives */ + ASSERT_STREQ(client_data.fail, ""); + ASSERT_EQ(client_data.num_recv, 1); + ASSERT_STREQ(client_data.data_rcvd, "hello"); + + mg_mgr_free(&mgr); + + ASSERT_STREQ(client_data.fail, ""); + ASSERT_STREQ(server_data.fail, ""); + ASSERT_STREQ(sclient_data.fail, ""); + + ASSERT_EQ(server_data.num_accept, 0); + ASSERT_EQ(server_data.num_connect, 0); + ASSERT_EQ(server_data.num_recv, 0); + ASSERT_EQ(server_data.num_send, 0); + ASSERT_EQ(server_data.num_close, 1); + + ASSERT_EQ(client_data.num_accept, 0); + ASSERT_EQ(client_data.num_connect, 1); + ASSERT_EQ(client_data.num_recv, 1); + ASSERT_EQ(client_data.num_send, 1); + ASSERT_EQ(client_data.num_close, 1); + + ASSERT_EQ(sclient_data.num_accept, 1); + ASSERT_EQ(sclient_data.num_connect, 0); + ASSERT_EQ(sclient_data.num_recv, 1); + ASSERT_EQ(sclient_data.num_send, 1); + ASSERT_EQ(sclient_data.num_close, 1); + + return NULL; +} +#endif + +#if MG_ENABLE_THREADS +static void eh2(struct mg_connection *nc, int ev, void *p) { + (void) p; + switch (ev) { + case MG_EV_RECV: + strcpy((char *) nc->user_data, nc->recv_mbuf.buf); + break; + default: + break; + } +} + +static void *thread_func(void *param) { + sock_t sock = *(sock_t *) param; + send(sock, ":-)", 4, 0); + return NULL; +} + +static const char *test_thread(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + sock_t sp[2]; + char buf[20] = ""; + + ASSERT_EQ(mg_socketpair(sp, SOCK_STREAM), 1); + mg_start_thread(thread_func, &sp[1]); + + mg_mgr_init(&mgr, NULL); + ASSERT((nc = mg_add_sock(&mgr, sp[0], eh2)) != NULL); + nc->user_data = buf; + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, ":-)"); + mg_mgr_free(&mgr); + closesocket(sp[1]); + + return NULL; +} +#endif /* MG_ENABLE_THREADS */ + +struct udp_res { + struct mbuf buf_srv; + struct mg_connection *srv_recv_conn; + struct mbuf buf_clnt; + struct mg_connection *clt_recv_conn; +}; + +static void eh3_srv(struct mg_connection *nc, int ev, void *p) { + struct udp_res *ur = (struct udp_res *) nc->mgr->user_data; + struct mbuf *io = &nc->recv_mbuf; + (void) p; + + if (nc->flags & MG_F_LISTENING) { + if (ev != MG_EV_POLL && ev != MG_EV_CLOSE) { + mbuf_append(&ur->buf_srv, "BUG", 3); + } + return; + } + + if (ev == MG_EV_ACCEPT) { + mbuf_append(&ur->buf_srv, "ACCEPT", 6); + } else if (ev == MG_EV_RECV) { + mbuf_append(&ur->buf_srv, (nc->flags & MG_F_SEND_AND_CLOSE ? "+" : "-"), 1); + mbuf_append(&ur->buf_srv, " RECV ", 6); + mbuf_append(&ur->buf_srv, io->buf, io->len); + mg_send(nc, io->buf, io->len); + mbuf_remove(io, io->len); + } else if (ev == MG_EV_CLOSE) { + mbuf_append(&ur->buf_srv, " CLOSE", 6); + } +} + +static void eh3_clnt(struct mg_connection *nc, int ev, void *p) { + struct udp_res *ur = (struct udp_res *) nc->mgr->user_data; + struct mbuf *io = &nc->recv_mbuf; + (void) p; + + if (ev == MG_EV_CONNECT) { + mbuf_append(&ur->buf_clnt, "CONNECT", 7); + mg_printf(nc, "%s", "boo!"); + } else if (ev == MG_EV_RECV) { + mbuf_append(&ur->buf_clnt, " RECV ", 6); + mbuf_append(&ur->buf_clnt, io->buf, io->len); + mbuf_remove(io, io->len); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else if (ev == MG_EV_CLOSE) { + mbuf_append(&ur->buf_clnt, " CLOSE", 6); + } +} + +static const char *test_udp(void) { + struct mg_mgr mgr; + struct mg_connection *nc1, *nc2; + const char *address = "udp://127.0.0.1:7878"; + struct udp_res res; + memset(&res, 0, sizeof(res)); + mbuf_init(&res.buf_srv, 100); + mbuf_init(&res.buf_clnt, 100); + + mg_mgr_init(&mgr, &res); + ASSERT((nc1 = mg_bind(&mgr, address, eh3_srv)) != NULL); + ASSERT((nc2 = mg_connect(&mgr, address, eh3_clnt)) != NULL); + + poll_until(&mgr, 1, c_int_eq, &res.buf_clnt.len, (void *) 23); + ASSERT_STREQ_NZ(res.buf_srv.buf, "ACCEPT+ RECV boo! CLOSE"); + ASSERT_STREQ_NZ(res.buf_clnt.buf, "CONNECT RECV boo! CLOSE"); + + mbuf_free(&res.buf_srv); + mbuf_free(&res.buf_clnt); + mg_mgr_free(&mgr); + + return NULL; +} + +static const char *test_parse_http_message(void) { + static const char *a = "GET / HTTP/1.0\n\n"; + static const char *b = "GET /blah HTTP/1.0\r\nFoo: bar \r\n\r\n"; + static const char *c = "get b c\nz: k \nb: t\nvvv\n\n xx"; + static const char *d = "a b c\nContent-Length: 21 \nb: t\nvvv\n\n"; + static const char *e = "GET /foo?a=b&c=d HTTP/1.0\n\n"; + static const char *f = "POST /x HTTP/1.0\n\n"; + static const char *g = "WOHOO /x HTTP/1.0\n\n"; + static const char *h = "HTTP/1.0 200 OK\n\n"; + static const char *i = "HTTP/1.0 999 OMGWTFBBQ\n\n"; + static const char *j = + "GET / HTTP/1.0\r\nHost: 127.0.0.1:18888\r\nCookie:\r\nX-PlayID: " + "45455\r\nRange: 0-1\r\n\r\n"; + int idx, len; + struct mg_str *v; + struct http_message req; + + len = strlen(b); + for (idx = 0; idx < len; idx++) { + ASSERT_EQ(mg_parse_http(b, idx, &req, 1), 0); + } + + ASSERT_EQ(mg_parse_http("\b23", 3, &req, 1), -1); + ASSERT_EQ(mg_parse_http("\b23", 3, &req, 0), -1); + ASSERT_EQ(mg_parse_http("get\n\n", 5, &req, 1), -1); + ASSERT_EQ(mg_parse_http("get\n\n", 5, &req, 0), -1); + ASSERT_EQ(mg_parse_http(a, strlen(a) - 1, &req, 1), 0); + ASSERT_EQ(mg_parse_http(a, strlen(a), &req, 0), -1); + + ASSERT_EQ(mg_parse_http(a, strlen(a), &req, 1), (int) strlen(a)); + ASSERT_EQ(req.message.len, strlen(a)); + ASSERT_EQ(req.body.len, 0); + + ASSERT_EQ(mg_parse_http(b, strlen(b), &req, 0), -1); + ASSERT_EQ(mg_parse_http(b, strlen(b), &req, 1), (int) strlen(b)); + ASSERT_EQ(req.header_names[0].len, 3); + ASSERT_EQ(req.header_values[0].len, 3); + ASSERT(req.header_names[1].p == NULL); + ASSERT_EQ(req.query_string.len, 0); + ASSERT_EQ(req.message.len, strlen(b)); + ASSERT_EQ(req.body.len, 0); + + ASSERT_EQ(mg_parse_http(c, strlen(c), &req, 1), (int) strlen(c) - 3); + ASSERT(req.header_names[2].p == NULL); + ASSERT(req.header_names[0].p != NULL); + ASSERT(req.header_names[1].p != NULL); + ASSERT_EQ(memcmp(req.header_values[1].p, "t", 1), 0); + ASSERT_EQ(req.header_names[1].len, 1); + ASSERT_EQ(req.body.len, 0); + + ASSERT_EQ(mg_parse_http(d, strlen(d), &req, 1), (int) strlen(d)); + ASSERT_EQ(req.body.len, 21); + ASSERT_EQ(req.message.len, 21 + strlen(d)); + ASSERT(mg_get_http_header(&req, "foo") == NULL); + ASSERT((v = mg_get_http_header(&req, "contENT-Length")) != NULL); + ASSERT_EQ(v->len, 2); + ASSERT_STREQ_NZ(v->p, "21"); + + ASSERT_EQ(mg_parse_http(e, strlen(e), &req, 1), (int) strlen(e)); + ASSERT_EQ(mg_vcmp(&req.uri, "/foo"), 0); + ASSERT_EQ(mg_vcmp(&req.query_string, "a=b&c=d"), 0); + + ASSERT_EQ(mg_parse_http(f, strlen(f), &req, 1), (int) strlen(f)); + ASSERT_EQ(req.body.len, (size_t) ~0); + + ASSERT_EQ(mg_parse_http(g, strlen(g), &req, 1), (int) strlen(g)); + ASSERT_EQ(req.body.len, 0); + + ASSERT_EQ(mg_parse_http(h, strlen(h), &req, 0), (int) strlen(h)); + ASSERT_EQ(mg_vcmp(&req.proto, "HTTP/1.0"), 0); + ASSERT_EQ(req.resp_code, 200); + ASSERT_EQ(mg_vcmp(&req.resp_status_msg, "OK"), 0); + ASSERT_EQ(req.body.len, (size_t) ~0); + + ASSERT_EQ(mg_parse_http(i, strlen(i), &req, 0), -1); + + ASSERT_EQ(mg_parse_http(j, strlen(j), &req, 1), (int) strlen(j)); + ASSERT(mg_get_http_header(&req, "Host") != NULL); + ASSERT(mg_get_http_header(&req, "Cookie") == NULL); + ASSERT(mg_get_http_header(&req, "Range") != NULL); + + return NULL; +} + +static const char *test_get_http_var(void) { + char buf[256]; + struct mg_str body; + body.p = "key1=value1&key2=value2&key3=value%203&key4=value+4"; + body.len = strlen(body.p); + + ASSERT(mg_get_http_var(&body, "key1", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "value1"); + ASSERT(mg_get_http_var(&body, "KEY1", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "value1"); + ASSERT(mg_get_http_var(&body, "key2", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "value2"); + ASSERT(mg_get_http_var(&body, "key3", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "value 3"); + ASSERT(mg_get_http_var(&body, "key4", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "value 4"); + + ASSERT_EQ(mg_get_http_var(&body, "key1", NULL, sizeof(buf)), -2); + ASSERT_EQ(mg_get_http_var(&body, "key1", buf, 0), -2); + ASSERT_EQ(mg_get_http_var(&body, NULL, buf, sizeof(buf)), -1); + ASSERT_EQ(mg_get_http_var(&body, "key1", buf, 1), -3); + + body.p = "key=broken%2"; + body.len = strlen(body.p); + ASSERT(mg_get_http_var(&body, "key", buf, sizeof(buf)) < 0); + + body.p = "key=broken%2x"; + body.len = strlen(body.p); + ASSERT(mg_get_http_var(&body, "key", buf, sizeof(buf)) < 0); + + ASSERT_EQ(mg_get_http_var(&body, "inexistent", buf, sizeof(buf)), -4); + + return NULL; +} + +static void cb1(struct mg_connection *nc, int ev, void *ev_data) { + if (ev == MG_EV_HTTP_REQUEST) { + struct http_message *hm = (struct http_message *) ev_data; + if (mg_vcmp(&hm->uri, "/foo") == 0) { + mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[%.*s %d]", (int) hm->uri.len, + hm->uri.p, (int) hm->body.len); + nc->flags |= MG_F_SEND_AND_CLOSE; + } else { + s_http_server_opts.document_root = "."; + s_http_server_opts.per_directory_auth_file = "passwords.txt"; + s_http_server_opts.auth_domain = "foo.com"; + s_http_server_opts.dav_document_root = "./data/dav"; + s_http_server_opts.hidden_file_pattern = "hidden_file.*$"; + s_http_server_opts.custom_mime_types = + ".txt=text/plain; charset=windows-1251," + ".c=text/plain; charset=utf-8"; + mg_serve_http(nc, hm, s_http_server_opts); + } + } else if (ev == MG_EV_SSI_CALL) { + mg_printf_html_escape(nc, "[ssi call: %s]", (char *) ev_data); + } else if (ev == MG_EV_SSI_CALL_CTX) { + struct mg_ssi_call_ctx *cctx = (struct mg_ssi_call_ctx *) ev_data; + mg_printf_html_escape(nc, "[ssi call w/ ctx: %.*s %.*s %.*s]", + (int) cctx->req->uri.len, cctx->req->uri.p, + (int) cctx->file.len, cctx->file.p, + (int) cctx->arg.len, cctx->arg.p); + } +} + +static void secret_endpoint_cb(struct mg_connection *nc, int ev, + void *ev_data) { + if (ev != MG_EV_HTTP_REQUEST) return; + mg_send_response_line(nc, 200, "Content-Type: text/plain\r\n"); + mg_printf(nc, "%s", "Secret!"); + nc->flags |= MG_F_SEND_AND_CLOSE; + (void) ev_data; +} + +static void cb2(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + + if (ev == MG_EV_HTTP_REPLY) { + sprintf((char *) nc->user_data, "%.*s %lu", (int) hm->body.len, hm->body.p, + (unsigned long) hm->message.len); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +} + +static void cb7(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + size_t size; + char *data, *user_data = (char *) nc->user_data; + + if (ev == MG_EV_HTTP_REPLY) { + /* Make sure that we've downloaded this executable, byte-to-byte */ + data = read_file(s_argv_0, &size); + DBG(("file %s, size %d; got %d", s_argv_0, (int) size, (int) hm->body.len)); + if (data != NULL && size == hm->body.len && + memcmp(hm->body.p, data, size) == 0) { + strcpy(user_data, "success"); + } else { + strcpy(user_data, "fail"); + } + free(data); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } else if (ev == MG_EV_RECV) { +#if 0 + LOG(LL_INFO, ("%d", (int) nc->recv_mbuf.len)); +#endif + } +} + +static void cb8(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + DBG(("%p ev=%d", (void *) nc, ev)); + + if (ev == MG_EV_HTTP_REPLY) { + snprintf((char *) nc->user_data, FETCH_BUF_SIZE, "%.*s", + (int) hm->message.len, hm->message.p); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +} + +static void cb10(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + struct mg_str *s; + + if (ev == MG_EV_HTTP_REPLY && + (s = mg_get_http_header(hm, "Content-Type")) != NULL) { + sprintf((char *) nc->user_data, "%.*s", (int) s->len, s->p); + } +} + +static void endpoint_handler(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + (void) ev_data; + + if (ev == MG_EV_HTTP_REQUEST) { + if (mg_vcmp(&hm->uri, "/") == 0 || mg_vcmp(&hm->uri, "/foo") == 0) { + mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[%.*s%s%.*s %d]", (int) hm->uri.len, + hm->uri.p, hm->query_string.len > 0 ? "?" : "", + (int) hm->query_string.len, hm->query_string.p, + (int) hm->body.len); + nc->flags |= MG_F_SEND_AND_CLOSE; + } + } else if (ev == MG_EV_CLOSE) { + if (nc->listener != NULL) { + (*(int *) nc->listener->user_data) += 100; + } + } +} + +static void handle_hello1(struct mg_connection *nc, int ev, void *ev_data) { + (void) ev_data; + switch (ev) { + case MG_EV_HTTP_REQUEST: + mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello1]"); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + case MG_EV_CLOSE: + (*(int *) nc->listener->user_data)++; + break; + } +} + +static void handle_hello2(struct mg_connection *nc, int ev, void *ev_data) { + (void) ev_data; + switch (ev) { + case MG_EV_HTTP_REQUEST: + mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello two]"); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + case MG_EV_CLOSE: + (*(int *) nc->listener->user_data)++; + break; + } +} + +static void handle_hello5(struct mg_connection *nc, int ev, void *ev_data) { + (void) ev_data; + switch (ev) { + case MG_EV_HTTP_REQUEST: + mg_printf(nc, "HTTP/1.0 200 OK\r\n\r\n[I am Hello again]"); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + case MG_EV_CLOSE: + (*(int *) nc->listener->user_data)++; + break; + } +} + +static void vfetch_http(char *buf, const char *auth_file, + const char *request_fmt, va_list ap) { + struct mg_mgr mgr; + struct mg_connection *nc; + struct mg_http_endpoint_opts ep_opts; + char local_addr[50]; + + /* Setup server. Use different local port for the next invocation. */ + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + snprintf(local_addr, sizeof(local_addr), "127.0.0.1:%d", s_listening_port++); + nc = mg_bind(&mgr, local_addr, cb1); + mg_set_protocol_http_websocket(nc); + memset(&ep_opts, 0, sizeof(ep_opts)); + ep_opts.auth_file = auth_file; + ep_opts.auth_domain = "foo.com"; + mg_register_http_endpoint_opt(nc, "/secret", secret_endpoint_cb, ep_opts); + + /* Setup client */ + nc = mg_connect(&mgr, local_addr, cb8); + mg_set_protocol_http_websocket(nc); + nc->user_data = buf; + mg_vprintf(nc, request_fmt, ap); + + /* Run event loop, destroy server */ + buf[0] = '\0'; + poll_until(&mgr, 5, c_str_ne, buf, (void *) ""); + + mg_mgr_free(&mgr); +} + +static void fetch_http(char *buf, const char *request_fmt, ...) { + va_list ap; + va_start(ap, request_fmt); + vfetch_http(buf, "data/auth/passwords.txt", request_fmt, ap); + va_end(ap); +} + +static void fetch_http2(char *buf, const char *auth_file, + const char *request_fmt, ...) { + va_list ap; + va_start(ap, request_fmt); + vfetch_http(buf, auth_file, request_fmt, ap); + va_end(ap); +} + +static const char *test_http_endpoints(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *local_addr = "127.0.0.1:7778"; + char buf[50]; + int close_count = 0; + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, local_addr, endpoint_handler)) != NULL); + mg_register_http_endpoint(nc, "/hello1", handle_hello1 MG_UD_ARG(NULL)); + mg_register_http_endpoint(nc, "/hello1/hello2", + handle_hello2 MG_UD_ARG(NULL)); + mg_register_http_endpoint(nc, "/hello5/*", handle_hello5 MG_UD_ARG(NULL)); + nc->user_data = &close_count; + + mg_set_protocol_http_websocket(nc); + /* Valid HTTP request. Pass test buffer to the callback. */ + ASSERT((nc = mg_connect_http(&mgr, cb2, "127.0.0.1:7778", NULL, + "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[/ 10] 25"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/", NULL, + "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[/ 10] 25"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/foo?a=b", NULL, + "foo")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[/foo?a=b 3] 31"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/foo", NULL, + "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[/foo 10] 28"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/hello1", NULL, + "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[I am Hello1] 32"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/hello1/hello2", + NULL, "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[I am Hello two] 35"); + + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7778/hello5/dummy", + NULL, "0123456789")) != NULL); + memset(buf, 0, sizeof(buf)); + nc->user_data = buf; + + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[I am Hello again] 37"); + + ASSERT_EQ(close_count, 700); + + mg_mgr_free(&mgr); + + return NULL; +} + +static const char *serve_file_verify(struct http_message *hm) { + size_t size; + char *data = read_file("unit_test.c", &size); + if (data == NULL || size != hm->body.len || + memcmp(hm->body.p, data, size) != 0) { + return "wrong data"; + } + free(data); + + { + char buf[20]; + struct mg_str *h = mg_get_http_header(hm, "Content-Length"); + ASSERT(h != NULL); + snprintf(buf, sizeof(buf), "%d", (int) hm->body.len); + ASSERT_MG_STREQ(*h, buf); + } + { + struct mg_str *h = mg_get_http_header(hm, "Connection"); + ASSERT(h != NULL); + ASSERT_MG_STREQ(*h, "keep-alive"); + } + return "success"; +} + +static void serve_file_cb(struct mg_connection *nc, int ev, void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + char *user_data = (char *) nc->user_data; + + if (ev == MG_EV_HTTP_REQUEST) { + mg_http_serve_file(nc, hm, "unit_test.c", mg_mk_str("text/plain"), + mg_mk_str("")); + } else if (ev == MG_EV_HTTP_REPLY) { + strcpy(user_data, serve_file_verify(hm)); + nc->flags |= MG_F_CLOSE_IMMEDIATELY; + } +} + +static const char *test_http_serve_file(void) { + struct mg_mgr mgr; + struct mg_connection *lc, *nc; + const char *local_addr = "127.0.0.1:7777"; + char status[100]; + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((lc = mg_bind(&mgr, local_addr, serve_file_cb)) != NULL); + mg_set_protocol_http_websocket(lc); + ASSERT((nc = mg_connect(&mgr, local_addr, serve_file_cb)) != NULL); + mg_set_protocol_http_websocket(nc); + status[0] = '\0'; + nc->user_data = status; + mg_printf(nc, "GET / HTTP/1.1\r\n\r\n"); + poll_until(&mgr, 5, c_str_ne, status, (void *) ""); + ASSERT_STREQ(status, "success"); + mg_mgr_free(&mgr); + return NULL; +} + +static void srv1(struct mg_connection *c, int ev, void *ev_data) { + if (ev == MG_EV_HTTP_REQUEST) { + mg_http_serve_file(c, (struct http_message *) ev_data, "unit_test.c", + mg_mk_str("text/plain"), mg_mk_str("")); + } +} + +static void srv2(struct mg_connection *c, int ev, void *ev_data) { + static cs_md5_ctx c1, c2; + struct http_message *hm = (struct http_message *) ev_data; + + switch (ev) { + case MG_EV_CONNECT: + cs_md5_init(&c1); + cs_md5_init(&c2); + break; + case MG_EV_HTTP_CHUNK: + cs_md5_update(&c1, (const unsigned char *) hm->body.p, hm->body.len); + c->flags |= MG_F_DELETE_CHUNK; + break; + case MG_EV_HTTP_REPLY: { + unsigned char sig1[16], sig2[sizeof(sig1)]; + size_t size; + char *data = read_file("unit_test.c", &size); + if (data != NULL) cs_md5_update(&c2, (const unsigned char *) data, size); + free(data); + cs_md5_final(sig1, &c1); + cs_md5_final(sig2, &c2); + *(int *) c->user_data = (memcmp(sig1, sig2, sizeof(sig1)) == 0) ? 1 : 2; + break; + } + } +} + +static const char *test_http_serve_file_streaming(void) { + struct mg_mgr mgr; + struct mg_connection *lc, *nc; + const char *local_addr = "127.0.0.1:7732"; + int status = 0; + mg_mgr_init(&mgr, NULL); + ASSERT((lc = mg_bind(&mgr, local_addr, srv1)) != NULL); + mg_set_protocol_http_websocket(lc); + ASSERT((nc = mg_connect(&mgr, local_addr, srv2)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &status; + mg_printf(nc, "GET / HTTP/1.1\r\n\r\n"); + poll_until(&mgr, 30, c_int_ne, &status, (void *) 0); + ASSERT_EQ(status, 1); + mg_mgr_free(&mgr); + return NULL; +} + +static const char *test_http(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *this_binary, *local_addr = "127.0.0.1:7777"; + char buf[50] = "", status[100] = "", mime1[20] = "", mime2[100] = ""; + char opt_buf[1024] = ""; + const char *opt_answer = + "HTTP/1.1 200 OK\r\nAllow: GET, POST, HEAD, CONNECT, OPTIONS, MKCOL, " + "PUT, DELETE, PROPFIND, MOVE"; + char url[1000]; + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, local_addr, cb1)) != NULL); + + mg_set_protocol_http_websocket(nc); + /* Valid HTTP request. Pass test buffer to the callback. */ + ASSERT((nc = mg_connect_http(&mgr, cb2, "http://127.0.0.1:7777/foo", NULL, + "0123456789")) != NULL); + nc->user_data = buf; + DBG(("== WAIT ==")); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "[/foo 10] 28"); + + /* OPTIONS request */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb8)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = opt_buf; + + mg_printf(nc, "%s", "OPTIONS / HTTP 1.0\n\n"); + + poll_until(&mgr, 1, c_str_ne, opt_buf, (void *) ""); + ASSERT_STREQ_NZ(opt_buf, opt_answer); + + /* Invalid HTTP request */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb2)) != NULL); + mg_set_protocol_http_websocket(nc); + + mg_printf(nc, "%s", "bl\x03\n\n"); + /* Test static file download by downloading this executable, argv[0] */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb7)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = status; + + /* Wine and GDB set argv0 to full path: strip the dir component */ + if ((this_binary = strrchr(s_argv_0, '\\')) != NULL) { + this_binary++; + } else if ((this_binary = strrchr(s_argv_0, '/')) != NULL) { + this_binary++; + } else { + this_binary = s_argv_0; + } + mg_printf(nc, "GET /%s HTTP/1.0\n\n", this_binary); + /* Test mime type for static file */ + snprintf(url, sizeof(url), "http://%s/data/dummy.xml", local_addr); + ASSERT((nc = mg_connect_http(&mgr, cb10, url, NULL, NULL)) != NULL); + nc->user_data = mime1; + + /* Test custom mime type for static file */ + snprintf(url, sizeof(url), "http://%s/data/range.txt", local_addr); + ASSERT((nc = mg_connect_http(&mgr, cb10, url, NULL, NULL)) != NULL); + nc->user_data = mime2; + + /* Run event loop. Use more cycles to let file download complete. */ + poll_until(&mgr, 5, c_str_ne, status, (void *) ""); + mg_mgr_free(&mgr); + + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(status, "success"); + ASSERT_STREQ(mime1, "text/xml"); + ASSERT_STREQ(mime2, "text/plain; charset=windows-1251"); + + return NULL; +} + +static const char *test_http_send_redirect(void) { + struct mg_connection nc; + init_test_connection(&nc); + + mg_http_send_redirect(&nc, 301, mg_mk_str("http://foo"), mg_mk_str(NULL)); + mg_send(&nc, "", 1); /* NUL */ + ASSERT_STREQ_NZ(nc.send_mbuf.buf, + "HTTP/1.1 301 Moved\r\n" + "Server: Mongoose/" MG_VERSION + "\r\n" + "Location: http://foo\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 41\r\n" + "Cache-Control: no-cache\r\n" + "\r\n" + "

Moved here.\r\n"); + ASSERT_EQ(nc.send_mbuf.len, 179); + mbuf_remove(&nc.send_mbuf, nc.send_mbuf.len); + + mg_http_send_redirect(&nc, 302, mg_mk_str("/bar"), mg_mk_str("O-M-G: WTF?!")); + mg_send(&nc, "", 1); /* NUL */ + ASSERT_STREQ_NZ(nc.send_mbuf.buf, + "HTTP/1.1 302 Found\r\n" + "Server: Mongoose/" MG_VERSION + "\r\n" + "Location: /bar\r\n" + "Content-Type: text/html\r\n" + "Content-Length: 35\r\n" + "Cache-Control: no-cache\r\n" + "O-M-G: WTF?!\r\n" + "\r\n" + "

Moved here.\r\n"); + ASSERT_EQ(nc.send_mbuf.len, 181); + mbuf_remove(&nc.send_mbuf, nc.send_mbuf.len); + + mbuf_free(&nc.send_mbuf); + return NULL; +} + +static const char *test_http_digest_auth(void) { + char buf[FETCH_BUF_SIZE], auth_hdr[200]; + char nonce[40]; + struct mg_str bufstr; + + /* Test digest authorization popup - per-directory auth file */ + fetch_http(buf, "%s", "GET /data/auth/a.txt?a=b HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); + bufstr = mg_mk_str(buf); + ASSERT_GT(mg_http_parse_header(&bufstr, "nonce", nonce, sizeof(nonce)), 0); + buf[0] = '\0'; + /* Per-endpoint auth config */ + fetch_http(buf, "%s", "GET /secret HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + fetch_http(buf, "%s", + "POST /secret/upload HTTP/1.0\r\n" + "Content-Type: multipart/form-data;boundary=omgwtf\r\n" + "\r\n--omgwtf\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); +#endif + + /* Test digest authorization success */ + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/a.txt?a=b", "foo.com", "joe", + "doe", nonce); + fetch_http(buf, "GET /data/auth/a.txt?a=b HTTP/1.0\r\n%s\r\n\r\n", auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + ASSERT_STREQ(buf + strlen(buf) - 7, "\r\n\r\nhi\n"); + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/secret", "foo.com", "joe", "doe", nonce); + fetch_http(buf, "GET /secret HTTP/1.0\r\n%s\r\n\r\n", auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + ASSERT_STREQ(buf + strlen(buf) - 7, "Secret!"); + + /* Test digest authorization failure with non-existing passwords file */ + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/a.txt?a=b", "foo.com", "joe", + "doe", nonce); + fetch_http2(buf, "data/auth/non-existing-passwords.txt", + "GET /data/auth/a.txt?a=b HTTP/1.0\r\n%s\r\n\r\n", auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + ASSERT_STREQ(buf + strlen(buf) - 7, "\r\n\r\nhi\n"); + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/secret", "foo.com", "joe", "doe", nonce); + fetch_http2(buf, "data/auth/non-existing-passwords.txt", + "GET /secret HTTP/1.0\r\n%s\r\n\r\n", auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); + + /* Test digest authorization failure with old nonce */ + { + /* + * Add 10 seconds to the nonce value, so that it'll be in the future + * and mg_check_nonce() should fail. + */ + unsigned long nonce_val = strtoul(nonce, NULL, 16); + sprintf(nonce, "%lx", nonce_val + 10); + + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/a.txt?a=b", "foo.com", "joe", + "doe", nonce); + fetch_http(buf, "GET /data/auth/a.txt?a=b HTTP/1.0\r\n%s\r\n\r\n", + auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); + + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/secret", "foo.com", "joe", "doe", + nonce); + + fetch_http(buf, "GET /secret HTTP/1.0\r\n%s\r\n\r\n", auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); + + /* Make nonce 1-hour-old, so mg_check_nonce() should fail again. */ + sprintf(nonce, "%lx", nonce_val - 60 * 60); + + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/a.txt?a=b", "foo.com", "joe", + "doe", nonce); + fetch_http(buf, "GET /data/auth/a.txt?a=b HTTP/1.0\r\n%s\r\n\r\n", + auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 401"); + + /* Renew nonce to represent the actual nonce from the server */ + bufstr = mg_mk_str(buf); + ASSERT_GT(mg_http_parse_header(&bufstr, "nonce", nonce, sizeof(nonce)), 0); + } + + /* Test that passwords file is hidden from view */ + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/p%61sswords%2etxt", "foo.com", + "joe", "doe", nonce); + fetch_http(buf, + "GET /data/auth/p%%61sswords%%2etxt HTTP/1.0\r\n" + "%s\r\n\r\n", + auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 404"); + /* On case-insensitive filesystems too. */ + mg_http_create_digest_auth_header(auth_hdr, sizeof(auth_hdr), "GET", + "/data/auth/Passwords.txt", "foo.com", + "joe", "doe", nonce); + fetch_http(buf, + "GET /data/auth/Passwords.txt HTTP/1.0\r\n" + "%s\r\n\r\n", + auth_hdr); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 404"); + + return NULL; +} + +static const char *test_http_errors(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *local_addr = "127.0.0.1:7777"; + char status[1000] = ""; + + mg_mgr_init(&mgr, NULL); + s_http_server_opts.enable_directory_listing = NULL; + ASSERT((nc = mg_bind(&mgr, local_addr, cb1)) != NULL); + mg_set_protocol_http_websocket(nc); + +#if !defined(TEST_UNDER_VIRTUALBOX) && !defined(_WIN32) + /* Test file which exists but cannot be opened */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb8)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = status; + system("touch test_unreadable; chmod 000 test_unreadable"); + mg_printf(nc, "GET /%s HTTP/1.0\n\n", "../test_unreadable"); + + /* Run event loop. Use more cycles to let file download complete. */ + poll_until(&mgr, 1, c_str_ne, status, (void *) ""); + system("rm -f test_unreadable"); + + /* Check that it failed */ + ASSERT_STREQ_NZ(status, "HTTP/1.1 403"); +#endif + + /* Test non existing file */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb8)) != NULL); + mg_set_protocol_http_websocket(nc); + status[0] = '\0'; + nc->user_data = status; + mg_printf(nc, "GET /%s HTTP/1.0\n\n", "/please_dont_create_this_file_srsly"); + + poll_until(&mgr, 1, c_str_ne, status, (void *) ""); + + /* Check that it failed */ + ASSERT_STREQ_NZ(status, "HTTP/1.1 404"); + + /* Cleanup */ + mg_mgr_free(&mgr); + + return NULL; +} + +static const char *test_http_index(void) { + char buf[FETCH_BUF_SIZE]; + fetch_http(buf, "%s", "GET /data/dir_with_index/ HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + ASSERT(strstr(buf, "Content-Length: 3\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 5, "\r\nfoo"); + /* Directory listing: allowed by default. */ + s_http_server_opts.enable_directory_listing = NULL; + fetch_http(buf, "%s", "GET /data/dir_no_index/ HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, + "HTTP/1.1 200 OK\r\n" + "Server: Mongoose/" MG_VERSION + "\r\n" + "Transfer-Encoding: chunked\r\n"); + ASSERT(strstr(buf, "") != NULL); + /* Allowed explicitly. */ + s_http_server_opts.enable_directory_listing = "yes"; + fetch_http(buf, "%s", "GET /data/dir_no_index/ HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + /* Disallowed. */ + s_http_server_opts.enable_directory_listing = "no"; + fetch_http(buf, "%s", "GET /data/dir_no_index/ HTTP/1.0\r\n\r\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 403"); + return NULL; +} + +static const char *test_ssi(void) { + char buf[FETCH_BUF_SIZE]; + fetch_http(buf, "%s", "GET /data/ssi/ HTTP/1.0\n\n"); + ASSERT_STREQ(buf, + "HTTP/1.1 200 OK\r\n" + "Server: Mongoose/" MG_VERSION + "\r\n" + "Content-Type: text/html\r\n" + "Connection: close\r\n\r\na\n\n" + "<b>[ssi call: foo(bar)]" + "[ssi call w/ ctx: /data/ssi/ ." DS "data" DS "ssi" DS + "nested.shtml foo(bar)]" + "</b>\nb\n\n\n"); + return NULL; +} + +#ifndef NO_CGI_TEST +static const char *test_cgi(void) { + char buf[FETCH_BUF_SIZE]; + char expected_server_port[100]; + const char *post_data = "aa=1234&bb=hi there"; + + fetch_http(buf, + "POST /data/cgi/ HTTP/1.0\r\n" + "Content-Type: application/x-www-form-urlencoded\r\n" + "Content-Length: %d\n\n%s", + (int) strlen(post_data), post_data); + + /* fprintf(stderr, "%s", buf); */ + + snprintf(expected_server_port, sizeof(expected_server_port), + "\nE: SERVER_PORT=%d\r\n", s_listening_port - 1); + ASSERT(strstr(buf, expected_server_port) != NULL); + + ASSERT_STREQ_NZ(buf, "HTTP/1.1 201 Created\r\n"); + ASSERT(strstr(buf, "\nE: CONTENT_LENGTH=19\r\n") != NULL); + ASSERT( + strstr(buf, "\nE: CONTENT_TYPE=application/x-www-form-urlencoded\r\n") != + NULL); + ASSERT(strstr(buf, "\nE: DOCUMENT_ROOT=.\r\n") != NULL); + ASSERT(strstr(buf, "\nE: HTTPS=off\r\n") != NULL); + ASSERT(strstr(buf, "\nE: HTTP_CONTENT_LENGTH=19\r\n") != NULL); + ASSERT( + strstr(buf, + "\nE: HTTP_CONTENT_TYPE=application/x-www-form-urlencoded\r\n") != + NULL); + ASSERT(strstr(buf, "\nE: REMOTE_ADDR=127.0.0.1\r\n") != NULL); + ASSERT(strstr(buf, "\nE: REQUEST_METHOD=POST\r\n") != NULL); + ASSERT(strstr(buf, "\nE: REQUEST_URI=/data/cgi/\r\n") != NULL); + ASSERT(strstr(buf, "\nE: SCRIPT_NAME=/data/cgi/index.cgi\r\n") != NULL); + ASSERT(strstr(buf, "\nE: SCRIPT_FILENAME=." DS "data" DS "cgi" DS + "index.cgi\r\n") != NULL); + ASSERT(strstr(buf, "\nE: PATH_INFO=") == NULL); + ASSERT(strstr(buf, "\nE: PATH_TRANSLATED=") == NULL); + ASSERT(strstr(buf, "\nE: SERVER_SOFTWARE=Mongoose/" MG_VERSION "\r\n") != + NULL); + ASSERT(strstr(buf, "\nP: aa=1234\r\n") != NULL); + ASSERT(strstr(buf, "\nP: bb=hi there\r\n") != NULL); + + fetch_http(buf, + "GET /data/cgi/index%%2ecgi/foo%%2fbar/baz/?x=y HTTP/1.0\r\n\r\n"); + + ASSERT_STREQ_NZ(buf, "HTTP/1.1 201 Created\r\n"); + ASSERT(strstr(buf, "\nE: CONTENT_LENGTH=") == NULL); + ASSERT(strstr(buf, "\nE: CONTENT_TYPE=") == NULL); + ASSERT(strstr(buf, "\nE: DOCUMENT_ROOT=.\r\n") != NULL); + ASSERT(strstr(buf, "\nE: HTTPS=off\r\n") != NULL); + ASSERT(strstr(buf, "\nE: HTTP_CONTENT_LENGTH=") == NULL); + ASSERT(strstr(buf, "\nE: HTTP_CONTENT_TYPE=") == NULL); + ASSERT(strstr(buf, "\nE: REQUEST_METHOD=GET\r\n") != NULL); + ASSERT( + strstr(buf, + "\nE: REQUEST_URI=/data/cgi/index%2ecgi/foo%2fbar/baz/?x=y\r\n") != + NULL); + ASSERT(strstr(buf, "\nE: SCRIPT_NAME=/data/cgi/index%2ecgi\r\n") != NULL); + ASSERT(strstr(buf, "\nE: SCRIPT_FILENAME=." DS "data" DS "cgi" DS + "index.cgi\r\n") != NULL); + ASSERT(strstr(buf, "\nE: PATH_INFO=/foo%2fbar/baz/") != NULL); + ASSERT(strstr(buf, "\nE: PATH_TRANSLATED=/foo%2fbar/baz/") != NULL); + ASSERT(strstr(buf, "\nE: SERVER_SOFTWARE=Mongoose/" MG_VERSION "\r\n") != + NULL); + ASSERT(strstr(buf, "\nQ: x=y\r\n") != NULL); + + return NULL; +} +#endif + +static const char *test_http_rewrites(void) { + char buf[FETCH_BUF_SIZE]; + + s_http_server_opts.url_rewrites = + "/~joe=./data/rewrites," + "@foo.com=./data/rewrites/foo.com"; + + /* Test rewrite */ + fetch_http(buf, "%s", "GET /~joe/msg.txt HTTP/1.0\nHost: foo.co\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200 OK"); + ASSERT(strstr(buf, "Content-Length: 6\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 8, "\r\nworks\n"); + + /* Test rewrite that points to directory, expect redirect */ + fetch_http(buf, "%s", "GET /~joe HTTP/1.0\n\n"); + ASSERT_STREQ(buf, + "HTTP/1.1 301 Moved\r\nLocation: /~joe/\r\n" + "Content-Length: 0\r\n\r\n"); + + /* Test domain-based rewrite */ + fetch_http(buf, "%s", "GET / HTTP/1.0\nHost: foo.com\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200 OK"); + ASSERT(strstr(buf, "Content-Length: 9\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 11, "\r\nfoo_root\n"); + + /* Test port-based rewrites */ + s_listening_port = 25555; + s_http_server_opts.url_rewrites = "%25555=https://foo"; + fetch_http(buf, "%s", "GET / HTTP/1.0\n\n"); + ASSERT_STREQ(buf, + "HTTP/1.1 301 Moved\r\n" + "Server: Mongoose/" MG_VERSION + "\r\n" + "Content-Length: 0\r\n" + "Location: https://foo/\r\n\r\n"); + + return NULL; +} + +static const char *test_http_dav(void) { + char buf[FETCH_BUF_SIZE]; + cs_stat_t st; + + remove("./data/dav/b.txt"); + rmdir("./data/dav/d"); + + /* Test PROPFIND */ + s_http_server_opts.enable_directory_listing = "yes"; + fetch_http(buf, "%s", "PROPFIND / HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 207"); + ASSERT(strstr(buf, "a.txt") != NULL); + ASSERT(strstr(buf, "hidden_file.txt") == NULL); + + /* Test MKCOL */ + fetch_http(buf, "%s", "MKCOL /d HTTP/1.0\nContent-Length:5\n\n12345"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 415"); + fetch_http(buf, "%s", "MKCOL /d HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 201"); + fetch_http(buf, "%s", "MKCOL /d HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 405"); + fetch_http(buf, "%s", "MKCOL /x/d HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 409"); + + /* Test PUT */ + fetch_http(buf, "%s", "PUT /b.txt HTTP/1.0\nContent-Length: 5\n\n12345"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 201"); + fetch_http(buf, "%s", "GET /data/dav/b.txt HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200"); + ASSERT(strstr(buf, "Content-Length: 5\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 7, "\r\n12345"); + + /* Test DELETE */ + fetch_http(buf, "%s", "DELETE /b.txt HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 204"); + ASSERT(mg_stat("./data/dav/b.txt", &st) != 0); + fetch_http(buf, "%s", "DELETE /d HTTP/1.0\n\n"); + ASSERT(mg_stat("./data/dav/d", &st) != 0); + + /* Non-existing file */ + fetch_http(buf, "%s", "PROPFIND /__blah.txt HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 404"); + + return NULL; +} + +static const char *test_http_range(void) { + char buf[FETCH_BUF_SIZE]; + + fetch_http(buf, "%s", "GET /data/range.txt HTTP/1.0\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 200 OK"); + ASSERT(strstr(buf, "Content-Length: 312\r\n") != 0); + + /* Fetch a piece from the middle of the file */ + fetch_http(buf, "%s", "GET /data/range.txt HTTP/1.0\nRange: bytes=5-10\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 206 Partial Content"); + ASSERT(strstr(buf, "Content-Length: 6\r\n") != 0); + ASSERT(strstr(buf, "Content-Range: bytes 5-10/312\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 8, "\r\n of co"); + + /* Fetch till EOF */ + fetch_http(buf, "%s", "GET /data/range.txt HTTP/1.0\nRange: bytes=300-\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 206 Partial Content"); + ASSERT(strstr(buf, "Content-Length: 12\r\n") != 0); + ASSERT(strstr(buf, "Content-Range: bytes 300-311/312\r\n") != 0); + ASSERT_STREQ(buf + strlen(buf) - 14, "\r\nis disease.\n"); + + /* Fetch past EOF, must trigger 416 response */ + fetch_http(buf, "%s", "GET /data/range.txt HTTP/1.0\nRange: bytes=1000-\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 416"); + ASSERT(strstr(buf, "Content-Length: 0\r\n") != 0); + ASSERT(strstr(buf, "Content-Range: bytes */312\r\n") != 0); + + /* Request range past EOF, must trigger 416 response */ + fetch_http(buf, "%s", "GET /data/range.txt HTTP/1.0\nRange: bytes=0-312\n\n"); + ASSERT_STREQ_NZ(buf, "HTTP/1.1 416"); + + return NULL; +} + +static void cb3(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + + if (ev == MG_EV_WEBSOCKET_FRAME) { + const char *reply = wm->size == 2 && !memcmp(wm->data, "hi", 2) ? "A" : "B"; + mg_printf_websocket_frame(nc, WEBSOCKET_OP_TEXT, "%s", reply); + } +} + +static void cb4(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + + if (ev == MG_EV_WEBSOCKET_FRAME) { + memcpy(nc->user_data, wm->data, wm->size); + mg_send_websocket_frame(nc, WEBSOCKET_OP_CLOSE, NULL, 0); + } else if (ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) { + /* Send "hi" to server. server must reply "A". */ + struct mg_str h[2]; + h[0].p = "h"; + h[0].len = 1; + h[1].p = "i"; + h[1].len = 1; + mg_send_websocket_framev(nc, WEBSOCKET_OP_TEXT, h, 2); + } +} + +static void cb_ws_server(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + + if (ev == MG_EV_WEBSOCKET_FRAME || ev == MG_EV_WEBSOCKET_CONTROL_FRAME) { + mg_printf_websocket_frame( + nc, WEBSOCKET_OP_TEXT, "%s%.2x:{%.*s}", + (ev == MG_EV_WEBSOCKET_CONTROL_FRAME ? "CONTROL:" : ""), wm->flags, + (int) wm->size, wm->data); + } +} + +static int s_ws_client1_connected = false; +static void cb_ws_client1(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + + if (ev == MG_EV_WEBSOCKET_FRAME || ev == MG_EV_WEBSOCKET_CONTROL_FRAME) { + char *tmp = NULL; + int tmplen; + tmplen = + mg_asprintf(&tmp, 0, "%s%.2x:[%.*s]", + (ev == MG_EV_WEBSOCKET_CONTROL_FRAME ? "CONTROL:" : ""), + wm->flags, (int) wm->size, wm->data); + mbuf_append((struct mbuf *) nc->user_data, tmp, tmplen); + free(tmp); + } else if (ev == MG_EV_CLOSE) { + s_ws_client1_connected = 0; + } else if (ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) { + s_ws_client1_connected = 1; + } +} + +static const char *test_websocket(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *local_addr = "127.0.0.1:7778"; + struct mbuf mb; + mbuf_init(&mb, 100); + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, local_addr, cb_ws_server)) != NULL); + mg_set_protocol_http_websocket(nc); + + /* + * Websocket request "hi" via mg_send_websocket_framev() + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + { + /* Send "hi" to server. server must reply "A". */ + struct mg_str h[2]; + h[0].p = "h"; + h[0].len = 1; + h[1].p = "i"; + h[1].len = 1; + mg_send_websocket_framev(nc, WEBSOCKET_OP_TEXT, h, 2); + } + poll_until(&mgr, 1, c_int_ne, &mb.len, (void *) 0); + mbuf_append(&mb, "\0", 1); + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(mb.buf, "81:[81:{hi}]"); + + /* + * Websocket request "hallloo", composed from two fragments + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + ASSERT_EQ(s_ws_client1_connected, 1); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT | WEBSOCKET_DONT_FIN, "hall", + 4); + mg_send_websocket_frame(nc, WEBSOCKET_OP_CONTINUE | WEBSOCKET_DONT_FIN, "--", + 2); + + /* Poll 0.5 seconds, we don't expect c_str_ne predicate to return true */ + poll_until(&mgr, 0.5, c_int_ne, &mb.len, (void *) 0); + ASSERT_EQ(mb.len, 0); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_CONTINUE, "loo", 3); + + poll_until(&mgr, 1, c_int_ne, &mb.len, (void *) 0); + mbuf_append(&mb, "\0", 1); + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(mb.buf, "81:[81:{hall--loo}]"); + + /* + * Ping request + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + ASSERT_EQ(s_ws_client1_connected, 1); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_PING, "myping", 6); + + { + /* + * As a response to Ping, we should first receive Pong (0x8a), + * and then text message (0x81) with our Ping (0x89) echo. + */ + const char *expected = "CONTROL:8a:[myping]81:[CONTROL:89:{myping}]"; + poll_until(&mgr, 1, c_int_eq, &mb.len, (void *) strlen(expected)); + mbuf_append(&mb, "\0", 1); + ASSERT_STREQ(mb.buf, expected); + } + + /* + * Ping request injected in between of a fragmented message + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + ASSERT_EQ(s_ws_client1_connected, 1); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT | WEBSOCKET_DONT_FIN, "abc", 3); + mg_send_websocket_frame(nc, WEBSOCKET_OP_CONTINUE | WEBSOCKET_DONT_FIN, "def", + 3); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_PING, "0123", 4); + + mg_send_websocket_frame(nc, WEBSOCKET_OP_CONTINUE, "ghi", 3); + + { + /* + * As a response to Ping, we should first receive Pong (0x8a), + * and then text message (0x81) with our Ping (0x89) echo. + * And then, our fragmented text message: abcdefghi. + */ + const char *expected = + "CONTROL:8a:[0123]81:[CONTROL:89:{0123}]81:[81:{abcdefghi}]"; + poll_until(&mgr, 1, c_int_eq, &mb.len, (void *) strlen(expected)); + mbuf_append(&mb, "\0", 1); + ASSERT_STREQ(mb.buf, expected); + } + + /* + * Test illegal text frame in the middle of a fragmented message + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + ASSERT_EQ(s_ws_client1_connected, 1); + + /* Send a few parts of a fragmented message */ + mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT | WEBSOCKET_DONT_FIN, "abc", 3); + mg_send_websocket_frame(nc, WEBSOCKET_OP_CONTINUE | WEBSOCKET_DONT_FIN, "def", + 3); + + /* Send (illegal) text frame in the middle of a fragmented message */ + mg_send_websocket_frame(nc, WEBSOCKET_OP_TEXT, "ghi", 3); + + /* Wait until connection is closed by the server */ + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 0); + ASSERT_EQ(s_ws_client1_connected, 0); + + /* Verify the error message */ + mbuf_append(&mb, "\0", 1); + ASSERT_STREQ( + mb.buf, + "CONTROL:88:[non-continuation in the middle of a fragmented message]"); + + /* + * Test closing by the client + */ + mb.len = 0; + s_ws_client1_connected = 0; + ASSERT((nc = mg_connect(&mgr, local_addr, cb_ws_client1)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = &mb; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 1); + ASSERT_EQ(s_ws_client1_connected, 1); + + /* Send a few parts of a fragmented message */ + mg_send_websocket_frame(nc, WEBSOCKET_OP_CLOSE, "bye", 3); + + /* Wait until connection is closed by the server */ + poll_until(&mgr, 1, c_int_ne, &s_ws_client1_connected, (void *) 0); + ASSERT_EQ(s_ws_client1_connected, 0); + /* + * TODO(dfrank): mongoose closes the connection automatically when + * mg_send_websocket_frame() is called with a WEBSOCKET_OP_CLOSE op, so we + * can't hear anything from the server. Server is actually obliged to send + * the control frame in response, but we can't test it here. + */ + + mg_mgr_free(&mgr); + mbuf_free(&mb); + + return NULL; +} + +static void cbwep(struct mg_connection *c, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + char *buf = (char *) c->user_data; + + switch (ev) { + case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: + strcat(buf, "R"); + break; + case MG_EV_WEBSOCKET_HANDSHAKE_DONE: + strcat(buf, "D"); + break; + case MG_EV_WEBSOCKET_FRAME: + strcat(buf, "F"); + mg_printf_websocket_frame(c, WEBSOCKET_OP_TEXT, "%s|%.*s", buf, + (int) wm->size, wm->data); + break; + } +} + +static const char *test_websocket_endpoint(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *local_addr = "127.0.0.1:7798"; + char buf[20] = "", buf2[20] = ""; + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, local_addr, cb3)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = buf2; + mg_register_http_endpoint(nc, "/boo", cbwep MG_UD_ARG(NULL)); + + /* Websocket request */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb4)) != NULL); + mg_set_protocol_http_websocket(nc); + nc->user_data = buf; + mg_send_websocket_handshake(nc, "/boo", NULL); + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + mg_mgr_free(&mgr); + + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(buf, "RDF|hi"); + + return NULL; +} + +static const char *test_connect_ws(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *urls[] = {"ws://127.0.0.1:7778/ws", "http://127.0.0.1:7778/ws", + "127.0.0.1:7778/ws", NULL}; + const char **url; + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, "127.0.0.1:7778", cb3)) != NULL); + mg_set_protocol_http_websocket(nc); + + for (url = urls; *url != NULL; url++) { + char buf[20] = ""; + ASSERT((nc = mg_connect_ws(&mgr, cb4, "ws://127.0.0.1:7778/ws", NULL, + NULL)) != NULL); + nc->user_data = buf; + poll_until(&mgr, 1, c_str_ne, buf, (void *) ""); + ASSERT_STREQ(buf, "A"); + } + + mg_mgr_free(&mgr); + + return NULL; +} + +struct big_payload_params { + size_t size; + char *buf; +}; + +static void cb3_big(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + + if (ev == MG_EV_WEBSOCKET_FRAME) { + int success = 1; + size_t i; + for (i = 0; i < wm->size; i++) { + if (wm->data[i] != 'x') { + success = 0; + break; + } + } + mg_printf_websocket_frame(nc, WEBSOCKET_OP_TEXT, "%s", + success ? "success" : "fail"); + } +} + +static void cb4_big(struct mg_connection *nc, int ev, void *ev_data) { + struct websocket_message *wm = (struct websocket_message *) ev_data; + struct big_payload_params *params = + (struct big_payload_params *) nc->user_data; + + if (ev == MG_EV_WEBSOCKET_FRAME) { + memcpy(params->buf, wm->data, wm->size); + mg_send_websocket_frame(nc, WEBSOCKET_OP_CLOSE, NULL, 0); + } else if (ev == MG_EV_WEBSOCKET_HANDSHAKE_DONE) { + /* Send large payload to server. server must reply "success". */ + char *payload = (char *) malloc(params->size); + memset(payload, 'x', params->size); + mg_printf_websocket_frame(nc, WEBSOCKET_OP_TEXT, "%.*s", params->size, + payload); + free(payload); + } +} + +/* Big payloads follow a different code path because it will use the extended + * length field and possibly mg_avprintf will need to reallocate the buffer. */ +static const char *test_websocket_big(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *local_addr = "127.0.0.1:7778"; + char buf[20] = ""; + struct big_payload_params params; + params.buf = buf; + + mg_mgr_init(&mgr, NULL); + /* mgr.hexdump_file = "-"; */ + ASSERT((nc = mg_bind(&mgr, local_addr, cb3_big)) != NULL); + mg_set_protocol_http_websocket(nc); + + /* Websocket request */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb4_big)) != NULL); + mg_set_protocol_http_websocket(nc); + params.size = 8192; + nc->user_data = ¶ms; + params.buf[0] = '\0'; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_str_ne, params.buf, (void *) ""); + + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(buf, "success"); + + /* Websocket request */ + ASSERT((nc = mg_connect(&mgr, local_addr, cb4_big)) != NULL); + mg_set_protocol_http_websocket(nc); + params.size = 65535; + nc->user_data = ¶ms; + params.buf[0] = '\0'; + mg_send_websocket_handshake(nc, "/ws", NULL); + poll_until(&mgr, 1, c_str_ne, params.buf, (void *) ""); + mg_mgr_free(&mgr); + + /* Check that test buffer has been filled by the callback properly. */ + ASSERT_STREQ(buf, "success"); + + return NULL; +} + +static const char *test_mqtt_handshake(void) { + struct mg_send_mqtt_handshake_opts opts; + struct mg_connection *nc = create_test_connection(); + const char *client_id = "testclient"; + const char *user_name = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"; + const char *password = "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB"; + const char *will_topic = "CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC"; + const char *will_message = "DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD"; + double before = mg_time(); + const char *got; + + msleep(2); + mg_set_protocol_mqtt(nc); + mg_send_mqtt_handshake(nc, client_id); + got = nc->send_mbuf.buf; + /* handshake header + keepalive + client id len + client id */ + ASSERT_EQ(nc->send_mbuf.len, + 1 + 1 + 1 + 5 + 1 + 1 + 2 + 2 + strlen(client_id)); + ASSERT_EQ(got[0], 0x10); + ASSERT_EQ(got[1], (int) nc->send_mbuf.len - 2); + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 4); + ASSERT_STREQ_NZ(&got[4], "MQTT"); + ASSERT_EQ(got[8], 4); + ASSERT_EQ(got[9], 0); /* connect flags */ + ASSERT_EQ(got[10], 0); + ASSERT_EQ(got[11], 60); + ASSERT_EQ(got[12], 0); + ASSERT_EQ(got[13], (char) strlen(client_id)); + ASSERT_EQ(strncmp(&got[14], client_id, strlen(client_id)), 0); + ASSERT_EQ(((struct mg_mqtt_proto_data *) nc->proto_data)->keep_alive, 60); + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len); + + memset(&opts, 0, sizeof(opts)); + before = mg_time(); + msleep(2); + opts.will_topic = will_topic; + opts.will_message = will_message; + opts.user_name = user_name; + opts.password = password; + opts.keep_alive = 0x1234; + mg_send_mqtt_handshake_opt(nc, client_id, opts); + got = nc->send_mbuf.buf; + /* handshake header + keepalive + client id len + client id */ + ASSERT_EQ(nc->send_mbuf.len, 1 + 2 + 1 + 5 + 1 + 1 + 2 + 2 + + strlen(client_id) + 2 + strlen(will_topic) + + 2 + strlen(will_message) + 2 + + strlen(user_name) + 2 + strlen(password)); + ASSERT_EQ(got[0], 0x10); + ASSERT_EQ(((unsigned char *) got)[1], 223); + ASSERT_EQ(got[2], 1); + ASSERT_EQ(got[3], 0); + ASSERT_EQ(got[4], 4); + ASSERT_STREQ_NZ(&got[5], "MQTT"); + ASSERT_EQ(got[9], 4); + ASSERT_EQ(((unsigned char *) got)[10], 0xc4); /* connect flags */ + ASSERT_EQ(got[11], 0x12); + ASSERT_EQ(got[12], 0x34); + ASSERT_EQ(got[13], 0); + ASSERT_EQ(got[14], (char) strlen(client_id)); + ASSERT_EQ(strncmp(&got[15], client_id, strlen(client_id)), 0); + ASSERT_EQ(((struct mg_mqtt_proto_data *) nc->proto_data)->keep_alive, 0x1234); + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_publish(void) { + struct mg_connection *nc = create_test_connection(); + const double before = mg_time(); + char data[] = "dummy"; + const char *got; + + mg_set_protocol_mqtt(nc); + msleep(2); + mg_mqtt_publish(nc, "/test", 42, MG_MQTT_QOS(1) | MG_MQTT_RETAIN, data, + sizeof(data)); + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 17); + + ASSERT(got[0] & MG_MQTT_RETAIN); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_PUBLISH << 4)); + ASSERT_EQ(MG_MQTT_GET_QOS(got[0]), 1); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 5); + ASSERT_STREQ_NZ(&got[4], "/test"); + + ASSERT_EQ(got[9], 0); + ASSERT_EQ(got[10], 42); + + ASSERT_EQ(strncmp(&got[11], data, sizeof(data)), 0); + + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_subscribe(void) { + struct mg_connection *nc = create_test_connection(); + const double before = mg_time(); + const int qos = 1; + const char *got; + struct mg_mqtt_topic_expression topic_expressions[] = {{"/stuff", qos}}; + mg_set_protocol_mqtt(nc); + msleep(2); + + mg_mqtt_subscribe(nc, topic_expressions, 1, 42); + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 13); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_SUBSCRIBE << 4)); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 42); + + ASSERT_EQ(got[4], 0); + ASSERT_EQ(got[5], 6); + ASSERT_STREQ_NZ(&got[6], "/stuff"); + ASSERT_EQ(got[12], qos); + + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_unsubscribe(void) { + struct mg_connection *nc = create_test_connection(); + char *topics[] = {(char *) "/stuff"}; + const double before = mg_time(); + const char *got; + mg_set_protocol_mqtt(nc); + msleep(2); + + mg_mqtt_unsubscribe(nc, topics, 1, 42); + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 12); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_UNSUBSCRIBE << 4)); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 42); + + ASSERT_EQ(got[4], 0); + ASSERT_EQ(got[5], 6); + ASSERT_STREQ_NZ(&got[6], "/stuff"); + + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_connack(void) { + struct mg_connection *nc = create_test_connection(); + const char *got; + mg_set_protocol_mqtt(nc); + + mg_mqtt_connack(nc, 42); + got = nc->send_mbuf.buf; + ASSERT(nc->send_mbuf.len > 0); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_CONNACK << 4)); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(got[3], 42); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_suback(void) { + struct mg_connection *nc = create_test_connection(); + uint8_t qoss[] = {1}; + const char *got; + mg_set_protocol_mqtt(nc); + + mg_mqtt_suback(nc, qoss, 1, 42); + + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 5); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_SUBACK << 4)); + ASSERT_EQ(MG_MQTT_GET_QOS(got[0]), 1); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 42); + ASSERT_EQ(got[4], 1); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_simple_acks(void) { + unsigned long i; + struct { + uint8_t cmd; + void (*f)(struct mg_connection *, uint16_t); + } cases[] = { + {MG_MQTT_CMD_PUBACK, mg_mqtt_puback}, + {MG_MQTT_CMD_PUBREC, mg_mqtt_pubrec}, + {MG_MQTT_CMD_PUBREL, mg_mqtt_pubrel}, + {MG_MQTT_CMD_PUBCOMP, mg_mqtt_pubcomp}, + {MG_MQTT_CMD_UNSUBACK, mg_mqtt_unsuback}, + }; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + struct mg_connection *nc = create_test_connection(); + const double before = mg_time(); + const char *got; + mg_set_protocol_mqtt(nc); + msleep(2); + + cases[i].f(nc, 42); + + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 4); + ASSERT_EQ((got[0] & 0xf0), (cases[i].cmd << 4)); + if (cases[i].cmd == MG_MQTT_CMD_PUBREL) { + ASSERT_EQ(got[0] & 0xf, 2); + } else { + ASSERT_EQ(got[0] & 0xf, 0); + } + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(got[2], 0); + ASSERT_EQ(got[3], 42); + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + + destroy_test_connection(nc); + } + return NULL; +} + +static const char *test_mqtt_nullary(void) { + unsigned long i; + struct { + uint8_t cmd; + void (*f)(struct mg_connection *); + } cases[] = { + {MG_MQTT_CMD_PINGREQ, mg_mqtt_ping}, + {MG_MQTT_CMD_PINGRESP, mg_mqtt_pong}, + {MG_MQTT_CMD_DISCONNECT, mg_mqtt_disconnect}, + }; + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + struct mg_connection *nc = create_test_connection(); + const double before = mg_time(); + struct mg_mqtt_message msg; + const char *got; + mg_set_protocol_mqtt(nc); + msleep(2); + + cases[i].f(nc); + + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 2); + ASSERT_EQ((got[0] & 0xf0), (cases[i].cmd << 4)); + ASSERT_EQ((size_t) got[1], (nc->send_mbuf.len - 2)); + ASSERT_EQ(parse_mqtt(&nc->send_mbuf, &msg), (int) nc->send_mbuf.len); + ASSERT_GT(((struct mg_mqtt_proto_data *) nc->proto_data)->last_control_time, + before); + + destroy_test_connection(nc); + } + return NULL; +} + +static const size_t mqtt_long_payload_len = 200; +static const size_t mqtt_very_long_payload_len = 20000; + +static void mqtt_eh(struct mg_connection *nc, int ev, void *ev_data) { + int *check = (int *) nc->user_data; + struct mg_mqtt_message *mm = (struct mg_mqtt_message *) ev_data; + size_t i; + (void) nc; + (void) ev_data; + + switch (ev) { + case MG_EV_MQTT_SUBACK: + *check = 1; + break; + case MG_EV_MQTT_PUBLISH: + *check = 0; + if (mg_vcmp(&mm->topic, "/topic")) { + *check = -1; + break; + } + + for (i = 0; i < mm->payload.len; i++) { + if (nc->recv_mbuf.buf[10 + i] != 'A') { + *check = -1; + break; + } + } + + if (mm->payload.len == mqtt_long_payload_len) { + *check = 2; + } else if (mm->payload.len == mqtt_very_long_payload_len) { + *check = 3; + } + break; + case MG_EV_MQTT_CONNACK: + *check = 4; + break; + } +} + +static const char *test_mqtt_parse_mqtt(void) { + struct mg_connection *nc = create_test_connection(); + char msg[] = {(char) (MG_MQTT_CMD_SUBACK << 4), 2, 1, 1}; + char *long_msg; + int check = 0; + int num_bytes = sizeof(msg), rest_len, i; + + nc->user_data = ✓ + nc->handler = mqtt_eh; + mg_set_protocol_mqtt(nc); + + mbuf_append(&nc->recv_mbuf, msg, num_bytes); + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + + ASSERT_EQ(check, 1); + mbuf_free(&nc->recv_mbuf); + + /* test a payload whose length encodes as two bytes */ + rest_len = 2 + 6 + mqtt_long_payload_len; + long_msg = (char *) malloc(512); + long_msg[0] = (char) (MG_MQTT_CMD_PUBLISH << 4); + long_msg[1] = (rest_len & 0x7f) | 0x80; + long_msg[2] = rest_len >> 7; + memcpy(&long_msg[3], "\0\006/topic", 8); + memset(&long_msg[11], 'A', mqtt_long_payload_len); + + num_bytes = 3 + rest_len; + mbuf_append(&nc->recv_mbuf, long_msg, num_bytes); + + /* Short read: handler is not run and data is not consumed */ + for (i = 0; i < num_bytes; i++) { + check = 123; + nc->recv_mbuf.len = i; + nc->proto_handler(nc, MG_EV_RECV, &i); + ASSERT_EQ((int) nc->recv_mbuf.len, i); + ASSERT_EQ(check, 123); + } + + nc->recv_mbuf.len = num_bytes; + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + ASSERT_EQ(check, 2); + mbuf_free(&nc->recv_mbuf); + free(long_msg); + + /* test a payload whose length encodes as two bytes */ + rest_len = 8 + mqtt_very_long_payload_len; + long_msg = (char *) malloc(20100); + long_msg[0] = (char) (MG_MQTT_CMD_PUBLISH << 4); + long_msg[1] = (rest_len & 127) | 0x80; + long_msg[2] = ((rest_len >> 7) & 127) | 0x80; + long_msg[3] = (rest_len >> 14); + memcpy(&long_msg[4], "\0\006/topic", 8); + memset(&long_msg[12], 'A', mqtt_very_long_payload_len); + + num_bytes = 4 + rest_len; + mbuf_append(&nc->recv_mbuf, long_msg, num_bytes); + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + + ASSERT_EQ(check, 3); + mbuf_remove(&nc->recv_mbuf, nc->recv_mbuf.len); + + /* Message length encodings > 4 bytes are not allowed by the standard, + * connections should be closed immediately. */ + long_msg[0] = (char) (MG_MQTT_CMD_PUBLISH << 4); + long_msg[1] = 0xff; + long_msg[2] = 0xff; + long_msg[3] = 0xff; + long_msg[4] = 0xff; + long_msg[5] = 0xff; + mbuf_append(&nc->recv_mbuf, long_msg, 10); + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + ASSERT(nc->flags & MG_F_CLOSE_IMMEDIATELY); + + mbuf_free(&nc->recv_mbuf); + free(long_msg); + + /* test encoding a large payload */ + long_msg = (char *) malloc(mqtt_very_long_payload_len); + memset(long_msg, 'A', mqtt_very_long_payload_len); + mg_mqtt_publish(nc, "/topic", 0, 0, long_msg, mqtt_very_long_payload_len); + nc->recv_mbuf = nc->send_mbuf; + mbuf_init(&nc->send_mbuf, 0); + num_bytes = nc->recv_mbuf.len; + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + + ASSERT_EQ(check, 3); + mbuf_free(&nc->recv_mbuf); + free(long_msg); + + /* test connack parsing */ + mg_mqtt_connack(nc, 0); + nc->recv_mbuf = nc->send_mbuf; + mbuf_init(&nc->send_mbuf, 0); + num_bytes = 4; + nc->proto_handler(nc, MG_EV_RECV, &num_bytes); + + ASSERT_EQ(check, 4); + mbuf_free(&nc->recv_mbuf); + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_parse_mqtt_qos1(void) { + /* clang-format off */ + struct mg_mqtt_message mm; + char msg_qos1[] = { + ((MG_MQTT_CMD_PUBLISH << 4) | (1 << 1)), + 2 + 6 + 2 + 7, + 0, 6, '/', 't', 'o', 'p', 'i', 'c', + 0x12, 0x34, + 'p', 'a', 'y', 'l', 'o', 'a', 'd', + }; + /* clang-format on */ + struct mbuf mb; + memset(&mm, 0, sizeof(mm)); + mbuf_init(&mb, 0); + mbuf_append(&mb, msg_qos1, sizeof(msg_qos1)); + ASSERT_EQ(parse_mqtt(&mb, &mm), (int) sizeof(msg_qos1)); + ASSERT_EQ(mm.cmd, MG_MQTT_CMD_PUBLISH); + ASSERT_EQ(mm.qos, 1); + ASSERT_EQ(mm.message_id, 0x1234); + ASSERT_EQ(mm.topic.len, 6); + ASSERT_STREQ_NZ(mm.topic.p, "/topic"); + ASSERT_EQ(mm.payload.len, 7); + ASSERT_STREQ_NZ(mm.payload.p, "payload"); + mbuf_free(&mb); + return NULL; +} + +static const char *test_mqtt_match_topic_expression(void) { + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo", mg_mk_str("foo")), 1); + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo", mg_mk_str("foo/")), 0); + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo", mg_mk_str("foo/bar")), 0); + + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo/#", mg_mk_str("foo")), 0); + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo/#", mg_mk_str("foo/")), 0); + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo/#", mg_mk_str("foo/bar")), 1); + ASSERT_EQ(mg_mqtt_vmatch_topic_expression("foo/#", mg_mk_str("foo/bar/baz")), + 1); + + return NULL; +} + +#if MG_ENABLE_MQTT_BROKER +struct mg_mqtt_topic_expression brk_test_te[] = {{"/dummy", 0}, {"/unit/#", 0}}; + +static void brk_cln_cb1(struct mg_connection *nc, int ev, void *p) { + struct mg_mqtt_message *msg = (struct mg_mqtt_message *) p; + + switch (ev) { + case MG_EV_CONNECT: + mg_set_protocol_mqtt(nc); + mg_send_mqtt_handshake(nc, "dummy"); + break; + case MG_EV_MQTT_CONNACK: + mg_mqtt_subscribe(nc, brk_test_te, ARRAY_SIZE(brk_test_te), 42); + break; + case MG_EV_MQTT_SUBACK: + mg_mqtt_publish(nc, "/unit/test", 0, MG_MQTT_QOS(0), "payload", 7); + break; + case MG_EV_MQTT_PUBLISH: + if (mg_vcmp(&msg->topic, "/unit/test") == 0 && msg->payload.len == 7 && + mg_vcmp(&msg->payload, "payload") == 0) { + *(int *) nc->user_data = 1; + } + break; + } +} + +static void mqtt_dummy_eh(struct mg_connection *nc, int ev, void *ev_data) { + (void) nc; + (void) ev; + (void) ev_data; +} + +static const char *test_mqtt_client_keep_alive(void) { + struct mg_send_mqtt_handshake_opts opts; + struct mg_connection *nc = create_test_connection(); + struct mg_mqtt_proto_data *pd; + double before = mg_time(); + const char *got; + mg_set_protocol_mqtt(nc); + pd = (struct mg_mqtt_proto_data *) nc->proto_data; + nc->handler = mqtt_dummy_eh; + + memset(&opts, 0, sizeof(opts)); + before = mg_time(); + msleep(2); + opts.keep_alive = 2; + mg_send_mqtt_handshake_opt(nc, "testclient", opts); + ASSERT_EQ(pd->keep_alive, 2); + ASSERT_GT(pd->last_control_time, before); + mbuf_remove(&nc->send_mbuf, nc->send_mbuf.len); /* Remove the CONNECT. */ + + before = pd->last_control_time; + msleep(2); + nc->proto_handler(nc, MG_EV_POLL, NULL); + ASSERT_EQ(nc->send_mbuf.len, 0); + ASSERT_EQ(pd->last_control_time, before); + sleep(1); + nc->proto_handler(nc, MG_EV_POLL, NULL); + ASSERT_EQ(nc->send_mbuf.len, 0); /* Not yet */ + ASSERT_EQ(pd->last_control_time, before); + sleep(1); + msleep(2); + nc->proto_handler(nc, MG_EV_POLL, NULL); + ASSERT_GT(nc->send_mbuf.len, 0); /* PINGREQ sent */ + got = nc->send_mbuf.buf; + ASSERT_EQ(nc->send_mbuf.len, 2); + ASSERT_EQ((got[0] & 0xf0), (MG_MQTT_CMD_PINGREQ << 4)); + ASSERT_GT(pd->last_control_time, before); /* Resets itself */ + + destroy_test_connection(nc); + return NULL; +} + +static const char *test_mqtt_broker(void) { + struct mg_mgr mgr; + struct mg_mqtt_broker brk; + struct mg_connection *brk_nc; + struct mg_connection *cln_nc; + const char *brk_local_addr = "127.0.0.1:7777"; + int brk_data = 0, cln_data = 0; + + mg_mgr_init(&mgr, NULL); + mg_mqtt_broker_init(&brk, &brk_data); + + ASSERT((brk_nc = mg_bind(&mgr, brk_local_addr, mg_mqtt_broker)) != NULL); + brk_nc->priv_2 = &brk; + + ASSERT((cln_nc = mg_connect(&mgr, brk_local_addr, brk_cln_cb1)) != NULL); + cln_nc->user_data = &cln_data; + + /* Run event loop. Use more cycles to let client and broker communicate. */ + poll_until(&mgr, 1, c_int_eq, &cln_data, (void *) 1); + + ASSERT_EQ(cln_data, 1); + + mg_mgr_free(&mgr); + + return NULL; +} +#endif /* MG_ENABLE_MQTT_BROKER */ + +static void cb5(struct mg_connection *nc, int ev, void *ev_data) { + switch (ev) { + case MG_EV_CONNECT: + sprintf((char *) nc->user_data, "%d", *(int *) ev_data); + break; + default: + break; + } +} + +static const char *test_connect_fail(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + char buf[100] = "0"; + + mg_mgr_init(&mgr, NULL); + ASSERT((nc = mg_connect(&mgr, "127.0.0.1:33211", cb5)) != NULL); + nc->user_data = buf; + poll_until(&mgr, 1, c_str_ne, buf, (void *) "0"); + mg_mgr_free(&mgr); + +/* printf("failed connect status: [%s]\n", buf); */ +/* TODO(lsm): fix this for Win32 */ +#ifndef _WIN32 + ASSERT(strcmp(buf, "0") != 0); +#endif + + return NULL; +} + +static void cb6(struct mg_connection *nc, int ev, void *ev_data) { + (void) ev; + (void) ev_data; + nc->flags |= MG_F_USER_4; + nc->flags |= MG_F_WANT_READ; /* Should not be allowed. */ +} + +static const char *test_connect_opts(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + struct mg_connect_opts opts; + + memset(&opts, 0, sizeof(opts)); + opts.user_data = (void *) 0xdeadbeef; + opts.flags = MG_F_USER_6; + opts.flags |= MG_F_WANT_READ; /* Should not be allowed. */ + + mg_mgr_init(&mgr, NULL); + ASSERT((nc = mg_connect_opt(&mgr, "127.0.0.1:33211", cb6, opts)) != NULL); + ASSERT(nc->user_data == (void *) 0xdeadbeef); + ASSERT(nc->flags & MG_F_USER_6); + ASSERT(!(nc->flags & MG_F_WANT_READ)); + /* TODO(rojer): find a way to test this w/o touching nc (already freed). + poll_mgr(&mgr, 25); + ASSERT(nc->flags & MG_F_USER_4); + ASSERT(nc->flags & MG_F_USER_6); + ASSERT(!(nc->flags & MG_F_WANT_READ)); + */ + mg_mgr_free(&mgr); + return NULL; +} + +static const char *test_connect_opts_error_string(void) { + struct mg_mgr mgr; + struct mg_connection *nc; + struct mg_connect_opts opts; + const char *error_string = NULL; + + memset(&opts, 0, sizeof(opts)); + opts.error_string = &error_string; + + mg_mgr_init(&mgr, NULL); + ASSERT((nc = mg_connect_opt(&mgr, "127.0.0.1:65537", cb6, opts)) == NULL); + ASSERT(error_string != NULL); + ASSERT_STREQ(error_string, "cannot parse address"); + mg_mgr_free(&mgr); + return NULL; +} + +#ifndef NO_DNS_TEST +static const char *test_resolve(void) { + char buf[20]; + + ASSERT(mg_resolve("localhost", buf, sizeof(buf)) > 0); + ASSERT_STREQ(buf, "127.0.0.1"); + + ASSERT_EQ(mg_resolve("please_dont_name_a_host_like_ths", buf, sizeof(buf)), + 0); + return NULL; +} +#endif + +static const char *test_base64(void) { + const char *cases[] = {"test", "longer string"}; + unsigned long i; + char enc[8192]; + char dec[8192]; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + mg_base64_encode((unsigned char *) cases[i], strlen(cases[i]), enc); + mg_base64_decode((unsigned char *) enc, strlen(enc), dec); + + ASSERT_EQ(strcmp(cases[i], dec), 0); + } + + ASSERT_EQ(mg_base64_decode((unsigned char *) "кю", 4, dec), 0); + ASSERT_EQ(mg_base64_decode((unsigned char *) "AAAA----", 8, dec), 4); + ASSERT_EQ(mg_base64_decode((unsigned char *) "Q2VzYW50YQ==", 12, dec), 12); + ASSERT_STREQ(dec, "Cesanta"); + + return NULL; +} + +static const char *test_sock_addr_to_str(void) { + char buf[60]; + buf[0] = '\0'; + { + union socket_address a4; + memset(&a4, 0, sizeof(a4)); + a4.sa.sa_family = AF_INET; + a4.sin.sin_addr.s_addr = inet_addr("127.0.0.1"); + a4.sin.sin_port = htons(12345); + mg_sock_addr_to_str(&a4, buf, sizeof(buf), 0); + ASSERT_STREQ(buf, ""); + mg_sock_addr_to_str(&a4, buf, sizeof(buf), MG_SOCK_STRINGIFY_IP); + ASSERT_STREQ(buf, "127.0.0.1"); + mg_sock_addr_to_str(&a4, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT); + ASSERT_STREQ(buf, "12345"); + mg_sock_addr_to_str(&a4, buf, sizeof(buf), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + ASSERT_STREQ(buf, "127.0.0.1:12345"); + } +#if MG_ENABLE_IPV6 && !defined(_WIN32) + { + union socket_address a6; + memset(&a6, 0, sizeof(a6)); + a6.sa.sa_family = AF_INET6; + ASSERT_EQ(inet_pton(AF_INET6, "2001::123", &a6.sin6.sin6_addr), 1); + a6.sin6.sin6_port = htons(12345); + mg_sock_addr_to_str(&a6, buf, sizeof(buf), 0); + ASSERT_STREQ(buf, ""); + mg_sock_addr_to_str(&a6, buf, sizeof(buf), MG_SOCK_STRINGIFY_IP); + ASSERT_STREQ(buf, "2001::123"); + mg_sock_addr_to_str(&a6, buf, sizeof(buf), MG_SOCK_STRINGIFY_PORT); + ASSERT_STREQ(buf, "12345"); + mg_sock_addr_to_str(&a6, buf, sizeof(buf), + MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT); + ASSERT_STREQ(buf, "[2001::123]:12345"); + } +#endif + return NULL; +} + +static const char *test_hexdump(void) { + const char *src = "\1\2\3\4abcd"; + char got[256]; + + const char *want = + "0000 01 02 03 04 61 62 63 64" + " ....abcd\n"; + ASSERT_EQ(mg_hexdump(src, strlen(src), got, sizeof(got)), (int) strlen(want)); + ASSERT_STREQ(got, want); + return NULL; +} + +static const char *test_hexdump_file(void) { + const char *path = "test_hexdump"; + char *data, *got; + size_t size; + FILE *fp; + struct mg_connection *nc = create_test_connection(); + + /* "In the GNU system, non-null pointers are printed as unsigned integers, + * as if a `%#x' conversion were used. Null pointers print as `(nil)'. + * (Pointers might print differently in other systems.)" + * indeed it prints 0x0 on apple. + */ + nc->user_data = (void *) 0xbeef; + + /* truncate file. open+O_TRUNC works on wine but not on real windows */ + fp = fopen(path, "w"); + ASSERT(fp != NULL); + fclose(fp); + + mbuf_append(&nc->send_mbuf, "foo", 3); + mbuf_append(&nc->recv_mbuf, "bar", 3); + mg_hexdump_connection(nc, path, nc->send_mbuf.buf, 3, MG_EV_SEND); + + mbuf_free(&nc->send_mbuf); + mbuf_free(&nc->recv_mbuf); + free(nc); + + ASSERT((data = read_file(path, &size)) != NULL); + unlink(path); + + got = data; + while (got - data < (int) size && *got++ != ' ') + ; + size -= got - data; + + ASSERT(strstr(got, + "0000 66 6f 6f " + " foo") != NULL); + + free(data); + return NULL; +} + +static const char *test_basic_auth_helpers() { + struct mbuf buf; + mbuf_init(&buf, 0); + mg_basic_auth_header(mg_mk_str("foo"), mg_mk_str("bar"), &buf); + ASSERT_EQ(buf.len, 35); + ASSERT_EQ(memcmp(buf.buf, "Authorization: Basic Zm9vOmJhcg==\r\n", 35), 0); + + buf.len = 0; + mg_basic_auth_header(mg_mk_str("foo:bar"), mg_mk_str_n(NULL, 0), &buf); + ASSERT_EQ(buf.len, 35); + ASSERT_EQ(memcmp(buf.buf, "Authorization: Basic Zm9vOmJhcg==\r\n", 35), 0); + + mbuf_free(&buf); + + { + char user[256]; + char pass[256]; + size_t i; + + struct { + const char *hdr; + const char *user; + const char *pass; + int res; + } cases[] = { + {"Basic Zm9vOmJhcg==", "foo", "bar", 0}, /* foo:bar */ + {"Basic Zm9v", "foo", "", 0}, /* foo */ + {"Basic Zm9vOmJhcjpiYXo=", "foo", "bar:baz", 0}, /* foo:bar:baz */ + {"Basic Zm9vOg==", "foo", "", 0}, /* foo: */ + {"Basic OmJhcg==", "", "", -1}, /* :bar */ + }; + + for (i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + struct mg_str hdr = mg_mk_str(cases[i].hdr); + int res; + + memset(user, 0, sizeof(user)); + memset(pass, 0, sizeof(pass)); + res = mg_parse_http_basic_auth(&hdr, user, sizeof(user), pass, + sizeof(pass)); + ASSERT_EQ(res, cases[i].res); + ASSERT_EQ(strcmp(user, cases[i].user), 0); + ASSERT_EQ(strcmp(pass, cases[i].pass), 0); + } + } + return NULL; +} + +static void test_auth_server_handler(struct mg_connection *nc, int ev, + void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + struct mg_str *hdr; + char user[256] = ""; + char pass[256] = ""; + int res; + + switch (ev) { + case MG_EV_WEBSOCKET_HANDSHAKE_REQUEST: + case MG_EV_HTTP_REQUEST: { + hdr = mg_get_http_header(hm, "Sec-WebSocket-Protocol"); + if (hdr != NULL) { + if (mg_vcmp(hdr, "myproto") == 0) { + *(int *) nc->user_data = 2; + break; + } + } + + res = mg_get_http_basic_auth(hm, user, sizeof(user), pass, sizeof(pass)); + if (res == -1) { + *(int *) nc->user_data = -1; + break; + } + + if (strcmp(user, "foo") == 0 && strcmp(pass, "bar") == 0) { + *(int *) nc->user_data = 1; + } else { + *(int *) nc->user_data = -2; + } + + break; + } + } +} + +static void test_auth_client_handler(struct mg_connection *nc, int ev, + void *ev_data) { + (void) nc; + (void) ev; + (void) ev_data; +} + +static const char *test_http_auth() { + int connected; + struct mg_connection *nc; + struct mg_mgr mgr; + mg_mgr_init(&mgr, NULL); + + nc = mg_bind(&mgr, "127.0.0.1:1234", test_auth_server_handler); + mg_set_protocol_http_websocket(nc); + nc->user_data = &connected; + + connected = 0; + mg_connect_http(&mgr, test_auth_client_handler, + "http://foo:bar@127.0.0.1:1234", NULL, NULL); + poll_until(&mgr, 4, c_int_eq, &connected, (void *) 1); + ASSERT_EQ(connected, 1); + + connected = 0; + mg_connect_ws(&mgr, test_auth_client_handler, "ws://foo:bar@127.0.0.1:1234", + NULL, NULL); + poll_until(&mgr, 4, c_int_eq, &connected, (void *) 1); + ASSERT_EQ(connected, 1); + + /* check that we didn't break proto or other headers */ + connected = 0; + mg_connect_ws(&mgr, test_auth_client_handler, "ws://127.0.0.1:1234", + "myproto", NULL); + poll_until(&mgr, 4, c_int_eq, &connected, (void *) 2); + ASSERT_EQ(connected, 2); + + mg_mgr_free(&mgr); + return NULL; +} + +void tunnel_server_test_handler(struct mg_connection *nc, int ev, + void *ev_data) { + (void) ev_data; + switch (ev) { + case MG_EV_HTTP_REQUEST: + if (nc->user_data) break; + + nc->user_data = (void *) malloc(sizeof(int)); + (*(int *) nc->user_data) = 0; + mg_set_timer(nc, mg_time() + 0.01); + + mg_printf(nc, "%s", + "HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n"); + break; + case MG_EV_TIMER: + mg_printf_http_chunk(nc, "%d", (*(int *) nc->user_data)++); + if (*(int *) nc->user_data == 10) { + mg_send_http_chunk(nc, "", 0); + nc->flags |= MG_F_SEND_AND_CLOSE; + } else { + mg_set_timer(nc, mg_time() + 0.01); + } + break; + case MG_EV_CLOSE: + free(nc->user_data); + default: + break; + } +} + +void tunnel_client_test_handler(struct mg_connection *nc, int ev, + void *ev_data) { + struct http_message *hm = (struct http_message *) ev_data; + (void) nc; + (void) ev_data; + switch (ev) { + case MG_EV_HTTP_REPLY: + if (hm->body.len == 10 && strcmp(hm->body.p, "0123456789") == 0) { + *(int *) nc->user_data = 1; + } else { + fprintf(stderr, "want: 10, got:%d \"%.*s\"\n", (int) hm->body.len, + (int) hm->body.len, hm->body.p); + } + break; + default: + break; + } +} + +/* + * NOTE(mkm): this test requires compiling the unit_test.c file with + * //tuna/dispatcher.c . + * Windows test runner doesn't use a makefile that's checked in + * and I can't shave this yak now. + * This doesn't mean the tunnel is not supposed to work on windows; there + * is no fundamental reason it shouldn't, but obviously we should shave + * the yak and make it testable there as well. + */ +#ifndef _WIN32 +static const char *test_tunnel(void) { + struct mg_connection *client, *server; + struct mg_mgr mgr; + int sentinel = 0; + mg_mgr_init(&mgr, NULL); + + mg_tund_bind(&mgr, "localhost:4321"); + + server = mg_bind(&mgr, "ws://localhost:4321", tunnel_server_test_handler); + mg_set_protocol_http_websocket(server); + /* + * Connection happens only at the next poll (because we need to give the app + * a chance to set MG_F_TUN_DO_NOT_RECONNECT), so let's poll once + */ + mg_mgr_poll(&mgr, 1); + mg_mgr_poll(&mgr, 1); + client = mg_connect_http(&mgr, tunnel_client_test_handler, + "http://localhost:4321/foo", NULL, NULL); + client->user_data = (void *) &sentinel; + + poll_until(&mgr, 1, c_int_eq, &sentinel, (void *) 1); + ASSERT_EQ(sentinel, 1); + + mg_mgr_free(&mgr); + return NULL; +} +#endif + +static const char *test_http_chunk(void) { + struct mg_connection nc; + init_test_connection(&nc); + + mg_printf_http_chunk(&nc, "%d %s", 123, ":-)"); + ASSERT_EQ(nc.send_mbuf.len, 12); + ASSERT_EQ(memcmp(nc.send_mbuf.buf, "7\r\n123 :-)\r\n", 12), 0); + mbuf_free(&nc.send_mbuf); + + mg_send_http_chunk(&nc, "", 0); + ASSERT_EQ(nc.send_mbuf.len, 5); + ASSERT_EQ(memcmp(nc.send_mbuf.buf, "0\r\n\r\n", 3), 0); + mbuf_free(&nc.send_mbuf); + + return NULL; +} + +static int s_handle_chunk_event = 0; +static char s_events[100]; + +static void eh_chunk2(struct mg_connection *nc, int ev, void *ev_data) { + if (ev == MG_EV_HTTP_CHUNK) { + if (s_handle_chunk_event) { + nc->flags |= MG_F_DELETE_CHUNK; + } + } + snprintf(s_events + strlen(s_events), sizeof(s_events) - strlen(s_events), + "_%d", ev); + (void) ev_data; +} + +static const char *test_http_extra_headers(void) { + char buf[FETCH_BUF_SIZE]; + const char *expect = "MyHdr: my_val\r\n"; + s_http_server_opts.extra_headers = "MyHdr: my_val"; + fetch_http(buf, "%s", "GET / HTTP/1.0\r\n\r\n"); + ASSERT(strstr(buf, expect) != NULL); + s_http_server_opts.extra_headers = NULL; + return NULL; +} + +static const char *test_http_not_modified(void) { + struct http_message hm; + cs_stat_t st; + const char *req1 = + "GET / HTTP/1.0\nIf-None-Match: \"0.123\"\n" + "If-Modified-Since: Tue 17 Nov 2015 21:01:56 GMT\n\n"; + const char *req4 = + "GET / HTTP/1.0\nIf-None-Match: \"0.7\"\n" + "If-Modified-Since: Tue 17 Nov 2015 21:01:56 GMT\n\n"; + const char *req2 = + "GET / HTTP/1.0\n" + "If-Modified-Since: Tue 17 Nov 2015 21:01:56 GMT\n\n"; + const char *req3 = "GET / HTTP/1.0\n\n"; + + st.st_size = 7; + st.st_mtime = (time_t) 0; + + /* Not modified according to If-Modified-Since, but modified by Etag. */ + ASSERT(mg_parse_http(req1, strlen(req1), &hm, 1) > 0); + ASSERT(mg_is_not_modified(&hm, &st) == 0); + + /* Not modified according to If-Modified-Since, and no Etag. */ + ASSERT(mg_parse_http(req2, strlen(req2), &hm, 1) > 0); + ASSERT(mg_is_not_modified(&hm, &st) != 0); + + ASSERT(mg_parse_http(req3, strlen(req3), &hm, 1) > 0); + ASSERT(mg_is_not_modified(&hm, &st) == 0); + + ASSERT(mg_parse_http(req4, strlen(req4), &hm, 1) > 0); + ASSERT(mg_is_not_modified(&hm, &st) != 0); + + return NULL; +} + +static const char *test_http_chunk2(void) { + struct mg_connection nc; + struct http_message hm; + char buf[100] = "5\r\nhe"; + struct mg_mgr mgr; + mg_mgr_init(&mgr, NULL); + + memset(&nc, 0, sizeof(nc)); + memset(&hm, 0, sizeof(hm)); + nc.mgr = &mgr; + nc.sock = INVALID_SOCKET; + nc.handler = eh_chunk2; + hm.message.len = hm.body.len = ~0; + + s_handle_chunk_event = 0; + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 0); + + /* Simulate arrival of chunks. MG_EV_HTTP_CHUNK events are not handled. */ + strcat(buf, "llo\r"); + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 0); + ASSERT_STREQ(buf, "5\r\nhello\r"); + + strcat(buf, "\n"); + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 5); + ASSERT_STREQ(buf, "hello"); + + s_handle_chunk_event = 1; + strcat(buf, "3\r\n:-)\r\n"); + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 8); + ASSERT_STREQ(buf, ""); + + s_handle_chunk_event = 0; + strcat(buf, "3\r\n...\r\na\r\n0123456789\r\n0\r"); + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 13); + ASSERT_STREQ(buf, "...01234567890\r"); + ASSERT_EQ(hm.message.len, (size_t) ~0); + + strcat(buf, "\n\r\n"); + ASSERT_EQ(mg_handle_chunked(&nc, &hm, buf, strlen(buf)), 13); + ASSERT_STREQ(buf, "...0123456789"); + ASSERT_EQ(hm.message.len, 13); + + ASSERT_STREQ(s_events, "_102_102_102_102"); + + ASSERT(nc.proto_data != NULL); + nc.proto_data_destructor(nc.proto_data); + + mg_mgr_free(&mgr); + + return NULL; +} + +static const char *test_parse_date_string(void) { + ASSERT_EQ(mg_parse_date_string("Sat, 31 Oct 2019 10:51:12 GMT"), 1572519072); + ASSERT_EQ(mg_parse_date_string("31/Oct/2019 10:51:12 GMT"), 1572519072); + ASSERT_EQ(mg_parse_date_string("31 Oct 2019 10:51:12 GMT"), 1572519072); + ASSERT_EQ(mg_parse_date_string("31-Oct-2019 10:51:12 GMT"), 1572519072); + return NULL; +} + +#if MG_ENABLE_HTTP_STREAMING_MULTIPART +struct cb_mp_srv_data { + struct mbuf res; + int request_end; +}; + +static void cb_mp_srv(struct mg_connection *nc, int ev, void *p) { + struct cb_mp_srv_data *mpd = (struct cb_mp_srv_data *) nc->user_data; + struct mbuf *data = &mpd->res; + if (nc->listener == NULL) return; + + switch (ev) { + case MG_EV_HTTP_MULTIPART_REQUEST: { + struct http_message *hm = (struct http_message *) p; + mbuf_append(data, "<MPRQ", 5); + mbuf_append(data, hm->uri.p, hm->uri.len); + break; + } + case MG_EV_HTTP_PART_BEGIN: { + struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p; + if (mp->status < 0) { + mbuf_append(data, "ERROR", 5); + } else { + DBG(("BEGIN %s %s", mp->var_name, mp->file_name)); + mbuf_append(data, mp->var_name, strlen(mp->var_name)); + mbuf_append(data, mp->file_name, strlen(mp->file_name)); + } + break; + } + case MG_EV_HTTP_PART_DATA: { + struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p; + if (mp->status < 0) { + mbuf_append(data, "ERROR", 5); + } else { + DBG(("DATA %d", (int) mp->data.len)); + mbuf_append(data, mp->data.p, mp->data.len); + } + break; + } + case MG_EV_HTTP_PART_END: { + struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p; + struct mbuf *data = (struct mbuf *) nc->listener->user_data; + mbuf_append(data, mp->var_name, strlen(mp->var_name)); + mbuf_append(data, mp->file_name, strlen(mp->file_name)); + if (mp->status < 0) { + DBG(("PARTERR %s %s", mp->var_name, mp->file_name)); + mbuf_append(data, "ERROR", 5); + } else { + DBG(("END %s %s", mp->var_name, mp->file_name)); + mbuf_append(data, "FIN", 3); + } + break; + } + case MG_EV_HTTP_MULTIPART_REQUEST_END: { + struct mg_http_multipart_part *mp = (struct mg_http_multipart_part *) p; + DBG(("RQEND %p %p", (void *) mp->var_name, (void *) mp->file_name)); + mbuf_append(data, (mp->status < 0 ? "-" : "+"), 1); + mbuf_append(data, (mp->var_name == NULL ? "1" : "0"), 1); + mbuf_append(data, (mp->file_name == NULL ? "1" : "0"), 1); + mbuf_append(data, "MPRQ>", 5); + mpd->request_end = 1; + break; + } + } +} + +static void cb_mp_send_one_byte(struct mg_connection *nc, int ev, void *p) { + static int i; + (void) p; + if (ev == MG_EV_POLL) { + char ch = ((char *) nc->user_data)[i++]; + int l = strlen((char *) nc->user_data); + if (ch != '\0') { + mg_send(nc, &ch, 1); + DBG(("%p sent %d of %d", (void *) nc, i, l)); + } + } +} + +static void cb_mp_empty(struct mg_connection *nc, int ev, void *p) { + (void) nc; + (void) ev; + (void) p; +} + +static const char b1[] = + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111\r\n" + "111111111111111111111111111111111111111111111111111111111111111"; +static const char b2[] = + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n" + "222222222222222222222222222222222222222222222222222222222222222\r\n"; +static const char b4[] = + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444\r\n" + "444444444444444444444444444444444444444444444444444444444444444"; + +static const char *test_http_multipart_check_res(struct mbuf *res) { + const char *ptr = res->buf; + ASSERT_STREQ_NZ(ptr, "<MPRQ/test"); + ptr += 10; + ASSERT_STREQ_NZ(ptr, "afoo"); + ptr += 4; + ASSERT_STREQ_NZ(ptr, b1); + ptr += sizeof(b1) - 1; + ASSERT_STREQ_NZ(ptr, "afooFIN"); + ptr += 7; + /* No file_name for second part */ + ASSERT_STREQ_NZ(ptr, "b"); + ptr++; + ASSERT_STREQ_NZ(ptr, b2); + ptr += sizeof(b2) - 1; + ASSERT_STREQ_NZ(ptr, "bFIN"); + ptr += 4; + ASSERT_STREQ_NZ(ptr, "cbar"); + ptr += 4; + ASSERT_STREQ_NZ(ptr, b4); + ptr += sizeof(b4) - 1; + ASSERT_STREQ_NZ(ptr, "cbarFIN"); + ptr += 7; + ASSERT_STREQ_NZ(ptr, "+11MPRQ>"); + ptr += 8; + ASSERT_EQ((size_t)(ptr - res->buf), res->len); + return NULL; +} + +static const char *test_http_multipart2(void) { + struct mg_mgr mgr; + struct mg_connection *nc_listen; + + const char multi_part_req_fmt[] = + "%s" + "Content-Disposition: form-data; name=\"a\"; filename=\"foo\"\r\n" + "\r\n" + "%s" + "\r\n--Asrf456BGe4h\r\n" + "Content-Disposition: form-data; name=\"b\"\r\n" + "\r\n" + "%s" + "\r\n--Asrf456BGe4h\r\n" + "Content-Disposition: form-data; name=\"c\"; filename=\"bar\"\r\n" + "\r\n" + "%s" + "\r\n--Asrf456BGe4h--\r\n" + "\r\n"; + + char multi_part_req[1024 * 5]; + int i; + struct mg_connection *c; + const char *r; + + struct cb_mp_srv_data mpd; + memset(&mpd, 0, sizeof(mpd)); + mbuf_init(&mpd.res, 0); + + mg_mgr_init(&mgr, NULL); + nc_listen = mg_bind(&mgr, "8765", cb_mp_srv); + nc_listen->user_data = &mpd; + + mg_set_protocol_http_websocket(nc_listen); + + snprintf(multi_part_req, sizeof(multi_part_req), multi_part_req_fmt, "", b1, + b2, b4); + + ASSERT((c = mg_connect_http(&mgr, cb_mp_send_one_byte, + "http://127.0.0.1:8765/test", + "Content-Type: " + "multipart/form-data;boundary=Asrf456BGe4h", + "\r\n--Asrf456BGe4h\r\n")) != NULL); + + c->user_data = multi_part_req; + + poll_until(&mgr, 10, c_int_eq, &mpd.request_end, (void *) 1); + + if ((r = test_http_multipart_check_res(&mpd.res)) != NULL) return r; + + c->flags |= MG_F_CLOSE_IMMEDIATELY; + mbuf_free(&mpd.res); + memset(&mpd, 0, sizeof(mpd)); + mbuf_init(&mpd.res, 0); + + snprintf(multi_part_req, sizeof(multi_part_req), multi_part_req_fmt, + "\r\n--Asrf456BGe4h\r\n", b1, b2, b4); + + /* Testing delivering to endpoint handler*/ + nc_listen = mg_bind(&mgr, "8766", cb_mp_empty); + nc_listen->user_data = &mpd; + + mg_set_protocol_http_websocket(nc_listen); + mg_register_http_endpoint(nc_listen, "/test", cb_mp_srv MG_UD_ARG(NULL)); + + ASSERT((c = mg_connect_http(&mgr, cb_mp_empty, "http://127.0.0.1:8766/test", + "Connection: keep-alive\r\nContent-Type: " + "multipart/form-data; boundary=Asrf456BGe4h", + multi_part_req)) != NULL); + + poll_until(&mgr, 3, c_int_eq, &mpd.request_end, (void *) 1); + mg_mgr_poll(&mgr, 1); /* Consume epilogue. */ + + if ((r = test_http_multipart_check_res(&mpd.res)) != NULL) return r; + + /* Keep the connection alive */ + + mbuf_free(&mpd.res); + memset(&mpd, 0, sizeof(mpd)); + mbuf_init(&mpd.res, 0); + + mg_printf(c, + "POST /test HTTP/1.1\r\nHost: 127.0.0.1:8766\r\nContent-Length: " + "%" SIZE_T_FMT + "\r\nConnection: keep-alive\r\nContent-Type: multipart/form-data; " + "boundary=Asrf456BGe4h\r\n%s", + strlen(multi_part_req), multi_part_req); + + poll_until(&mgr, 3, c_int_eq, &mpd.request_end, (void *) 1); + + if ((r = test_http_multipart_check_res(&mpd.res)) != NULL) return r; + + c->flags |= MG_F_CLOSE_IMMEDIATELY; + + mbuf_free(&mpd.res); + memset(&mpd, 0, sizeof(mpd)); + mbuf_init(&mpd.res, 0); + mbuf_remove(&c->recv_mbuf, c->recv_mbuf.len); + + /* Test interrupted request */ + multi_part_req[1800] = '\0'; + c = mg_connect_http( + &mgr, cb_mp_empty, "http://127.0.0.1:8765", + "Content-Type: multipart/form-data; boundary=Asrf456BGe4h", + multi_part_req); + + ASSERT(c != NULL); + for (i = 0; i < 20; i++) { + mg_mgr_poll(&mgr, 1); + } + + c->flags |= MG_F_CLOSE_IMMEDIATELY; + + poll_until(&mgr, 3, c_int_eq, &mpd.request_end, (void *) 1); + + ASSERT_STREQ_NZ(mpd.res.buf + mpd.res.len - 17, "cbarERROR-11MPRQ>"); + + mbuf_free(&mpd.res); + memset(&mpd, 0, sizeof(mpd)); + mbuf_init(&mpd.res, 0); + + ASSERT( + mg_connect_http( + &mgr, cb_mp_empty, "http://127.0.0.1:8766/test", + "Content-Type: multipart/form-data; boundary=Asrf456BGe4h", + "\r\n--Asrf456BGe4h\r\n" + "Content-Disposition: form-data; name=\"d\"; filename=\"small\"\r\n" + "\r\n" + "boooo" + "\r\n--Asrf456BGe4h--\r\n" + "\r\n") != NULL); + + poll_until(&mgr, 3, c_int_eq, &mpd.request_end, (void *) 1); + + ASSERT(mpd.res.len != 0); + ASSERT_STREQ_NZ(mpd.res.buf, "<MPRQ/testdsmallboooodsmallFIN+11MPRQ>"); + mbuf_free(&mpd.res); + + /* + * Test malformed packet + * See https://github.com/cesanta/dev/issues/6974 + * This request should not lead to crash + */ + c = mg_connect(&mgr, "127.0.0.1:8766", cb_mp_empty); + mg_printf(c, + "POST /test HTTP/1.1\r\n" + "Connection: keep-alive\r\n" + "Content-Type: multipart/form-data; Content-Length: 1\r\n" + "1\r\n\r\n"); + for (i = 0; i < 20; i++) { + mg_mgr_poll(&mgr, 1); + } + + mg_mgr_free(&mgr); + + return NULL; +} +#endif /* MG_ENABLE_HTTP_STREAMING_MULTIPART */ + +static const char *test_http_multipart(void) { + struct http_message hm; + char buf[FETCH_BUF_SIZE] = "", var_name[100], file_name[100]; + const char *chunk; + size_t chunk_len, ofs; + + fetch_http(buf, "%s", "GET /data/multipart.txt HTTP/1.0\r\n\r\n"); + mg_parse_http(buf, strlen(buf), &hm, 1); + + ofs = mg_parse_multipart(hm.body.p, hm.body.len, var_name, sizeof(var_name), + file_name, sizeof(file_name), &chunk, &chunk_len); + ASSERT(ofs < hm.body.len); + ASSERT(ofs > 0); + ASSERT_EQ(chunk_len, 10); + ASSERT_EQ(memcmp(chunk, "file1 data", chunk_len), 0); + + ofs = mg_parse_multipart(hm.body.p + ofs, hm.body.len - ofs, var_name, + sizeof(var_name), file_name, sizeof(file_name), + &chunk, &chunk_len); + ASSERT(ofs < hm.body.len); + ASSERT(ofs > 0); + ASSERT_EQ(chunk_len, 10); + ASSERT_EQ(memcmp(chunk, "file2 data", chunk_len), 0); + + ofs = mg_parse_multipart(hm.body.p + ofs, hm.body.len - ofs, var_name, + sizeof(var_name), file_name, sizeof(file_name), + &chunk, &chunk_len); + ASSERT(ofs == 0); + + return NULL; +} + +static const char *test_dns_encode(void) { + struct mg_connection nc; + const char *got; + int query_types[] = {MG_DNS_A_RECORD, MG_DNS_AAAA_RECORD}; + size_t i; + init_test_connection(&nc); + + /* + * Testing TCP encoding since when the connection + * is in UDP mode the data is not stored in the send buffer. + */ + + for (i = 0; i < ARRAY_SIZE(query_types); i++) { + mg_send_dns_query(&nc, "www.cesanta.com", query_types[i]); + got = nc.send_mbuf.buf; + ASSERT_EQ(nc.send_mbuf.len, 12 + 4 + 13 + 4 + 2); + ASSERT_EQ(got[14], 3); + ASSERT_STREQ_NZ(&got[15], "www"); + ASSERT_EQ(got[18], 7); + ASSERT_STREQ_NZ(&got[19], "cesanta"); + ASSERT_EQ(got[26], 3); + ASSERT_STREQ_NZ(&got[27], "com"); + ASSERT_EQ(got[30], 0); + ASSERT_EQ(got[31], 0); + ASSERT_EQ(got[32], query_types[i]); + ASSERT_EQ(got[33], 0); + ASSERT_EQ(got[34], 1); + + mbuf_free(&nc.send_mbuf); + } + return NULL; +} + +static const char *test_dns_uncompress(void) { + /* + * Order or string constants is important. Names being uncompressed + * must be before the end of the DNS packet end. + */ + const char *s = + "\03www\07cesanta\03com\0\03www\xc0\06\05dummy\07cesanta\3com\0"; + struct mg_dns_message msg; + struct mg_str name = mg_mk_str(s); + struct mg_str comp_name = mg_mk_str_n(s + 17, 6); + struct mg_str n; + char dst[256]; + int len; + size_t i; + + const char *cases[] = {"www.cesanta.com", "www", "ww", "www.", "www.c"}; + + memset(&msg, 0, sizeof(msg)); + msg.pkt.p = s + 23; + msg.pkt.len = strlen(msg.pkt.p) + 1; + + memset(dst, 'X', sizeof(dst)); + n = mg_mk_str_n(msg.pkt.p, msg.pkt.len); + len = mg_dns_uncompress_name(&msg, &n, dst, sizeof(dst)); + ASSERT_EQ(len, 17); + ASSERT_EQ(len, (int) strlen(dst)); + ASSERT_STREQ_NZ(dst, "dummy.cesanta.com"); + + for (i = 0; i < ARRAY_SIZE(cases); i++) { + size_t l = strlen(cases[i]); + memset(dst, 'X', sizeof(dst)); + len = mg_dns_uncompress_name(&msg, &name, dst, l); + ASSERT_EQ(len, (int) l); + ASSERT_EQ(strncmp(dst, cases[i], l), 0); + ASSERT_EQ(dst[l], 'X'); + } + + /* if dst has enough space, check the trailing '\0' */ + memset(dst, 'X', sizeof(dst)); + len = mg_dns_uncompress_name(&msg, &name, dst, sizeof(dst)); + ASSERT_EQ(len, 15); + ASSERT_EQ(len, (int) strlen(dst)); + ASSERT_STREQ_NZ(dst, "www.cesanta.com"); + ASSERT_EQ(dst[15], 0); + + /* check compressed name */ + memset(dst, 'X', sizeof(dst)); + len = mg_dns_uncompress_name(&msg, &comp_name, dst, sizeof(dst)); + printf("%d [%s]\n", len, dst); + ASSERT_EQ(len, 15); + ASSERT_EQ(len, (int) strlen(dst)); + ASSERT_STREQ_NZ(dst, "www.cesanta.com"); + ASSERT_EQ(dst[15], 0); + + /* check parsing loop avoidance */ + msg.pkt.p = "\05dummy\xc0\x06\0"; + msg.pkt.len = strlen(msg.pkt.p) + 1; + memset(dst, 'X', sizeof(dst)); + len = mg_dns_uncompress_name(&msg, &comp_name, dst, sizeof(dst)); + ASSERT_EQ(len, 0); + + return NULL; +} + +static const char *test_dns_decode(void) { + struct mg_dns_message msg; + char name[256]; + const char *hostname = "go.cesanta.com"; + const char *cname = "ghs.googlehosted.com"; + struct mg_dns_resource_record *r; + uint16_t tiny; + struct in_addr ina; + int n; + + /* + * Response for a record A query host for `go.cesanta.com`. + * The response contains two answers: + * + * CNAME go.cesanta.com -> ghs.googlehosted.com + * A ghs.googlehosted.com -> 74.125.136.121 + * + * Captured from a reply generated by Google DNS server (8.8.8.8) + */ + const unsigned char pkt[] = { + 0xa1, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x67, 0x6f, 0x07, 0x63, 0x65, 0x73, 0x61, 0x6e, 0x74, 0x61, 0x03, + 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, + 0x00, 0x01, 0x00, 0x00, 0x09, 0x52, 0x00, 0x13, 0x03, 0x67, 0x68, 0x73, + 0x0c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x68, 0x6f, 0x73, 0x74, 0x65, + 0x64, 0xc0, 0x17, 0xc0, 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, + 0x2b, 0x00, 0x04, 0x4a, 0x7d, 0x88, 0x79}; + + ASSERT_EQ(mg_parse_dns((const char *) pkt, sizeof(pkt), &msg), 0); + ASSERT_EQ(msg.num_questions, 1); + ASSERT_EQ(msg.num_answers, 2); + + r = &msg.questions[0]; + ASSERT_EQ(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)), + strlen(hostname)); + ASSERT_EQ(strncmp(name, hostname, strlen(hostname)), 0); + + r = &msg.answers[0]; + ASSERT_EQ(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)), + strlen(hostname)); + ASSERT_EQ(strncmp(name, hostname, strlen(hostname)), 0); + + ASSERT_EQ(mg_dns_uncompress_name(&msg, &r->rdata, name, sizeof(name)), + strlen(cname)); + ASSERT_EQ(strncmp(name, cname, strlen(cname)), 0); + + r = &msg.answers[1]; + ASSERT_EQ(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)), + strlen(cname)); + ASSERT_EQ(strncmp(name, cname, strlen(cname)), 0); + ASSERT_EQ(mg_dns_parse_record_data(&msg, r, &tiny, sizeof(tiny)), -1); + ASSERT_EQ(mg_dns_parse_record_data(&msg, r, &ina, sizeof(ina)), 0); + ASSERT_EQ(ina.s_addr, inet_addr("74.125.136.121")); + + /* Test iteration */ + n = 0; + r = NULL; + while ((r = mg_dns_next_record(&msg, MG_DNS_A_RECORD, r))) { + n++; + } + ASSERT_EQ(n, 1); + + n = 0; + r = NULL; + while ((r = mg_dns_next_record(&msg, MG_DNS_CNAME_RECORD, r))) { + n++; + } + ASSERT_EQ(n, 1); + + /* Test unknown record type */ + r = mg_dns_next_record(&msg, MG_DNS_A_RECORD, r); + r->rtype = 0xff; + ASSERT_EQ(mg_dns_parse_record_data(&msg, r, &ina, sizeof(ina)), -1); + + return NULL; +} + +static const char *test_dns_decode_truncated(void) { + struct mg_dns_message msg; + char name[256]; + const char *hostname = "go.cesanta.com"; + const char *cname = "ghs.googlehosted.com"; + struct mg_dns_resource_record *r; + uint16_t tiny; + struct in_addr ina; + int n; + int i; + + const unsigned char src[] = { + 0xa1, 0x00, 0x81, 0x80, 0x00, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x67, 0x6f, 0x07, 0x63, 0x65, 0x73, 0x61, 0x6e, 0x74, 0x61, 0x03, + 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x05, + 0x00, 0x01, 0x00, 0x00, 0x09, 0x52, 0x00, 0x13, 0x03, 0x67, 0x68, 0x73, + 0x0c, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x68, 0x6f, 0x73, 0x74, 0x65, + 0x64, 0xc0, 0x17, 0xc0, 0x2c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x01, + 0x2b, 0x00, 0x04, 0x4a, 0x7d, 0x88, 0x79}; + char *pkt = NULL; + +#define WONDER(expr) \ + if (!(expr)) continue + + for (i = sizeof(src) - 1; i > 0; i--) { + free(pkt); + pkt = (char *) malloc(i); + memcpy(pkt, src, i); + + WONDER(mg_parse_dns((const char *) pkt, i, &msg) == 0); + WONDER(msg.num_questions == 1); + WONDER(msg.num_answers == 2); + + r = &msg.questions[0]; + WONDER(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)) == + strlen(hostname)); + WONDER(strncmp(name, hostname, strlen(hostname)) == 0); + + r = &msg.answers[0]; + WONDER(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)) == + strlen(hostname)); + WONDER(strncmp(name, hostname, strlen(hostname)) == 0); + + WONDER(mg_dns_uncompress_name(&msg, &r->rdata, name, sizeof(name)) == + strlen(cname)); + WONDER(strncmp(name, cname, strlen(cname)) == 0); + + r = &msg.answers[1]; + WONDER(mg_dns_uncompress_name(&msg, &r->name, name, sizeof(name)) == + strlen(cname)); + WONDER(strncmp(name, cname, strlen(cname)) == 0); + WONDER(mg_dns_parse_record_data(&msg, r, &tiny, sizeof(tiny)) == -1); + WONDER(mg_dns_parse_record_data(&msg, r, &ina, sizeof(ina)) == 0); + WONDER(ina.s_addr == inet_addr("74.125.136.121")); + + /* Test iteration */ + n = 0; + r = NULL; + while ((r = mg_dns_next_record(&msg, MG_DNS_A_RECORD, r))) { + n++; + } + WONDER(n == 1); + + n = 0; + r = NULL; + while ((r = mg_dns_next_record(&msg, MG_DNS_CNAME_RECORD, r))) { + n++; + } + WONDER(n == 1); + + /* Test unknown record type */ + r = mg_dns_next_record(&msg, MG_DNS_A_RECORD, r); + WONDER(r != NULL); + printf("GOT %p\n", (void *) r); + r->rtype = 0xff; + WONDER(mg_dns_parse_record_data(&msg, r, &ina, sizeof(ina)) == -1); + + ASSERT("Should have failed" == NULL); + } + free(pkt); + return NULL; +} + +static const char *check_www_cesanta_com_reply(const char *pkt, size_t len) { + char name[256]; + + in_addr_t addr = inet_addr("54.194.65.250"); + struct in_addr ina; + struct mg_dns_message msg; + + memset(&msg, 0, sizeof(msg)); + ASSERT(mg_parse_dns(pkt, len, &msg) != -1); + + memset(name, 0, sizeof(name)); + ASSERT(mg_dns_uncompress_name(&msg, &msg.questions[0].name, name, + sizeof(name)) > 0); + ASSERT_STREQ_NZ(name, "www.cesanta.com"); + memset(name, 0, sizeof(name)); + ASSERT(mg_dns_uncompress_name(&msg, &msg.answers[0].name, name, + sizeof(name)) > 0); + ASSERT_STREQ_NZ(name, "www.cesanta.com"); + ASSERT_EQ(msg.answers[0].rtype, MG_DNS_CNAME_RECORD); + memset(name, 0, sizeof(name)); + ASSERT(mg_dns_parse_record_data(&msg, &msg.answers[0], name, sizeof(name)) != + -1); + ASSERT_STREQ_NZ(name, "cesanta.com"); + memset(name, 0, sizeof(name)); + ASSERT(mg_dns_uncompress_name(&msg, &msg.answers[1].name, name, + sizeof(name)) > 0); + ASSERT_STREQ_NZ(name, "cesanta.com"); + + ASSERT_EQ(msg.answers[1].rtype, MG_DNS_A_RECORD); + ASSERT(mg_dns_parse_record_data(&msg, &msg.answers[1], &ina, sizeof(ina)) != + -1); + ASSERT_EQ(ina.s_addr, addr); + + return NULL; +} + +static const char *test_dns_reply_encode(void) { + const char *err; + struct mg_dns_message msg; + struct mg_dns_resource_record *rr; + char name[256]; + in_addr_t addr = inet_addr("54.194.65.250"); + struct mbuf pkt; + struct mg_connection nc; + + mbuf_init(&pkt, 0); + init_test_connection(&nc); + + /* create a fake query */ + + mg_send_dns_query(&nc, "www.cesanta.com", MG_DNS_A_RECORD); + /* remove message length from tcp buffer */ + mbuf_remove(&nc.send_mbuf, 2); + + ASSERT_EQ(mg_parse_dns(nc.send_mbuf.buf, nc.send_mbuf.len, &msg), 0); + + /* build an answer */ + + msg.num_answers = 2; + mg_dns_insert_header(&pkt, 0, &msg); + mg_dns_copy_questions(&pkt, &msg); + + mg_dns_uncompress_name(&msg, &msg.questions[0].name, name, sizeof(name)); + + rr = &msg.answers[0]; + *rr = msg.questions[0]; + rr->rtype = MG_DNS_CNAME_RECORD; + rr->ttl = 3600; + rr->kind = MG_DNS_ANSWER; + ASSERT(mg_dns_encode_record(&pkt, rr, "www.cesanta.com", 15, + (void *) "cesanta.com", 11) != -1); + + rr = &msg.answers[1]; + *rr = msg.questions[0]; + rr->ttl = 3600; + rr->kind = MG_DNS_ANSWER; + ASSERT(mg_dns_encode_record(&pkt, rr, "cesanta.com", 11, &addr, 4) != -1); + + if ((err = check_www_cesanta_com_reply(pkt.buf, pkt.len)) != NULL) { + return err; + } + + mbuf_free(&pkt); + mbuf_free(&nc.send_mbuf); + return NULL; +} + +#if MG_ENABLE_DNS_SERVER +static void dns_server_eh(struct mg_connection *nc, int ev, void *ev_data) { + struct mg_dns_message *msg; + struct mg_dns_resource_record *rr; + struct mg_dns_reply reply; + char name[512]; + int i; + + name[511] = 0; + switch (ev) { + case MG_DNS_MESSAGE: + msg = (struct mg_dns_message *) ev_data; + reply = mg_dns_create_reply(&nc->send_mbuf, msg); + + for (i = 0; i < msg->num_questions; i++) { + rr = &msg->questions[i]; + if (rr->rtype == MG_DNS_A_RECORD) { + mg_dns_uncompress_name(msg, &rr->name, name, sizeof(name) - 1); + + if (strcmp(name, "cesanta.com") == 0) { + mg_dns_reply_record(&reply, rr, NULL, rr->rtype, 3600, + nc->user_data, 4); + } else if (strcmp(name, "www.cesanta.com") == 0) { + mg_dns_reply_record(&reply, rr, NULL, MG_DNS_CNAME_RECORD, 3600, + "cesanta.com", strlen("cesanta.com")); + + mg_dns_reply_record(&reply, rr, "cesanta.com", rr->rtype, 3600, + nc->user_data, 4); + } + } + } + + /* + * We don't set the error flag even if there were no answers + * maching the MG_DNS_A_RECORD query type. + * This indicates that we have (syntetic) answers for MG_DNS_A_RECORD. + * See http://goo.gl/QWvufr for a distinction between NXDOMAIN and NODATA. + */ + + mg_dns_send_reply(nc, &reply); + nc->flags |= MG_F_SEND_AND_CLOSE; + break; + } +} + +static int check_record_name(struct mg_dns_message *msg, struct mg_str *n, + const char *want) { + char name[512]; + if (mg_dns_uncompress_name(msg, n, name, sizeof(name)) == 0) { + return 0; + } + return strncmp(name, want, sizeof(name)) == 0; +} + +static const char *test_dns_server(void) { + const char *err; + struct mg_connection nc; + struct mg_dns_message msg; + in_addr_t addr = inet_addr("54.194.65.250"); + int ilen; + + init_test_connection(&nc); + + nc.handler = dns_server_eh; + nc.user_data = &addr; + mg_set_protocol_dns(&nc); + + mg_send_dns_query(&nc, "www.cesanta.com", MG_DNS_A_RECORD); + + nc.recv_mbuf = nc.send_mbuf; + mbuf_init(&nc.send_mbuf, 0); + + ilen = nc.recv_mbuf.len; + nc.proto_handler(&nc, MG_EV_RECV, &ilen); + mbuf_free(&nc.recv_mbuf); + /* remove message length from tcp buffer before manually checking */ + mbuf_remove(&nc.send_mbuf, 2); + + if ((err = check_www_cesanta_com_reply(nc.send_mbuf.buf, nc.send_mbuf.len)) != + NULL) { + mbuf_free(&nc.send_mbuf); + return err; + } + + mbuf_free(&nc.send_mbuf); + + /* test mg_dns_reply_record */ + mg_send_dns_query(&nc, "cesanta.com", MG_DNS_A_RECORD); + + nc.recv_mbuf = nc.send_mbuf; + mbuf_init(&nc.send_mbuf, 0); + + ilen = nc.recv_mbuf.len; + nc.proto_handler(&nc, MG_EV_RECV, &ilen); + /* remove message length from tcp buffer before manually checking */ + mbuf_remove(&nc.send_mbuf, 2); + + ASSERT(mg_parse_dns(nc.send_mbuf.buf, nc.send_mbuf.len, &msg) != -1); + ASSERT_EQ(msg.num_answers, 1); + ASSERT_EQ(msg.answers[0].rtype, MG_DNS_A_RECORD); + ASSERT(check_record_name(&msg, &msg.answers[0].name, "cesanta.com")); + + mbuf_free(&nc.send_mbuf); + mbuf_free(&nc.recv_mbuf); + + /* check malformed request error */ + memset(&msg, 0, sizeof(msg)); + ilen = 0; + nc.proto_handler(&nc, MG_EV_RECV, &ilen); + /* remove message length from tcp buffer before manually checking */ + mbuf_remove(&nc.send_mbuf, 2); + + ASSERT(mg_parse_dns(nc.send_mbuf.buf, nc.send_mbuf.len, &msg) != -1); + ASSERT(msg.flags & 1); + ASSERT_EQ(msg.num_questions, 0); + ASSERT_EQ(msg.num_answers, 0); + + mbuf_free(&nc.recv_mbuf); + mbuf_free(&nc.send_mbuf); + return NULL; +} +#endif /* MG_ENABLE_DNS_SERVER */ + +static void dns_resolve_cb(struct mg_dns_message *msg, void *data, + enum mg_resolve_err e) { + struct mg_dns_resource_record *rr; + char cname[256]; + struct in_addr got_addr; + in_addr_t want_addr = inet_addr("131.107.255.255"); + + (void) e; + rr = mg_dns_next_record(msg, MG_DNS_A_RECORD, NULL); + if (rr != NULL) { + mg_dns_parse_record_data(msg, rr, &got_addr, sizeof(got_addr)); + } + + rr = mg_dns_next_record(msg, MG_DNS_CNAME_RECORD, NULL); + if (rr != NULL) { + mg_dns_parse_record_data(msg, rr, cname, sizeof(cname)); + } + + /* + * We saw cases when A query returns only A record, or A and CNAME records. + * Expect A answer, and optionally CNAME answer. + */ + if (want_addr == got_addr.s_addr || strcmp(cname, "dns.msftncsi.com") == 0) { + *(int *) data = 1; /* Success */ + } else { + *(int *) data = 2; /* Error */ + } +} + +static const char *test_dns_resolve(void) { + struct mg_mgr mgr; + struct mg_mgr_init_opts opts; + + int data = 0; + + mg_mgr_init(&mgr, NULL); + + /* Microsoft promises dns.msftncsi.com is always 131.107.255.255 */ + mg_resolve_async(&mgr, "dns.msftncsi.com", MG_DNS_A_RECORD, dns_resolve_cb, + &data); + + poll_until(&mgr, 1, c_int_ne, &data, (void *) 0); + ASSERT_EQ(data, 1); + + mg_mgr_free(&mgr); + + data = 0; + memset(&opts, 0, sizeof(opts)); + opts.nameserver = "8.8.4.4"; + mg_mgr_init_opt(&mgr, NULL, opts); + + /* Microsoft promises dns.msftncsi.com is always 131.107.255.255 */ + mg_resolve_async(&mgr, "dns.msftncsi.com", MG_DNS_A_RECORD, dns_resolve_cb, + &data); + + poll_until(&mgr, 1, c_int_ne, &data, (void *) 0); + ASSERT_EQ(data, 1); + + mg_mgr_free(&mgr); + + data = 0; + mg_mgr_init(&mgr, NULL); + mg_set_nameserver(&mgr, "8.8.4.4"); + + /* Microsoft promises dns.msftncsi.com is always 131.107.255.255 */ + mg_resolve_async(&mgr, "dns.msftncsi.com", MG_DNS_A_RECORD, dns_resolve_cb, + &data); + + poll_until(&mgr, 1, c_int_ne, &data, (void *) 0); + ASSERT_EQ(data, 1); + + mg_mgr_free(&mgr); + + return NULL; +} + +static void dns_resolve_timeout_cb(struct mg_dns_message *msg, void *data, + enum mg_resolve_err e) { + (void) e; + if (msg == NULL) { + *(int *) data = 1; + } +} + +extern char mg_dns_server[256]; + +static const char *test_dns_resolve_timeout(void) { + struct mg_mgr mgr; + struct mg_resolve_async_opts opts; + int data = 0; + mg_mgr_init(&mgr, NULL); + memset(&opts, 0, sizeof(opts)); + + opts.nameserver = "7.7.7.7"; + opts.timeout = -1; /* 0 would be the default timeout */ + opts.max_retries = 1; + mg_resolve_async_opt(&mgr, "www.cesanta.com", MG_DNS_A_RECORD, + dns_resolve_timeout_cb, &data, opts); + + poll_until(&mgr, 5, c_int_eq, &data, (void *) 1); + ASSERT_EQ(data, 1); + + mg_mgr_free(&mgr); + return NULL; +} + +#ifndef NO_RESOLVE_HOSTS_TEST +static const char *test_dns_resolve_hosts(void) { + union socket_address sa; + in_addr_t want_addr = inet_addr("127.0.0.1"); + + memset(&sa, 0, sizeof(sa)); + ASSERT_EQ(mg_resolve_from_hosts_file("localhost", &sa), 0); + ASSERT_EQ(sa.sin.sin_addr.s_addr, want_addr); + ASSERT_EQ(mg_resolve_from_hosts_file("does_not,exist!in_host*file", &sa), -1); + + return NULL; +} +#endif + +static void ehb_srv(struct mg_connection *nc, int ev, void *p) { + struct mbuf *io = &nc->recv_mbuf; + (void) io; + (void) p; + + if (ev == MG_EV_RECV) { + if (*(int *) p == 1) (*(int *) nc->mgr->user_data)++; + mbuf_remove(io, *(int *) p); + } +} + +static void ehb_srv2(struct mg_connection *c, int ev, void *p) { + if (ev == MG_EV_CLOSE) { + if (c->recv_mbuf.size == 1) (*(int *) c->mgr->user_data) = 1; + (void) p; + } +} + +static const char *test_buffer_limit(void) { + struct mg_mgr mgr; + struct mg_connection *clnt, *srv; + const char *address = "tcp://127.0.0.1:7878"; + int res = 0; + + mg_mgr_init(&mgr, &res); + ASSERT((srv = mg_bind(&mgr, address, ehb_srv)) != NULL); + srv->recv_mbuf_limit = 1; + ASSERT((clnt = mg_connect(&mgr, address, NULL)) != NULL); + mg_printf(clnt, "abcd"); + + poll_until(&mgr, 1, c_int_eq, &res, (void *) 4); + + /* expect four single byte read events */ + ASSERT_EQ(res, 4); + + /* + * Make sure we're not closing full non-drained connections without any + * protocol handler + */ + res = 0; + srv->handler = ehb_srv2; + ASSERT((clnt = mg_connect(&mgr, address, NULL)) != NULL); + mg_printf(clnt, "abcd"); + poll_until(&mgr, 1, c_int_eq, &res, (void *) 1); + ASSERT_EQ(res, 0); + + /* + * Make sure we're closing full non-drained connections with mqtt protocol + * handler + */ + res = 0; + srv->handler = ehb_srv2; + mg_set_protocol_mqtt(srv); + ASSERT((clnt = mg_connect(&mgr, address, NULL)) != NULL); + mg_printf(clnt, "abcd"); + poll_until(&mgr, 1, c_int_eq, &res, (void *) 1); + ASSERT_EQ(res, 1); + + mg_mgr_free(&mgr); + return NULL; +} + +static const char *test_http_parse_header(void) { + static struct mg_str h = MG_MK_STR( + "xx=1 kl yy, ert=234 kl=123, " + "uri=\"/?naii=x,y\";ii=\"12\\\"34\" zz='aa bb',tt=2,gf=\"xx d=1234"); + char buf[20]; + + ASSERT_EQ(mg_http_parse_header(&h, "ert", buf, sizeof(buf)), 3); + ASSERT_STREQ(buf, "234"); + ASSERT_EQ(mg_http_parse_header(&h, "ert", buf, 2), 0); + ASSERT_EQ(mg_http_parse_header(&h, "ert", buf, 3), 0); + ASSERT_EQ(mg_http_parse_header(&h, "ert", buf, 4), 3); + ASSERT_EQ(mg_http_parse_header(&h, "gf", buf, sizeof(buf)), 0); + ASSERT_EQ(mg_http_parse_header(&h, "zz", buf, sizeof(buf)), 5); + ASSERT_STREQ(buf, "aa bb"); + ASSERT_EQ(mg_http_parse_header(&h, "d", buf, sizeof(buf)), 4); + ASSERT_STREQ(buf, "1234"); + buf[0] = 'x'; + ASSERT_EQ(mg_http_parse_header(&h, "MMM", buf, sizeof(buf)), 0); + ASSERT_EQ(buf[0], '\0'); + ASSERT_EQ(mg_http_parse_header(&h, "kl", buf, sizeof(buf)), 3); + ASSERT_STREQ(buf, "123"); + ASSERT_EQ(mg_http_parse_header(&h, "xx", buf, sizeof(buf)), 1); + ASSERT_STREQ(buf, "1"); + ASSERT_EQ(mg_http_parse_header(&h, "ii", buf, sizeof(buf)), 5); + ASSERT_STREQ(buf, "12\"34"); + ASSERT_EQ(mg_http_parse_header(&h, "tt", buf, sizeof(buf)), 1); + ASSERT_STREQ(buf, "2"); + ASSERT(mg_http_parse_header(&h, "uri", buf, sizeof(buf)) > 0); + + return NULL; +} + +#if MG_ENABLE_COAP +struct results { + int server; + int client; +}; + +static void coap_handler_1(struct mg_connection *nc, int ev, void *p) { + switch (ev) { + case MG_EV_CONNECT: { + struct mg_coap_message cm; + memset(&cm, 0, sizeof(cm)); + cm.msg_id = 1; + cm.msg_type = MG_COAP_MSG_CON; + mg_coap_send_message(nc, &cm); + break; + } + case MG_EV_COAP_ACK: { + struct mg_coap_message *cm = (struct mg_coap_message *) p; + ((struct results *) (nc->user_data))->client = cm->msg_id + cm->msg_type; + break; + } + case MG_EV_COAP_CON: { + struct mg_coap_message *cm = (struct mg_coap_message *) p; + ((struct results *) (nc->user_data))->server = cm->msg_id + cm->msg_type; + mg_coap_send_ack(nc, cm->msg_id); + break; + } + } +} + +static const char *test_coap(void) { + struct mbuf packet_in, packet_out; + struct mg_coap_message cm; + uint32_t res; + + unsigned char coap_packet_1[] = {0x42, 0x01, 0xe9, 0x1b, 0x07, 0x90, 0xb8, + 0x73, 0x65, 0x70, 0x61, 0x72, 0x61, 0x74, + 0x65, 0x10, 0xd1, 0x23, 0x11}; + unsigned char coap_packet_2[] = {0x60, 0x00, 0xe9, 0x1b}; + unsigned char coap_packet_3[] = { + 0x42, 0x45, 0x57, 0x0f, 0x07, 0x90, 0xff, 0x54, 0x68, 0x69, 0x73, 0x20, + 0x6d, 0x65, 0x73, 0x73, 0x61, 0x67, 0x65, 0x20, 0x77, 0x61, 0x73, 0x20, + 0x73, 0x65, 0x6e, 0x74, 0x20, 0x62, 0x79, 0x20, 0x61, 0x20, 0x73, 0x65, + 0x70, 0x61, 0x72, 0x61, 0x74, 0x65, 0x20, 0x72, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x2e, 0x0a, 0x59, 0x6f, 0x75, 0x72, 0x20, 0x63, 0x6c, + 0x69, 0x65, 0x6e, 0x74, 0x20, 0x77, 0x69, 0x6c, 0x6c, 0x20, 0x6e, 0x65, + 0x65, 0x64, 0x20, 0x74, 0x6f, 0x20, 0x61, 0x63, 0x6b, 0x6e, 0x6f, 0x77, + 0x6c, 0x65, 0x64, 0x67, 0x65, 0x20, 0x69, 0x74, 0x2c, 0x20, 0x6f, 0x74, + 0x68, 0x65, 0x72, 0x77, 0x69, 0x73, 0x65, 0x20, 0x69, 0x74, 0x20, 0x77, + 0x69, 0x6c, 0x6c, 0x20, 0x62, 0x65, 0x20, 0x72, 0x65, 0x74, 0x72, 0x61, + 0x6e, 0x73, 0x6d, 0x69, 0x74, 0x74, 0x65, 0x64, 0x2e}; + unsigned char coap_packet_4[] = {0x60, 0x00, 0x57, 0x0f}; + unsigned char coap_packet_5[] = { + 0x40, 0x03, 0x95, 0x22, 0xb7, 0x73, 0x74, 0x6f, 0x72, 0x61, + 0x67, 0x65, 0x0a, 0x6d, 0x79, 0x72, 0x65, 0x73, 0x6f, 0x75, + 0x72, 0x63, 0x65, 0xff, 0x6d, 0x79, 0x64, 0x61, 0x74, 0x61}; + unsigned char coap_packet_6[] = {0xFF, 0x00, 0xFF, 0x00}; + unsigned char coap_packet_7[] = { + 0x40, 0x03, 0x95, 0x22, 0xb7, 0x73, 0x74, 0x6f, 0x72, 0x61, + 0x67, 0x65, 0x0a, 0x6d, 0x79, 0x72, 0x65, 0x73, 0x6f, 0x75, + 0x72, 0x63, 0x65, 0xf1, 0x6d, 0x79, 0x64, 0x61, 0x74, 0x61}; + + mbuf_init(&packet_in, 0); + /* empty buf */ + res = mg_coap_parse(&packet_in, &cm); + ASSERT((res & MG_COAP_NOT_ENOUGH_DATA) != 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_in); + + mbuf_init(&packet_out, 0); + /* ACK, MID: 59675, Empty Message */ + packet_in.buf = (char *) coap_packet_2; + packet_in.len = sizeof(coap_packet_2); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.code_class, 0); + ASSERT_EQ(cm.code_detail, 0); + ASSERT_EQ(cm.msg_id, 59675); + ASSERT_EQ(cm.msg_type, MG_COAP_MSG_ACK); + ASSERT(cm.options == NULL); + ASSERT_EQ(cm.payload.len, 0); + ASSERT(cm.payload.p == NULL); + ASSERT_EQ(cm.token.len, 0); + ASSERT(cm.token.p == NULL); + res = mg_coap_compose(&cm, &packet_out); + ASSERT_EQ(res, 0); + ASSERT_EQ(packet_out.len, sizeof(coap_packet_2)); + ASSERT_EQ(memcmp(packet_out.buf, coap_packet_2, packet_out.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + + /* ACK, MID: 22287, Empty Message */ + packet_in.buf = (char *) coap_packet_4; + packet_in.len = sizeof(coap_packet_4); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.code_class, 0); + ASSERT_EQ(cm.code_detail, 0); + ASSERT_EQ(cm.msg_id, 22287); + ASSERT_EQ(cm.msg_type, MG_COAP_MSG_ACK); + ASSERT(cm.options == NULL); + ASSERT_EQ(cm.payload.len, 0); + ASSERT(cm.payload.p == NULL); + ASSERT_EQ(cm.token.len, 0); + ASSERT(cm.token.p == NULL); + res = mg_coap_compose(&cm, &packet_out); + ASSERT_EQ(res, 0); + ASSERT_EQ(packet_out.len, sizeof(coap_packet_4)); + ASSERT_EQ(memcmp(packet_out.buf, coap_packet_4, packet_out.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + + /* CON, MID: 59675 ... */ + packet_in.buf = (char *) coap_packet_1; + packet_in.len = sizeof(coap_packet_1); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.code_class, 0); + ASSERT_EQ(cm.code_detail, 1); + ASSERT_EQ(cm.msg_id, 59675); + ASSERT_EQ(cm.msg_type, MG_COAP_MSG_CON); + ASSERT(cm.options != 0); + ASSERT_EQ(cm.options->number, 11); + ASSERT_EQ(cm.options->value.len, 8); + ASSERT_STREQ_NZ(cm.options->value.p, "separate"); + ASSERT(cm.options->next != 0); + ASSERT_EQ(cm.options->next->number, 12); + ASSERT_EQ(cm.options->next->value.len, 0); + ASSERT(cm.options->next->next != 0); + ASSERT_EQ(cm.options->next->next->number, 60); + ASSERT_EQ(cm.options->next->next->value.len, 1); + ASSERT_EQ(*cm.options->next->next->value.p, 0x11); + ASSERT(cm.options->next->next->next == NULL); + ASSERT_EQ(cm.payload.len, 0); + ASSERT(cm.payload.p == NULL); + ASSERT_EQ(cm.token.len, 2); + ASSERT_EQ(*cm.token.p, 0x07); + ASSERT_EQ((unsigned char) *(cm.token.p + 1), 0x90); + res = mg_coap_compose(&cm, &packet_out); + ASSERT_EQ(res, 0); + ASSERT_EQ(packet_out.len, sizeof(coap_packet_1)); + ASSERT_EQ(memcmp(packet_out.buf, coap_packet_1, packet_out.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + + /* CON, MID: 22287 ... */ + packet_in.buf = (char *) coap_packet_3; + packet_in.len = sizeof(coap_packet_3); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.code_class, 2); + ASSERT_EQ(cm.code_detail, 5); + ASSERT_EQ(cm.msg_id, 22287); + ASSERT_EQ(cm.msg_type, MG_COAP_MSG_CON); + ASSERT(cm.options == NULL); + ASSERT_EQ(cm.token.len, 2); + ASSERT_EQ(*cm.token.p, 0x07); + ASSERT_EQ((unsigned char) *(cm.token.p + 1), 0x90); + ASSERT_EQ(cm.payload.len, 122); + ASSERT(strncmp(cm.payload.p, + "This message was sent by a separate response.\n" + "Your client will need to acknowledge it," + " otherwise it will be retransmitted.", + 122) == 0); + res = mg_coap_compose(&cm, &packet_out); + ASSERT_EQ(res, 0); + ASSERT_EQ(packet_out.len, sizeof(coap_packet_3)); + ASSERT_EQ(memcmp(packet_out.buf, coap_packet_3, packet_out.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + + packet_in.buf = (char *) coap_packet_5; + packet_in.len = sizeof(coap_packet_5); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.code_class, 0); + ASSERT_EQ(cm.code_detail, 3); + ASSERT_EQ(cm.msg_id, 38178); + ASSERT_EQ(cm.msg_type, MG_COAP_MSG_CON); + ASSERT(cm.options != 0); + ASSERT_EQ(cm.options->number, 11); + ASSERT_EQ(cm.options->value.len, 7); + ASSERT_STREQ_NZ(cm.options->value.p, "storage"); + ASSERT(cm.options->next != 0); + ASSERT_EQ(cm.options->next->number, 11); + ASSERT_EQ(cm.options->next->value.len, 10); + ASSERT_STREQ_NZ(cm.options->next->value.p, "myresource"); + ASSERT(cm.options->next->next == NULL); + ASSERT_EQ(cm.token.len, 0); + ASSERT_EQ(cm.payload.len, 6); + ASSERT_STREQ_NZ(cm.payload.p, "mydata"); + res = mg_coap_compose(&cm, &packet_out); + ASSERT_EQ(res, 0); + ASSERT_EQ(packet_out.len, sizeof(coap_packet_5)); + ASSERT_EQ(memcmp(packet_out.buf, coap_packet_5, packet_out.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + + packet_in.buf = (char *) coap_packet_6; + packet_in.len = sizeof(coap_packet_6); + res = mg_coap_parse(&packet_in, &cm); + ASSERT((res & MG_COAP_ERROR) != 0); + mg_coap_free_options(&cm); + + packet_in.buf = (char *) coap_packet_7; + packet_in.len = sizeof(coap_packet_7); + res = mg_coap_parse(&packet_in, &cm); + ASSERT((res & MG_COAP_ERROR) != 0); + mg_coap_free_options(&cm); + + { + unsigned char coap_packet_2_broken[] = {0x6F, 0x00, 0xe9, 0x1b}; + packet_in.buf = (char *) coap_packet_2_broken; + packet_in.len = sizeof(coap_packet_2_broken); + res = mg_coap_parse(&packet_in, &cm); + ASSERT((res & MG_COAP_FORMAT_ERROR) != 0); + } + + { + unsigned char coap_packet_2_broken[] = {0x65, 0x00, 0xe9, 0x1b}; + packet_in.buf = (char *) coap_packet_2_broken; + packet_in.len = sizeof(coap_packet_2_broken); + res = mg_coap_parse(&packet_in, &cm); + ASSERT((res & MG_COAP_NOT_ENOUGH_DATA) != 0); + } + + memset(&cm, 0, sizeof(cm)); + mg_coap_add_option(&cm, 10, 0, 0); + ASSERT_EQ(cm.options->number, 10); + ASSERT(cm.options->next == NULL); + mg_coap_add_option(&cm, 5, 0, 0); + ASSERT_EQ(cm.options->number, 5); + ASSERT_EQ(cm.options->next->number, 10); + ASSERT(cm.options->next->next == NULL); + mg_coap_add_option(&cm, 7, 0, 0); + ASSERT_EQ(cm.options->number, 5); + ASSERT_EQ(cm.options->next->number, 7); + ASSERT_EQ(cm.options->next->next->number, 10); + ASSERT(cm.options->next->next->next == NULL); + mg_coap_add_option(&cm, 1, 0, 0); + ASSERT_EQ(cm.options->number, 1); + ASSERT_EQ(cm.options->next->number, 5); + ASSERT_EQ(cm.options->next->next->number, 7); + ASSERT_EQ(cm.options->next->next->next->number, 10); + ASSERT(cm.options->next->next->next->next == NULL); + mg_coap_free_options(&cm); + + { + unsigned char value16[] = {0xCC, 0xDD}; + packet_in.buf = (char *) coap_packet_4; + packet_in.len = sizeof(coap_packet_4); + res = mg_coap_parse(&packet_in, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + mg_coap_add_option(&cm, 0xAABB, (char *) value16, sizeof(value16)); + res = mg_coap_compose(&cm, &packet_out); + mg_coap_free_options(&cm); + ASSERT_EQ(res, 0); + res = mg_coap_parse(&packet_out, &cm); + ASSERT_EQ((res & MG_COAP_ERROR), 0); + ASSERT_EQ(cm.options->number, 0xAABB); + ASSERT_EQ(cm.options->value.len, 2); + ASSERT_EQ(memcmp(cm.options->value.p, value16, cm.options->value.len), 0); + mg_coap_free_options(&cm); + mbuf_free(&packet_out); + } + + memset(&cm, 0, sizeof(cm)); + cm.msg_id = 1; + cm.msg_type = MG_COAP_MSG_MAX + 1; + res = mg_coap_compose(&cm, &packet_out); + ASSERT((res & MG_COAP_ERROR) != 0 && (res & MG_COAP_MSG_TYPE_FIELD) != 0); + + cm.msg_type = MG_COAP_MSG_ACK; + cm.token.len = 10000; + res = mg_coap_compose(&cm, &packet_out); + ASSERT((res & MG_COAP_ERROR) != 0 && (res & MG_COAP_TOKEN_FIELD) != 0); + + cm.token.len = 0; + cm.code_class = 0xFF; + res = mg_coap_compose(&cm, &packet_out); + ASSERT((res & MG_COAP_ERROR) != 0 && (res & MG_COAP_CODE_CLASS_FIELD) != 0); + + cm.code_class = 0; + cm.code_detail = 0xFF; + res = mg_coap_compose(&cm, &packet_out); + ASSERT((res & MG_COAP_ERROR) != 0 && (res & MG_COAP_CODE_DETAIL_FIELD) != 0); + + cm.code_detail = 0; + mg_coap_add_option(&cm, 0xFFFFFFF, 0, 0); + res = mg_coap_compose(&cm, &packet_out); + ASSERT((res & MG_COAP_ERROR) != 0 && (res & MG_COAP_OPTIOMG_FIELD) != 0); + mg_coap_free_options(&cm); + + { + struct mg_mgr mgr; + struct mg_connection *nc; + const char *address = "tcp://127.0.0.1:8686"; + + mg_mgr_init(&mgr, 0); + + nc = mg_bind(&mgr, address, coap_handler_1); + ASSERT(nc != NULL); + ASSERT_EQ(mg_set_protocol_coap(nc), -1); + + mg_mgr_free(&mgr); + } + + { + struct results res; + struct mg_mgr mgr; + struct mg_connection *nc1, *nc2; + const char *address = "udp://127.0.0.1:5683"; + + mg_mgr_init(&mgr, 0); + + nc1 = mg_bind(&mgr, address, coap_handler_1); + mg_set_protocol_coap(nc1); + nc1->user_data = &res; + + nc2 = mg_connect(&mgr, address, coap_handler_1); + mg_set_protocol_coap(nc2); + nc2->user_data = &res; + + poll_until(&mgr, 5, c_int_eq, &res.client, (void *) 3); + + mg_mgr_free(&mgr); + + ASSERT_EQ(res.server, 1); + ASSERT_EQ(res.client, 3); + } + + return NULL; +} +#endif + +static const char *test_strcmp(void) { + struct mg_str s1; + + s1.p = "aa"; + s1.len = strlen(s1.p); + ASSERT_EQ(mg_vcasecmp(&s1, "aa"), 0); + ASSERT_EQ(mg_vcmp(&s1, "aa"), 0); + ASSERT(mg_vcasecmp(&s1, "ab") < 0); + ASSERT(mg_vcmp(&s1, "ab") < 0); + ASSERT(mg_vcasecmp(&s1, "abb") < 0); + ASSERT(mg_vcmp(&s1, "abb") < 0); + ASSERT(mg_vcasecmp(&s1, "b") < 0); + ASSERT(mg_vcmp(&s1, "b") < 0); + return NULL; +} + +#if MG_ENABLE_SNTP +static const char *test_sntp(void) { + const char sntp_good[] = + "\x24\x02\x00\xeb\x00\x00\x00\x1e\x00\x00\x07\xb6\x3e" + "\xc9\xd6\xa2\xdb\xde\xea\x30\x91\x86\xb7\x10\xdb\xde" + "\xed\x98\x00\x00\x00\xde\xdb\xde\xed\x99\x0a\xe2\xc7" + "\x96\xdb\xde\xed\x99\x0a\xe4\x6b\xda"; + const char bad_good[] = + "\x55\x02\x00\xeb\x00\x00\x00\x1e\x00\x00\x07\xb6\x3e" + "\xc9\xd6\xa2\xdb\xde\xea\x30\x91\x86\xb7\x10\xdb\xde" + "\xed\x98\x00\x00\x00\xde\xdb\xde\xed\x99\x0a\xe2\xc7" + "\x96\xdb\xde\xed\x99\x0a\xe4\x6b\xda"; + + int res; + struct mg_sntp_message msg; + struct tm *tm; + time_t t; + memset(&msg, 0, sizeof(0)); + + res = mg_sntp_parse_reply(sntp_good, sizeof(sntp_good) - 1, &msg); + ASSERT_EQ(res, 0); + t = (time_t) msg.time; + tm = gmtime(&t); + ASSERT_EQ(tm->tm_year, 116); + ASSERT_EQ(tm->tm_mon, 10); + ASSERT_EQ(tm->tm_mday, 22); + ASSERT_EQ(tm->tm_hour, 16); + ASSERT_EQ(tm->tm_min, 15); + ASSERT_EQ(tm->tm_sec, 21); + res = mg_sntp_parse_reply(bad_good, sizeof(bad_good) - 1, &msg); + ASSERT_EQ(res, -1); + + return NULL; +} +#endif + +#if MG_ENABLE_SOCKS +static const char *test_socks(void) { + struct mg_mgr mgr; + struct mg_connection *c; + struct mg_connect_opts opts; + const char *this_binary; + const char *web_addr = "127.0.0.1:7257"; + const char *proxy_addr = "127.0.0.1:7258"; + char status[100] = ""; + + mg_mgr_init(&mgr, NULL); +#if 0 + mgr.hexdump_file = "-"; +#endif + +#if 1 + cs_log_set_level(LL_INFO); +#endif + + /* Start web server */ + ASSERT((c = mg_bind(&mgr, web_addr, cb1)) != NULL); + mg_set_protocol_http_websocket(c); + + /* Start socks proxy */ + ASSERT((c = mg_bind(&mgr, proxy_addr, ev_handler_empty)) != NULL); + mg_set_protocol_socks(c); + + /* Create HTTP client that uses socks proxy */ + memset(&opts, 0, sizeof(opts)); + opts.iface = mg_socks_mk_iface(&mgr, proxy_addr); + + ASSERT((c = mg_connect_opt(&mgr, web_addr, cb7, opts)) != NULL); + mg_set_protocol_http_websocket(c); + c->user_data = status; + /* Wine and GDB set argv0 to full path: strip the dir component */ + if ((this_binary = strrchr(s_argv_0, '\\')) != NULL) { + this_binary++; + } else if ((this_binary = strrchr(s_argv_0, '/')) != NULL) { + this_binary++; + } else { + this_binary = s_argv_0; + } + mg_printf(c, "GET /%s HTTP/1.0\n\n", this_binary); + + /* Run event loop. Use more cycles to let file download complete. */ + poll_until(&mgr, 5, c_str_ne, status, (void *) ""); + mg_mgr_free(&mgr); + ASSERT_STREQ(status, "success"); + + return NULL; +} +#endif + +static const char *run_tests(const char *filter, double *total_elapsed) { + RUN_TEST(test_mbuf); + RUN_TEST(test_parse_uri); + RUN_TEST(test_assemble_uri); + RUN_TEST(test_parse_address); + RUN_TEST(test_mg_normalize_uri_path); + RUN_TEST(test_mg_uri_to_local_path); + RUN_TEST(test_mg_url_encode); + RUN_TEST(test_check_ip_acl); + RUN_TEST(test_connect_opts); + RUN_TEST(test_connect_opts_error_string); + RUN_TEST(test_to64); + RUN_TEST(test_alloc_vprintf); + RUN_TEST(test_socketpair); + RUN_TEST(test_timer); +#ifdef __linux__ + RUN_TEST(test_simple); +#endif +#if MG_ENABLE_THREADS + RUN_TEST(test_thread); +#endif + RUN_TEST(test_mgr); + RUN_TEST(test_parse_http_message); + RUN_TEST(test_get_http_var); + RUN_TEST(test_http_serve_file); + RUN_TEST(test_http_serve_file_streaming); + RUN_TEST(test_http); + RUN_TEST(test_http_send_redirect); + RUN_TEST(test_http_digest_auth); + RUN_TEST(test_http_errors); + RUN_TEST(test_http_index); + RUN_TEST(test_http_parse_header); + RUN_TEST(test_ssi); +#ifndef NO_CGI_TEST + RUN_TEST(test_cgi); +#endif + RUN_TEST(test_http_rewrites); + RUN_TEST(test_http_dav); + RUN_TEST(test_http_range); + RUN_TEST(test_http_multipart); +#if MG_ENABLE_HTTP_STREAMING_MULTIPART + RUN_TEST(test_http_multipart2); +#endif + RUN_TEST(test_parse_date_string); + RUN_TEST(test_websocket); + RUN_TEST(test_websocket_endpoint); + RUN_TEST(test_connect_ws); + RUN_TEST(test_websocket_big); + RUN_TEST(test_http_chunk); + RUN_TEST(test_http_chunk2); + RUN_TEST(test_http_not_modified); + RUN_TEST(test_http_extra_headers); + RUN_TEST(test_http_endpoints); + RUN_TEST(test_mqtt_handshake); + RUN_TEST(test_mqtt_publish); + RUN_TEST(test_mqtt_subscribe); + RUN_TEST(test_mqtt_unsubscribe); + RUN_TEST(test_mqtt_connack); + RUN_TEST(test_mqtt_suback); + RUN_TEST(test_mqtt_simple_acks); + RUN_TEST(test_mqtt_nullary); + RUN_TEST(test_mqtt_parse_mqtt); + RUN_TEST(test_mqtt_parse_mqtt_qos1); + RUN_TEST(test_mqtt_match_topic_expression); + RUN_TEST(test_mqtt_client_keep_alive); +#if MG_ENABLE_MQTT_BROKER + RUN_TEST(test_mqtt_broker); +#endif + RUN_TEST(test_dns_encode); + RUN_TEST(test_dns_uncompress); + RUN_TEST(test_dns_decode); + RUN_TEST(test_dns_decode_truncated); + RUN_TEST(test_dns_reply_encode); +#if MG_ENABLE_DNS_SERVER + RUN_TEST(test_dns_server); +#endif + RUN_TEST(test_dns_resolve); + RUN_TEST(test_dns_resolve_timeout); +#ifndef NO_RESOLVE_HOSTS_TEST + RUN_TEST(test_dns_resolve_hosts); +#endif + RUN_TEST(test_buffer_limit); + RUN_TEST(test_connection_errors); + RUN_TEST(test_connect_fail); +#ifndef NO_DNS_TEST + RUN_TEST(test_resolve); +#endif + RUN_TEST(test_base64); + RUN_TEST(test_sock_addr_to_str); + RUN_TEST(test_hexdump); + RUN_TEST(test_hexdump_file); + RUN_TEST(test_basic_auth_helpers); + RUN_TEST(test_http_auth); +#ifndef _WIN32 + RUN_TEST(test_tunnel); +#endif +#if MG_ENABLE_SSL + RUN_TEST(test_ssl); +#ifdef OPENSSL_VERSION_NUMBER + RUN_TEST(test_modern_crypto); +#endif +#endif + RUN_TEST(test_udp); +#if MG_ENABLE_COAP + RUN_TEST(test_coap); +#endif +#if MG_ENABLE_SNTP + RUN_TEST(test_sntp); +#endif + RUN_TEST(test_strcmp); +#if MG_ENABLE_SOCKS + RUN_TEST(test_socks); +#endif + return NULL; +} + +#if defined(_MSC_VER) && _MSC_VER >= 1900 +int __cdecl CrtDbgHook(int nReportType, char *szMsg, int *pnRet) { + (void) nReportType; + (void) szMsg; + (void) pnRet; + + fprintf(stderr, "CRT debug hook: type: %d, msg: %s\n", nReportType, szMsg); + /* Return true - Abort,Retry,Ignore dialog will *not* be displayed */ + return 1; +} +#endif + +void setup() { + test_iface = mg_if_create_iface(mg_ifaces[MG_MAIN_IFACE], NULL); +} + +void teardown() { + free(test_iface); +} + +int __cdecl main(int argc, char *argv[]) { + const char *fail_msg; + const char *filter = argc > 1 ? argv[1] : ""; + double total_elapsed = 0.0; + + setvbuf(stdout, NULL, _IONBF, 0); + setvbuf(stderr, NULL, _IONBF, 0); + +#if defined(_MSC_VER) && _MSC_VER >= 1900 + /* NOTE: not available on wine/vc6 */ + _CrtSetReportHook2(_CRT_RPTHOOK_INSTALL, CrtDbgHook); +#endif + + setup(); + + s_argv_0 = argv[0]; + fail_msg = run_tests(filter, &total_elapsed); + printf("%s, run %d in %.3lfs\n", fail_msg ? "FAIL" : "PASS", num_tests, + total_elapsed); + + teardown(); + if (fail_msg != NULL) { + /* Prevent leak analyzer from running: there will be "leaks" because of + * premature return from the test, and in this case we don't care. */ + _exit(EXIT_FAILURE); + } + return EXIT_SUCCESS; +} diff --git a/test/unit_test.h b/test/unit_test.h new file mode 100644 index 00000000..e40f08b3 --- /dev/null +++ b/test/unit_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2014 Cesanta Software Limited + * All rights reserved + * This software is dual-licensed: you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. For the terms of this + * license, see <http://www.gnu.org/licenses/>. + * + * You are free to use this software under the terms of the GNU General + * Public License, 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. + * + * Alternatively, you can license this software under a commercial + * license, as set out in <https://www.cesanta.com/license>. + */ + +#ifndef CS_MONGOOSE_TEST_UNIT_TEST_H_ +#define CS_MONGOOSE_TEST_UNIT_TEST_H_ + +#ifndef _WIN32 +#include "common/platform.h" +#endif + +extern void *(*test_malloc)(size_t); +extern void *(*test_calloc)(size_t, size_t); + +#endif /* CS_MONGOOSE_TEST_UNIT_TEST_H_ */ diff --git a/test/win/mgtest.sh b/test/win/mgtest.sh new file mode 100755 index 00000000..05c0f1c7 --- /dev/null +++ b/test/win/mgtest.sh @@ -0,0 +1,12 @@ +#!/bin/bash -e + +cd $(dirname $0) +./mkmgtest.cmd $PWD/mgtest.vcxproj +cd .. +cp win/Debug/mgtest.exe . + +./mgtest.exe || { + #hack to flush the logs + echo failed + exit 1 +} diff --git a/test/win/mgtest.sln b/test/win/mgtest.sln new file mode 100644 index 00000000..e78b6436 --- /dev/null +++ b/test/win/mgtest.sln @@ -0,0 +1,28 @@ + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.24720.0 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "mgtest", "mgtest.vcxproj", "{699B1D38-AFCE-4B16-B030-A54833909F8B}" +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|x64 = Debug|x64 + Debug|x86 = Debug|x86 + Release|x64 = Release|x64 + Release|x86 = Release|x86 + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Debug|x64.ActiveCfg = Debug|x64 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Debug|x64.Build.0 = Debug|x64 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Debug|x86.ActiveCfg = Debug|Win32 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Debug|x86.Build.0 = Debug|Win32 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Release|x64.ActiveCfg = Release|x64 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Release|x64.Build.0 = Release|x64 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Release|x86.ActiveCfg = Release|Win32 + {699B1D38-AFCE-4B16-B030-A54833909F8B}.Release|x86.Build.0 = Release|Win32 + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/test/win/mgtest.vcxproj b/test/win/mgtest.vcxproj new file mode 100644 index 00000000..a8920dc6 --- /dev/null +++ b/test/win/mgtest.vcxproj @@ -0,0 +1,161 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project DefaultTargets="Build" ToolsVersion="14.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup Label="ProjectConfigurations"> + <ProjectConfiguration Include="Debug|Win32"> + <Configuration>Debug</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|Win32"> + <Configuration>Release</Configuration> + <Platform>Win32</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Debug|x64"> + <Configuration>Debug</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + <ProjectConfiguration Include="Release|x64"> + <Configuration>Release</Configuration> + <Platform>x64</Platform> + </ProjectConfiguration> + </ItemGroup> + <PropertyGroup Label="Globals"> + <ProjectGuid>{699B1D38-AFCE-4B16-B030-A54833909F8B}</ProjectGuid> + <Keyword>Win32Proj</Keyword> + <RootNamespace>mgtest</RootNamespace> + <WindowsTargetPlatformVersion>8.1</WindowsTargetPlatformVersion> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>true</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> + <ConfigurationType>Application</ConfigurationType> + <UseDebugLibraries>false</UseDebugLibraries> + <PlatformToolset>v140</PlatformToolset> + <WholeProgramOptimization>true</WholeProgramOptimization> + <CharacterSet>Unicode</CharacterSet> + </PropertyGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> + <ImportGroup Label="ExtensionSettings"> + </ImportGroup> + <ImportGroup Label="Shared"> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> + </ImportGroup> + <PropertyGroup Label="UserMacros" /> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <LinkIncremental>true</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <LinkIncremental>false</LinkIncremental> + </PropertyGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> + <ClCompile> + <PrecompiledHeader> + </PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <Optimization>Disabled</Optimization> + <PreprocessorDefinitions>WIN32;MG_ENABLE_POLL_UNTIL;MG_ENABLE_COAP;MG_ENABLE_DNS_SERVER;MG_ENABLE_MQTT_BROKER;MG_ENABLE_THREADS;MG_DISABLE_DAV_AUTH;MG_INTERNAL=;_DEBUG;_CONSOLE;MG_ENABLE_COAP;MG_ENABLE_DNS_SERVER;MG_ENABLE_MQTT_BROKER;MG_ENABLE_THREADS;MG_ENABLE_HTTP_SSI_EXEC;MG_ENABLE_HTTP_WEBDAV;MG_DISABLE_DAV_AUTH;MG_ENABLE_SYNC_RESOLVER;NO_CGI_TEST;NO_RESOLVE_HOSTS_TEST;_CRT_SECURE_NO_WARNINGS;_CRT_NONSTDC_NO_WARNINGS;%(PreprocessorDefinitions)</PreprocessorDefinitions> + <AdditionalIncludeDirectories>../../..</AdditionalIncludeDirectories> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> + <ClCompile> + <PrecompiledHeader> + </PrecompiledHeader> + <WarningLevel>Level3</WarningLevel> + <Optimization>Disabled</Optimization> + <PreprocessorDefinitions>_DEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <PrecompiledHeader> + </PrecompiledHeader> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <PreprocessorDefinitions>WIN32;MG_ENABLE_COAP;MG_ENABLE_DNS_SERVER;MG_ENABLE_MQTT_BROKER;MG_ENABLE_THREADS;MG_ENABLE_HTTP_WEBDAV;MG_ENABLE_HTTP_SSI_EXEC;MG_ENABLE_SYNC_RESOLVER;MG_DISABLE_DAV_AUTH;MG_INTERNAL=;NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> + <ClCompile> + <WarningLevel>Level3</WarningLevel> + <PrecompiledHeader> + </PrecompiledHeader> + <Optimization>MaxSpeed</Optimization> + <FunctionLevelLinking>true</FunctionLevelLinking> + <IntrinsicFunctions>true</IntrinsicFunctions> + <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> + </ClCompile> + <Link> + <SubSystem>Console</SubSystem> + <EnableCOMDATFolding>true</EnableCOMDATFolding> + <OptimizeReferences>true</OptimizeReferences> + <GenerateDebugInformation>true</GenerateDebugInformation> + </Link> + </ItemDefinitionGroup> + <ItemGroup> + <Text Include="ReadMe.txt" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\..\common\test_util.c" /> + <ClCompile Include="..\..\..\mongoose\mongoose.c" /> + <ClCompile Include="..\..\..\mongoose\test\unit_test.c" /> + </ItemGroup> + <ItemGroup> + <ClInclude Include="..\..\..\common\test_util.h" /> + <ClInclude Include="..\..\..\mongoose\mongoose.h" /> + <ClInclude Include="..\..\..\mongoose\test\unit_test.h" /> + </ItemGroup> + <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> + <ImportGroup Label="ExtensionTargets"> + </ImportGroup> +</Project> diff --git a/test/win/mgtest.vcxproj.filters b/test/win/mgtest.vcxproj.filters new file mode 100644 index 00000000..88cc5273 --- /dev/null +++ b/test/win/mgtest.vcxproj.filters @@ -0,0 +1,42 @@ +<?xml version="1.0" encoding="utf-8"?> +<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> + <ItemGroup> + <Filter Include="Source Files"> + <UniqueIdentifier>{4FC737F1-C7A5-4376-A066-2A32D752A2FF}</UniqueIdentifier> + <Extensions>cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx</Extensions> + </Filter> + <Filter Include="Header Files"> + <UniqueIdentifier>{93995380-89BD-4b04-88EB-625FBE52EBFB}</UniqueIdentifier> + <Extensions>h;hh;hpp;hxx;hm;inl;inc;xsd</Extensions> + </Filter> + <Filter Include="Resource Files"> + <UniqueIdentifier>{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}</UniqueIdentifier> + <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> + </Filter> + </ItemGroup> + <ItemGroup> + <Text Include="ReadMe.txt" /> + </ItemGroup> + <ItemGroup> + <ClCompile Include="..\..\..\mongoose\mongoose.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="..\..\..\mongoose\test\unit_test.c"> + <Filter>Source Files</Filter> + </ClCompile> + <ClCompile Include="..\..\..\common\test_util.c"> + <Filter>Source Files</Filter> + </ClCompile> + </ItemGroup> + <ItemGroup> + <ClInclude Include="..\..\..\mongoose\mongoose.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="..\..\..\mongoose\test\unit_test.h"> + <Filter>Header Files</Filter> + </ClInclude> + <ClInclude Include="..\..\..\common\test_util.h"> + <Filter>Header Files</Filter> + </ClInclude> + </ItemGroup> +</Project> \ No newline at end of file diff --git a/test/win/mkmgtest.cmd b/test/win/mkmgtest.cmd new file mode 100644 index 00000000..b1b224fc --- /dev/null +++ b/test/win/mkmgtest.cmd @@ -0,0 +1 @@ +"C:\Program Files (x86)\MSBuild\14.0\Bin\MSBuild.exe" /p:Configuration=Debug /t:Clean;Build %1