summaryrefslogtreecommitdiffstats
path: root/src/aclk/https_client.c
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/aclk/https_client.c (renamed from aclk/https_client.c)81
1 files changed, 39 insertions, 42 deletions
diff --git a/aclk/https_client.c b/src/aclk/https_client.c
index 5385786b8..2bc768f24 100644
--- a/aclk/https_client.c
+++ b/src/aclk/https_client.c
@@ -8,8 +8,6 @@
#include "daemon/global_statistics.h"
-#define DEFAULT_CHUNKED_RESPONSE_BUFFER_SIZE (4096)
-
static const char *http_req_type_to_str(http_req_type_t req) {
switch (req) {
case HTTP_REQ_GET:
@@ -25,10 +23,9 @@ static const char *http_req_type_to_str(http_req_type_t req) {
#define TRANSFER_ENCODING_CHUNKED (-2)
-#define HTTP_PARSE_CTX_INITIALIZER { .state = HTTP_PARSE_INITIAL, .content_length = -1, .http_code = 0 }
-void http_parse_ctx_create(http_parse_ctx *ctx)
+void http_parse_ctx_create(http_parse_ctx *ctx, enum http_parse_state parse_state)
{
- ctx->state = HTTP_PARSE_INITIAL;
+ ctx->state = parse_state;
ctx->content_length = -1;
ctx->http_code = 0;
ctx->headers = c_rhash_new(0);
@@ -54,6 +51,7 @@ void http_parse_ctx_destroy(http_parse_ctx *ctx)
#define HTTP_LINE_TERM "\x0D\x0A"
#define RESP_PROTO "HTTP/1.1 "
+#define RESP_PROTO10 "HTTP/1.0 "
#define HTTP_KEYVAL_SEPARATOR ": "
#define HTTP_HDR_BUFFER_SIZE 1024
#define PORT_STR_MAX_BYTES 12
@@ -247,10 +245,20 @@ http_parse_rc parse_http_response(rbuf_t buf, http_parse_ctx *parse_ctx)
if (parse_ctx->state != HTTP_PARSE_CONTENT && !rbuf_find_bytes(buf, HTTP_LINE_TERM, strlen(HTTP_LINE_TERM), &idx))
return HTTP_PARSE_NEED_MORE_DATA;
switch (parse_ctx->state) {
+ case HTTP_PARSE_PROXY_CONNECT:
case HTTP_PARSE_INITIAL:
if (rbuf_memcmp_n(buf, RESP_PROTO, strlen(RESP_PROTO))) {
- netdata_log_error("Expected response to start with \"%s\"", RESP_PROTO);
- return HTTP_PARSE_ERROR;
+ if (parse_ctx->state == HTTP_PARSE_PROXY_CONNECT) {
+ if (rbuf_memcmp_n(buf, RESP_PROTO10, strlen(RESP_PROTO10))) {
+ netdata_log_error(
+ "Expected response to start with \"%s\" or \"%s\"", RESP_PROTO, RESP_PROTO10);
+ return HTTP_PARSE_ERROR;
+ }
+ }
+ else {
+ netdata_log_error("Expected response to start with \"%s\"", RESP_PROTO);
+ return HTTP_PARSE_ERROR;
+ }
}
rbuf_bump_tail(buf, strlen(RESP_PROTO));
if (rbuf_pop(buf, rc, 4) != 4) {
@@ -316,8 +324,6 @@ typedef struct https_req_ctx {
size_t written;
- int self_signed_allowed;
-
http_parse_ctx parse_ctx;
time_t req_start_time;
@@ -494,47 +500,45 @@ static int read_parse_response(https_req_ctx_t *ctx) {
return 0;
}
+static const char *http_methods[] = {
+ [HTTP_REQ_GET] = "GET ",
+ [HTTP_REQ_POST] = "POST ",
+ [HTTP_REQ_CONNECT] = "CONNECT ",
+};
+
+
#define TX_BUFFER_SIZE 8192
#define RX_BUFFER_SIZE (TX_BUFFER_SIZE*2)
static int handle_http_request(https_req_ctx_t *ctx) {
BUFFER *hdr = buffer_create(TX_BUFFER_SIZE, &netdata_buffers_statistics.buffers_aclk);
int rc = 0;
- http_parse_ctx_create(&ctx->parse_ctx);
+ http_req_type_t req_type = ctx->request->request_type;
- // Prepare data to send
- switch (ctx->request->request_type) {
- case HTTP_REQ_CONNECT:
- buffer_strcat(hdr, "CONNECT ");
- break;
- case HTTP_REQ_GET:
- buffer_strcat(hdr, "GET ");
- break;
- case HTTP_REQ_POST:
- buffer_strcat(hdr, "POST ");
- break;
- default:
- netdata_log_error("Unknown HTTPS request type!");
- rc = 1;
- goto err_exit;
+ if (req_type >= HTTP_REQ_INVALID) {
+ netdata_log_error("Unknown HTTPS request type!");
+ rc = 1;
+ goto err_exit;
}
+ buffer_strcat(hdr, http_methods[req_type]);
- if (ctx->request->request_type == HTTP_REQ_CONNECT) {
+ if (req_type == HTTP_REQ_CONNECT) {
buffer_strcat(hdr, ctx->request->host);
buffer_sprintf(hdr, ":%d", ctx->request->port);
- } else {
+ http_parse_ctx_create(&ctx->parse_ctx, HTTP_PARSE_PROXY_CONNECT);
+ }
+ else {
buffer_strcat(hdr, ctx->request->url);
+ http_parse_ctx_create(&ctx->parse_ctx, HTTP_PARSE_INITIAL);
}
buffer_strcat(hdr, HTTP_1_1 HTTP_ENDL);
//TODO Headers!
- if (ctx->request->request_type != HTTP_REQ_CONNECT) {
- buffer_sprintf(hdr, "Host: %s\x0D\x0A", ctx->request->host);
- }
+ buffer_sprintf(hdr, "Host: %s\x0D\x0A", ctx->request->host);
buffer_strcat(hdr, "User-Agent: Netdata/rocks newhttpclient\x0D\x0A");
- if (ctx->request->request_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
+ if (req_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
buffer_sprintf(hdr, "Content-Length: %zu\x0D\x0A", ctx->request->payload_size);
}
if (ctx->request->proxy_username) {
@@ -565,7 +569,7 @@ static int handle_http_request(https_req_ctx_t *ctx) {
goto err_exit;
}
- if (ctx->request->request_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
+ if (req_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
if (https_client_write_all(ctx, ctx->request->payload, ctx->request->payload_size)) {
netdata_log_error("Couldn't write payload into SSL connection");
rc = 3;
@@ -762,12 +766,6 @@ void https_req_response_free(https_req_response_t *res) {
freez(res->payload);
}
-void https_req_response_init(https_req_response_t *res) {
- res->http_code = 0;
- res->payload = NULL;
- res->payload_size = 0;
-}
-
static inline char *UNUSED_FUNCTION(min_non_null)(char *a, char *b) {
if (!a)
return b;
@@ -816,12 +814,11 @@ static inline void port_by_proto(url_t *url) {
}
}
-#define STRDUPZ_2PTR(dest, start, end) \
- { \
+#define STRDUPZ_2PTR(dest, start, end) do { \
dest = mallocz(1 + end - start); \
memcpy(dest, start, end - start); \
dest[end - start] = 0; \
- }
+ } while(0)
int url_parse(const char *url, url_t *parsed) {
const char *start = url;
@@ -833,7 +830,7 @@ int url_parse(const char *url, url_t *parsed) {
return 1;
}
- STRDUPZ_2PTR(parsed->proto, start, end)
+ STRDUPZ_2PTR(parsed->proto, start, end);
start = end + strlen(URI_PROTO_SEPARATOR);
}