|
@@ -2622,26 +2622,43 @@ static int is_valid_http_method(const char *method) {
|
|
|
// Parse HTTP request, fill in mg_request_info structure.
|
|
|
// This function modifies the buffer with HTTP request by nul-terminating
|
|
|
// HTTP request components, header names and header values.
|
|
|
-static int parse_http_request(char *buf, struct mg_request_info *ri) {
|
|
|
- int status = 0;
|
|
|
-
|
|
|
- // RFC says that all initial whitespaces should be ingored
|
|
|
- while (*buf != '\0' && isspace(* (unsigned char *) buf)) {
|
|
|
- buf++;
|
|
|
+static int parse_http_message(char *buf, int len, struct mg_request_info *ri) {
|
|
|
+ int request_length = get_request_len(buf, len);
|
|
|
+ if (request_length > 0) {
|
|
|
+ // Reset attributes. DO NOT TOUCH is_ssl, remote_ip, remote_port
|
|
|
+ ri->remote_user = ri->request_method = ri->uri = ri->http_version;
|
|
|
+ ri->num_headers = 0;
|
|
|
+ ri->status_code = -1;
|
|
|
+
|
|
|
+ buf[request_length - 1] = '\0';
|
|
|
+
|
|
|
+ // RFC says that all initial whitespaces should be ingored
|
|
|
+ while (*buf != '\0' && isspace(* (unsigned char *) buf)) {
|
|
|
+ buf++;
|
|
|
+ }
|
|
|
+ ri->request_method = skip(&buf, " ");
|
|
|
+ ri->uri = skip(&buf, " ");
|
|
|
+ ri->http_version = skip(&buf, "\r\n");
|
|
|
+ parse_http_headers(&buf, ri);
|
|
|
}
|
|
|
+ return request_length;
|
|
|
+}
|
|
|
|
|
|
- ri->request_method = skip(&buf, " ");
|
|
|
- ri->uri = skip(&buf, " ");
|
|
|
- ri->http_version = skip(&buf, "\r\n");
|
|
|
-
|
|
|
- if (is_valid_http_method(ri->request_method) &&
|
|
|
- strncmp(ri->http_version, "HTTP/", 5) == 0) {
|
|
|
+static int parse_http_request(char *buf, int len, struct mg_request_info *ri) {
|
|
|
+ int result = parse_http_message(buf, len, ri);
|
|
|
+ if (result > 0 &&
|
|
|
+ is_valid_http_method(ri->request_method) &&
|
|
|
+ !strncmp(ri->http_version, "HTTP/", 5)) {
|
|
|
ri->http_version += 5; // Skip "HTTP/"
|
|
|
- parse_http_headers(&buf, ri);
|
|
|
- status = 1;
|
|
|
+ } else {
|
|
|
+ result = -1;
|
|
|
}
|
|
|
+ return result;
|
|
|
+}
|
|
|
|
|
|
- return status;
|
|
|
+static int parse_http_response(char *buf, int len, struct mg_request_info *ri) {
|
|
|
+ int result = parse_http_message(buf, len, ri);
|
|
|
+ return result > 0 && !strncmp(ri->request_method, "HTTP/", 5) ? result : -1;
|
|
|
}
|
|
|
|
|
|
// Keep reading the input (either opened file descriptor fd, or socket sock,
|
|
@@ -2659,7 +2676,7 @@ static int read_request(FILE *fp, SOCKET sock, SSL *ssl, char *buf, int bufsiz,
|
|
|
(n = pull(fp, sock, ssl, buf + *nread, bufsiz - *nread)) > 0) {
|
|
|
*nread += n;
|
|
|
}
|
|
|
- } while (*nread < bufsiz && request_len == 0 && n > 0);
|
|
|
+ } while (*nread <= bufsiz && request_len == 0 && n > 0);
|
|
|
|
|
|
return request_len;
|
|
|
}
|
|
@@ -3802,14 +3819,7 @@ static int set_acl_option(struct mg_context *ctx) {
|
|
|
}
|
|
|
|
|
|
static void reset_per_request_attributes(struct mg_connection *conn) {
|
|
|
- struct mg_request_info *ri = &conn->request_info;
|
|
|
-
|
|
|
- // Reset request info attributes. DO NOT TOUCH is_ssl, remote_ip, remote_port
|
|
|
- ri->remote_user = ri->request_method = ri->uri = ri->http_version =
|
|
|
- conn->path_info = NULL;
|
|
|
- ri->num_headers = 0;
|
|
|
- ri->status_code = -1;
|
|
|
-
|
|
|
+ conn->path_info = NULL;
|
|
|
conn->num_bytes_sent = conn->consumed_content = 0;
|
|
|
conn->content_len = -1;
|
|
|
conn->request_len = conn->data_len = 0;
|
|
@@ -3855,6 +3865,88 @@ static void close_connection(struct mg_connection *conn) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
+struct mg_connection *mg_connect(struct mg_context *ctx,
|
|
|
+ const char *host, int port, int use_ssl) {
|
|
|
+ struct mg_connection *newconn = NULL;
|
|
|
+ struct sockaddr_in sin;
|
|
|
+ struct hostent *he;
|
|
|
+ int sock;
|
|
|
+
|
|
|
+ if (ctx->ssl_ctx == NULL && use_ssl) {
|
|
|
+ cry(fc(ctx), "%s: SSL is not initialized", __func__);
|
|
|
+ } else if ((he = gethostbyname(host)) == NULL) {
|
|
|
+ cry(fc(ctx), "%s: gethostbyname(%s): %s", __func__, host, strerror(ERRNO));
|
|
|
+ } else if ((sock = socket(PF_INET, SOCK_STREAM, 0)) == INVALID_SOCKET) {
|
|
|
+ cry(fc(ctx), "%s: socket: %s", __func__, strerror(ERRNO));
|
|
|
+ } else {
|
|
|
+ sin.sin_family = AF_INET;
|
|
|
+ sin.sin_port = htons((uint16_t) port);
|
|
|
+ sin.sin_addr = * (struct in_addr *) he->h_addr_list[0];
|
|
|
+ if (connect(sock, (struct sockaddr *) &sin, sizeof(sin)) != 0) {
|
|
|
+ cry(fc(ctx), "%s: connect(%s:%d): %s", __func__, host, port,
|
|
|
+ strerror(ERRNO));
|
|
|
+ closesocket(sock);
|
|
|
+ } else if ((newconn = (struct mg_connection *)
|
|
|
+ calloc(1, sizeof(*newconn))) == NULL) {
|
|
|
+ cry(fc(ctx), "%s: calloc: %s", __func__, strerror(ERRNO));
|
|
|
+ closesocket(sock);
|
|
|
+ } else {
|
|
|
+ newconn->client.sock = sock;
|
|
|
+ newconn->client.rsa.sin = sin;
|
|
|
+ newconn->client.is_ssl = use_ssl;
|
|
|
+ if (use_ssl) {
|
|
|
+ sslize(newconn, SSL_connect);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ return newconn;
|
|
|
+}
|
|
|
+
|
|
|
+FILE *mg_fetch(struct mg_context *ctx, const char *url, const char *path,
|
|
|
+ struct mg_request_info *ri) {
|
|
|
+ struct mg_connection *newconn;
|
|
|
+ int n, req_length, data_length = 0, port = 80;
|
|
|
+ char host[1025], proto[10], buf[16384];
|
|
|
+ FILE *fp = NULL;
|
|
|
+
|
|
|
+ if (sscanf(url, "%9[htps]://%1024[^:]:%d/%n", proto, host, &port, &n) != 3 &&
|
|
|
+ sscanf(url, "%9[htps]://%1024[^/]/%n", proto, host, &n) != 2) {
|
|
|
+ cry(fc(ctx), "%s: invalid URL: [%s]", __func__, url);
|
|
|
+ } else if ((newconn = mg_connect(ctx, host, port,
|
|
|
+ !strcmp(proto, "https"))) == NULL) {
|
|
|
+ cry(fc(ctx), "%s: mg_connect(%s): %s", __func__, url, strerror(ERRNO));
|
|
|
+ } else {
|
|
|
+ mg_printf(newconn, "GET /%s HTTP/1.0\r\n\r\n", url + n);
|
|
|
+ req_length = read_request(NULL, newconn->client.sock,
|
|
|
+ newconn->ssl, buf, sizeof(buf), &data_length);
|
|
|
+ if (req_length <= 0) {
|
|
|
+ cry(fc(ctx), "%s(%s): invalid HTTP reply", __func__, url);
|
|
|
+ } else if (parse_http_response(buf, req_length, ri) <= 0) {
|
|
|
+ cry(fc(ctx), "%s(%s): cannot parse HTTP headers", __func__, url);
|
|
|
+ } else if ((fp = fopen(path, "w+b")) == NULL) {
|
|
|
+ cry(fc(ctx), "%s: fopen(%s): %s", __func__, path, strerror(ERRNO));
|
|
|
+ } else {
|
|
|
+ data_length -= req_length;
|
|
|
+ memmove(buf, buf + req_length, data_length);
|
|
|
+ do {
|
|
|
+ if (fwrite(buf, 1, data_length, fp) != (size_t) data_length) {
|
|
|
+ fclose(fp);
|
|
|
+ fp = NULL;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ data_length = mg_read(newconn, buf, sizeof(buf));
|
|
|
+ } while (data_length > 0);
|
|
|
+ }
|
|
|
+ close_connection(newconn);
|
|
|
+ free(newconn);
|
|
|
+ }
|
|
|
+
|
|
|
+ return fp;
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
static void discard_current_request_from_buffer(struct mg_connection *conn) {
|
|
|
char *buffered;
|
|
|
int buffered_len, body_len;
|
|
@@ -3903,9 +3995,8 @@ static void process_new_connection(struct mg_connection *conn) {
|
|
|
return; // Remote end closed the connection
|
|
|
}
|
|
|
|
|
|
- // Nul-terminate the request cause parse_http_request() uses sscanf
|
|
|
- conn->buf[conn->request_len - 1] = '\0';
|
|
|
- if (!parse_http_request(conn->buf, ri) || !is_valid_uri(ri->uri)) {
|
|
|
+ if (parse_http_request(conn->buf, conn->buf_size, ri) <= 0 ||
|
|
|
+ !is_valid_uri(ri->uri)) {
|
|
|
// Do not put garbage in the access log, just send it back to the client
|
|
|
send_http_error(conn, 400, "Bad Request",
|
|
|
"Cannot parse HTTP request: [%.*s]", conn->data_len, conn->buf);
|