https_client.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. #include "libnetdata/libnetdata.h"
  3. #include "https_client.h"
  4. #include "mqtt_websockets/c-rbuf/include/ringbuffer.h"
  5. enum http_parse_state {
  6. HTTP_PARSE_INITIAL = 0,
  7. HTTP_PARSE_HEADERS,
  8. HTTP_PARSE_CONTENT
  9. };
  10. static const char *http_req_type_to_str(http_req_type_t req) {
  11. switch (req) {
  12. case HTTP_REQ_GET:
  13. return "GET";
  14. case HTTP_REQ_POST:
  15. return "POST";
  16. case HTTP_REQ_CONNECT:
  17. return "CONNECT";
  18. default:
  19. return "unknown";
  20. }
  21. }
  22. typedef struct {
  23. enum http_parse_state state;
  24. int content_length;
  25. int http_code;
  26. } http_parse_ctx;
  27. #define HTTP_PARSE_CTX_INITIALIZER { .state = HTTP_PARSE_INITIAL, .content_length = -1, .http_code = 0 }
  28. static inline void http_parse_ctx_clear(http_parse_ctx *ctx) {
  29. ctx->state = HTTP_PARSE_INITIAL;
  30. ctx->content_length = -1;
  31. ctx->http_code = 0;
  32. }
  33. #define POLL_TO_MS 100
  34. #define NEED_MORE_DATA 0
  35. #define PARSE_SUCCESS 1
  36. #define PARSE_ERROR -1
  37. #define HTTP_LINE_TERM "\x0D\x0A"
  38. #define RESP_PROTO "HTTP/1.1 "
  39. #define HTTP_KEYVAL_SEPARATOR ": "
  40. #define HTTP_HDR_BUFFER_SIZE 256
  41. #define PORT_STR_MAX_BYTES 12
  42. static void process_http_hdr(http_parse_ctx *parse_ctx, const char *key, const char *val)
  43. {
  44. // currently we care only about content-length
  45. // but in future the way this is written
  46. // it can be extended
  47. if (!strcmp("content-length", key)) {
  48. parse_ctx->content_length = atoi(val);
  49. }
  50. }
  51. static int parse_http_hdr(rbuf_t buf, http_parse_ctx *parse_ctx)
  52. {
  53. int idx, idx_end;
  54. char buf_key[HTTP_HDR_BUFFER_SIZE];
  55. char buf_val[HTTP_HDR_BUFFER_SIZE];
  56. char *ptr = buf_key;
  57. if (!rbuf_find_bytes(buf, HTTP_LINE_TERM, strlen(HTTP_LINE_TERM), &idx_end)) {
  58. error("CRLF expected");
  59. return 1;
  60. }
  61. char *separator = rbuf_find_bytes(buf, HTTP_KEYVAL_SEPARATOR, strlen(HTTP_KEYVAL_SEPARATOR), &idx);
  62. if (!separator) {
  63. error("Missing Key/Value separator");
  64. return 1;
  65. }
  66. if (idx >= HTTP_HDR_BUFFER_SIZE) {
  67. error("Key name is too long");
  68. return 1;
  69. }
  70. rbuf_pop(buf, buf_key, idx);
  71. buf_key[idx] = 0;
  72. rbuf_bump_tail(buf, strlen(HTTP_KEYVAL_SEPARATOR));
  73. idx_end -= strlen(HTTP_KEYVAL_SEPARATOR) + idx;
  74. if (idx_end >= HTTP_HDR_BUFFER_SIZE) {
  75. error("Value of key \"%s\" too long", buf_key);
  76. return 1;
  77. }
  78. rbuf_pop(buf, buf_val, idx_end);
  79. buf_val[idx_end] = 0;
  80. for (ptr = buf_key; *ptr; ptr++)
  81. *ptr = tolower(*ptr);
  82. process_http_hdr(parse_ctx, buf_key, buf_val);
  83. return 0;
  84. }
  85. static int parse_http_response(rbuf_t buf, http_parse_ctx *parse_ctx)
  86. {
  87. int idx;
  88. char rc[4];
  89. do {
  90. if (parse_ctx->state != HTTP_PARSE_CONTENT && !rbuf_find_bytes(buf, HTTP_LINE_TERM, strlen(HTTP_LINE_TERM), &idx))
  91. return NEED_MORE_DATA;
  92. switch (parse_ctx->state) {
  93. case HTTP_PARSE_INITIAL:
  94. if (rbuf_memcmp_n(buf, RESP_PROTO, strlen(RESP_PROTO))) {
  95. error("Expected response to start with \"%s\"", RESP_PROTO);
  96. return PARSE_ERROR;
  97. }
  98. rbuf_bump_tail(buf, strlen(RESP_PROTO));
  99. if (rbuf_pop(buf, rc, 4) != 4) {
  100. error("Expected HTTP status code");
  101. return PARSE_ERROR;
  102. }
  103. if (rc[3] != ' ') {
  104. error("Expected space after HTTP return code");
  105. return PARSE_ERROR;
  106. }
  107. rc[3] = 0;
  108. parse_ctx->http_code = atoi(rc);
  109. if (parse_ctx->http_code < 100 || parse_ctx->http_code >= 600) {
  110. error("HTTP code not in range 100 to 599");
  111. return PARSE_ERROR;
  112. }
  113. rbuf_find_bytes(buf, HTTP_LINE_TERM, strlen(HTTP_LINE_TERM), &idx);
  114. rbuf_bump_tail(buf, idx + strlen(HTTP_LINE_TERM));
  115. parse_ctx->state = HTTP_PARSE_HEADERS;
  116. break;
  117. case HTTP_PARSE_HEADERS:
  118. if (!idx) {
  119. parse_ctx->state = HTTP_PARSE_CONTENT;
  120. rbuf_bump_tail(buf, strlen(HTTP_LINE_TERM));
  121. break;
  122. }
  123. if (parse_http_hdr(buf, parse_ctx))
  124. return PARSE_ERROR;
  125. rbuf_find_bytes(buf, HTTP_LINE_TERM, strlen(HTTP_LINE_TERM), &idx);
  126. rbuf_bump_tail(buf, idx + strlen(HTTP_LINE_TERM));
  127. break;
  128. case HTTP_PARSE_CONTENT:
  129. // replies like CONNECT etc. do not have content
  130. if (parse_ctx->content_length < 0)
  131. return PARSE_SUCCESS;
  132. if (rbuf_bytes_available(buf) >= (size_t)parse_ctx->content_length)
  133. return PARSE_SUCCESS;
  134. return NEED_MORE_DATA;
  135. }
  136. } while(1);
  137. }
  138. typedef struct https_req_ctx {
  139. https_req_t *request;
  140. int sock;
  141. rbuf_t buf_rx;
  142. struct pollfd poll_fd;
  143. SSL_CTX *ssl_ctx;
  144. SSL *ssl;
  145. size_t written;
  146. int self_signed_allowed;
  147. http_parse_ctx parse_ctx;
  148. time_t req_start_time;
  149. } https_req_ctx_t;
  150. static int https_req_check_timedout(https_req_ctx_t *ctx) {
  151. if (now_realtime_sec() > ctx->req_start_time + ctx->request->timeout_s) {
  152. error("request timed out");
  153. return 1;
  154. }
  155. return 0;
  156. }
  157. static char *_ssl_err_tos(int err)
  158. {
  159. switch(err){
  160. case SSL_ERROR_SSL:
  161. return "SSL_ERROR_SSL";
  162. case SSL_ERROR_WANT_READ:
  163. return "SSL_ERROR_WANT_READ";
  164. case SSL_ERROR_WANT_WRITE:
  165. return "SSL_ERROR_WANT_WRITE";
  166. case SSL_ERROR_NONE:
  167. return "SSL_ERROR_NONE";
  168. case SSL_ERROR_ZERO_RETURN:
  169. return "SSL_ERROR_ZERO_RETURN";
  170. case SSL_ERROR_WANT_CONNECT:
  171. return "SSL_ERROR_WANT_CONNECT";
  172. case SSL_ERROR_WANT_ACCEPT:
  173. return "SSL_ERROR_WANT_ACCEPT";
  174. }
  175. return "Unknown!!!";
  176. }
  177. static int socket_write_all(https_req_ctx_t *ctx, char *data, size_t data_len) {
  178. ctx->written = 0;
  179. ctx->poll_fd.events = POLLOUT;
  180. do {
  181. int ret = poll(&ctx->poll_fd, 1, POLL_TO_MS);
  182. if (ret < 0) {
  183. error("poll error");
  184. return 1;
  185. }
  186. if (ret == 0) {
  187. if (https_req_check_timedout(ctx)) {
  188. error("Poll timed out");
  189. return 2;
  190. }
  191. continue;
  192. }
  193. ret = write(ctx->sock, &data[ctx->written], data_len - ctx->written);
  194. if (ret > 0) {
  195. ctx->written += ret;
  196. } else if (errno != EAGAIN && errno != EWOULDBLOCK) {
  197. error("Error writing to socket");
  198. return 3;
  199. }
  200. } while (ctx->written < data_len);
  201. return 0;
  202. }
  203. static int ssl_write_all(https_req_ctx_t *ctx, char *data, size_t data_len) {
  204. ctx->written = 0;
  205. ctx->poll_fd.events |= POLLOUT;
  206. do {
  207. int ret = poll(&ctx->poll_fd, 1, POLL_TO_MS);
  208. if (ret < 0) {
  209. error("poll error");
  210. return 1;
  211. }
  212. if (ret == 0) {
  213. if (https_req_check_timedout(ctx)) {
  214. error("Poll timed out");
  215. return 2;
  216. }
  217. continue;
  218. }
  219. ctx->poll_fd.events = 0;
  220. ret = SSL_write(ctx->ssl, &data[ctx->written], data_len - ctx->written);
  221. if (ret > 0) {
  222. ctx->written += ret;
  223. } else {
  224. ret = SSL_get_error(ctx->ssl, ret);
  225. switch (ret) {
  226. case SSL_ERROR_WANT_READ:
  227. ctx->poll_fd.events |= POLLIN;
  228. break;
  229. case SSL_ERROR_WANT_WRITE:
  230. ctx->poll_fd.events |= POLLOUT;
  231. break;
  232. default:
  233. error("SSL_write Err: %s", _ssl_err_tos(ret));
  234. return 3;
  235. }
  236. }
  237. } while (ctx->written < data_len);
  238. return 0;
  239. }
  240. static inline int https_client_write_all(https_req_ctx_t *ctx, char *data, size_t data_len) {
  241. if (ctx->ssl_ctx)
  242. return ssl_write_all(ctx, data, data_len);
  243. return socket_write_all(ctx, data, data_len);
  244. }
  245. static int read_parse_response(https_req_ctx_t *ctx) {
  246. int ret;
  247. char *ptr;
  248. size_t size;
  249. ctx->poll_fd.events = POLLIN;
  250. do {
  251. ret = poll(&ctx->poll_fd, 1, POLL_TO_MS);
  252. if (ret < 0) {
  253. error("poll error");
  254. return 1;
  255. }
  256. if (ret == 0) {
  257. if (https_req_check_timedout(ctx)) {
  258. error("Poll timed out");
  259. return 2;
  260. }
  261. if (!ctx->ssl_ctx)
  262. continue;
  263. }
  264. ctx->poll_fd.events = 0;
  265. ptr = rbuf_get_linear_insert_range(ctx->buf_rx, &size);
  266. if (ctx->ssl_ctx)
  267. ret = SSL_read(ctx->ssl, ptr, size);
  268. else
  269. ret = read(ctx->sock, ptr, size);
  270. if (ret > 0) {
  271. rbuf_bump_head(ctx->buf_rx, ret);
  272. } else {
  273. if (ctx->ssl_ctx) {
  274. ret = SSL_get_error(ctx->ssl, ret);
  275. switch (ret) {
  276. case SSL_ERROR_WANT_READ:
  277. ctx->poll_fd.events |= POLLIN;
  278. break;
  279. case SSL_ERROR_WANT_WRITE:
  280. ctx->poll_fd.events |= POLLOUT;
  281. break;
  282. default:
  283. error("SSL_read Err: %s", _ssl_err_tos(ret));
  284. return 3;
  285. }
  286. } else {
  287. if (errno != EAGAIN && errno != EWOULDBLOCK) {
  288. error("write error");
  289. return 3;
  290. }
  291. ctx->poll_fd.events |= POLLIN;
  292. }
  293. }
  294. } while (!(ret = parse_http_response(ctx->buf_rx, &ctx->parse_ctx)));
  295. if (ret != PARSE_SUCCESS) {
  296. error("Error parsing HTTP response");
  297. return 1;
  298. }
  299. return 0;
  300. }
  301. #define TX_BUFFER_SIZE 8192
  302. #define RX_BUFFER_SIZE (TX_BUFFER_SIZE*2)
  303. static int handle_http_request(https_req_ctx_t *ctx) {
  304. BUFFER *hdr = buffer_create(TX_BUFFER_SIZE);
  305. int rc = 0;
  306. http_parse_ctx_clear(&ctx->parse_ctx);
  307. // Prepare data to send
  308. switch (ctx->request->request_type) {
  309. case HTTP_REQ_CONNECT:
  310. buffer_strcat(hdr, "CONNECT ");
  311. break;
  312. case HTTP_REQ_GET:
  313. buffer_strcat(hdr, "GET ");
  314. break;
  315. case HTTP_REQ_POST:
  316. buffer_strcat(hdr, "POST ");
  317. break;
  318. default:
  319. error("Unknown HTTPS request type!");
  320. rc = 1;
  321. goto err_exit;
  322. }
  323. if (ctx->request->request_type == HTTP_REQ_CONNECT) {
  324. buffer_strcat(hdr, ctx->request->host);
  325. buffer_sprintf(hdr, ":%d", ctx->request->port);
  326. } else {
  327. buffer_strcat(hdr, ctx->request->url);
  328. }
  329. buffer_strcat(hdr, " HTTP/1.1\x0D\x0A");
  330. //TODO Headers!
  331. if (ctx->request->request_type != HTTP_REQ_CONNECT) {
  332. buffer_sprintf(hdr, "Host: %s\x0D\x0A", ctx->request->host);
  333. }
  334. buffer_strcat(hdr, "User-Agent: Netdata/rocks newhttpclient\x0D\x0A");
  335. if (ctx->request->request_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
  336. buffer_sprintf(hdr, "Content-Length: %zu\x0D\x0A", ctx->request->payload_size);
  337. }
  338. buffer_strcat(hdr, "\x0D\x0A");
  339. // Send the request
  340. if (https_client_write_all(ctx, hdr->buffer, hdr->len)) {
  341. error("Couldn't write HTTP request header into SSL connection");
  342. rc = 2;
  343. goto err_exit;
  344. }
  345. if (ctx->request->request_type == HTTP_REQ_POST && ctx->request->payload && ctx->request->payload_size) {
  346. if (https_client_write_all(ctx, ctx->request->payload, ctx->request->payload_size)) {
  347. error("Couldn't write payload into SSL connection");
  348. rc = 3;
  349. goto err_exit;
  350. }
  351. }
  352. // Read The Response
  353. if (read_parse_response(ctx)) {
  354. error("Error reading or parsing response from server");
  355. rc = 4;
  356. goto err_exit;
  357. }
  358. err_exit:
  359. buffer_free(hdr);
  360. return rc;
  361. }
  362. static int cert_verify_callback(int preverify_ok, X509_STORE_CTX *ctx)
  363. {
  364. X509 *err_cert;
  365. int err, depth;
  366. char *err_str;
  367. if (!preverify_ok) {
  368. err = X509_STORE_CTX_get_error(ctx);
  369. depth = X509_STORE_CTX_get_error_depth(ctx);
  370. err_cert = X509_STORE_CTX_get_current_cert(ctx);
  371. err_str = X509_NAME_oneline(X509_get_subject_name(err_cert), NULL, 0);
  372. error("Cert Chain verify error:num=%d:%s:depth=%d:%s", err,
  373. X509_verify_cert_error_string(err), depth, err_str);
  374. free(err_str);
  375. }
  376. #ifdef ACLK_SSL_ALLOW_SELF_SIGNED
  377. if (!preverify_ok && err == X509_V_ERR_DEPTH_ZERO_SELF_SIGNED_CERT)
  378. {
  379. preverify_ok = 1;
  380. error("Self Signed Certificate Accepted as the agent was built with ACLK_SSL_ALLOW_SELF_SIGNED");
  381. }
  382. #endif
  383. return preverify_ok;
  384. }
  385. int https_request(https_req_t *request, https_req_response_t *response) {
  386. int rc = 1, ret;
  387. char connect_port_str[PORT_STR_MAX_BYTES];
  388. const char *connect_host = request->proxy_host ? request->proxy_host : request->host;
  389. int connect_port = request->proxy_host ? request->proxy_port : request->port;
  390. struct timeval timeout = { .tv_sec = request->timeout_s, .tv_usec = 0 };
  391. https_req_ctx_t *ctx = callocz(1, sizeof(https_req_ctx_t));
  392. ctx->req_start_time = now_realtime_sec();
  393. ctx->buf_rx = rbuf_create(RX_BUFFER_SIZE);
  394. if (!ctx->buf_rx) {
  395. error("Couldn't allocate buffer for RX data");
  396. goto exit_req_ctx;
  397. }
  398. snprintfz(connect_port_str, PORT_STR_MAX_BYTES, "%d", connect_port);
  399. ctx->sock = connect_to_this_ip46(IPPROTO_TCP, SOCK_STREAM, connect_host, 0, connect_port_str, &timeout);
  400. if (ctx->sock < 0) {
  401. error("Error connecting TCP socket to \"%s\"", connect_host);
  402. goto exit_buf_rx;
  403. }
  404. if (fcntl(ctx->sock, F_SETFL, fcntl(ctx->sock, F_GETFL, 0) | O_NONBLOCK) == -1) {
  405. error("Error setting O_NONBLOCK to TCP socket.");
  406. goto exit_sock;
  407. }
  408. ctx->poll_fd.fd = ctx->sock;
  409. // Do the CONNECT if proxy is used
  410. if (request->proxy_host) {
  411. https_req_t req = HTTPS_REQ_T_INITIALIZER;
  412. req.request_type = HTTP_REQ_CONNECT;
  413. req.timeout_s = request->timeout_s;
  414. req.host = request->host;
  415. req.port = request->port;
  416. req.url = request->url;
  417. ctx->request = &req;
  418. if (handle_http_request(ctx)) {
  419. error("Failed to CONNECT with proxy");
  420. goto exit_sock;
  421. }
  422. if (ctx->parse_ctx.http_code != 200) {
  423. error("Proxy didn't return 200 OK (got %d)", ctx->parse_ctx.http_code);
  424. goto exit_sock;
  425. }
  426. info("Proxy accepted CONNECT upgrade");
  427. }
  428. ctx->request = request;
  429. ctx->ssl_ctx = security_initialize_openssl_client();
  430. if (ctx->ssl_ctx==NULL) {
  431. error("Cannot allocate SSL context");
  432. goto exit_sock;
  433. }
  434. if (!SSL_CTX_set_default_verify_paths(ctx->ssl_ctx)) {
  435. error("Error setting default verify paths");
  436. goto exit_CTX;
  437. }
  438. SSL_CTX_set_verify(ctx->ssl_ctx, SSL_VERIFY_PEER | SSL_VERIFY_CLIENT_ONCE, cert_verify_callback);
  439. ctx->ssl = SSL_new(ctx->ssl_ctx);
  440. if (ctx->ssl==NULL) {
  441. error("Cannot allocate SSL");
  442. goto exit_CTX;
  443. }
  444. SSL_set_fd(ctx->ssl, ctx->sock);
  445. ret = SSL_connect(ctx->ssl);
  446. if (ret != -1 && ret != 1) {
  447. error("SSL could not connect");
  448. goto exit_SSL;
  449. }
  450. if (ret == -1) {
  451. // expected as underlying socket is non blocking!
  452. // consult SSL_connect documentation for details
  453. int ec = SSL_get_error(ctx->ssl, ret);
  454. if (ec != SSL_ERROR_WANT_READ && ec != SSL_ERROR_WANT_WRITE) {
  455. error("Failed to start SSL connection");
  456. goto exit_SSL;
  457. }
  458. }
  459. // The actual request here
  460. if (handle_http_request(ctx)) {
  461. error("Couldn't process request");
  462. goto exit_SSL;
  463. }
  464. response->http_code = ctx->parse_ctx.http_code;
  465. if (ctx->parse_ctx.content_length > 0) {
  466. response->payload_size = ctx->parse_ctx.content_length;
  467. response->payload = mallocz(response->payload_size + 1);
  468. ret = rbuf_pop(ctx->buf_rx, response->payload, response->payload_size);
  469. if (ret != (int)response->payload_size) {
  470. error("Payload size doesn't match remaining data on the buffer!");
  471. response->payload_size = ret;
  472. }
  473. // normally we take payload as it is and copy it
  474. // but for convenience in cases where payload is sth. like
  475. // json we add terminating zero so that user of the data
  476. // doesn't have to convert to C string (0 terminated)
  477. // other uses still have correct payload_size and can copy
  478. // only exact data without affixed 0x00
  479. ((char*)response->payload)[response->payload_size] = 0; // mallocz(response->payload_size + 1);
  480. }
  481. info("HTTPS \"%s\" request to \"%s\" finished with HTTP code: %d", http_req_type_to_str(ctx->request->request_type), ctx->request->host, response->http_code);
  482. rc = 0;
  483. exit_SSL:
  484. SSL_free(ctx->ssl);
  485. exit_CTX:
  486. SSL_CTX_free(ctx->ssl_ctx);
  487. exit_sock:
  488. close(ctx->sock);
  489. exit_buf_rx:
  490. rbuf_free(ctx->buf_rx);
  491. exit_req_ctx:
  492. freez(ctx);
  493. return rc;
  494. }
  495. void https_req_response_free(https_req_response_t *res) {
  496. freez(res->payload);
  497. }
  498. void https_req_response_init(https_req_response_t *res) {
  499. res->http_code = 0;
  500. res->payload = NULL;
  501. res->payload_size = 0;
  502. }
  503. static inline char *UNUSED_FUNCTION(min_non_null)(char *a, char *b) {
  504. if (!a)
  505. return b;
  506. if (!b)
  507. return a;
  508. return (a < b ? a : b);
  509. }
  510. #define URI_PROTO_SEPARATOR "://"
  511. #define URL_PARSER_LOG_PREFIX "url_parser "
  512. static int parse_host_port(url_t *url) {
  513. char *ptr = strrchr(url->host, ':');
  514. if (ptr) {
  515. size_t port_len = strlen(ptr + 1);
  516. if (!port_len) {
  517. error(URL_PARSER_LOG_PREFIX ": specified but no port number");
  518. return 1;
  519. }
  520. if (port_len > 5 /* MAX port length is 5digit long in decimal */) {
  521. error(URL_PARSER_LOG_PREFIX "port # is too long");
  522. return 1;
  523. }
  524. *ptr = 0;
  525. if (!strlen(url->host)) {
  526. error(URL_PARSER_LOG_PREFIX "host empty after removing port");
  527. return 1;
  528. }
  529. url->port = atoi (ptr + 1);
  530. }
  531. return 0;
  532. }
  533. static inline void port_by_proto(url_t *url) {
  534. if (url->port)
  535. return;
  536. if (!url->proto)
  537. return;
  538. if (!strcmp(url->proto, "http")) {
  539. url->port = 80;
  540. return;
  541. }
  542. if (!strcmp(url->proto, "https")) {
  543. url->port = 443;
  544. return;
  545. }
  546. }
  547. #define STRDUPZ_2PTR(dest, start, end) \
  548. { \
  549. dest = mallocz(1 + end - start); \
  550. memcpy(dest, start, end - start); \
  551. dest[end - start] = 0; \
  552. }
  553. int url_parse(const char *url, url_t *parsed) {
  554. const char *start = url;
  555. const char *end = strstr(url, URI_PROTO_SEPARATOR);
  556. if (end) {
  557. if (end == start) {
  558. error (URL_PARSER_LOG_PREFIX "found " URI_PROTO_SEPARATOR " without protocol specified");
  559. return 1;
  560. }
  561. STRDUPZ_2PTR(parsed->proto, start, end)
  562. start = end + strlen(URI_PROTO_SEPARATOR);
  563. }
  564. end = strchr(start, '/');
  565. if (!end)
  566. end = start + strlen(start);
  567. if (start == end) {
  568. error(URL_PARSER_LOG_PREFIX "Host empty");
  569. return 1;
  570. }
  571. STRDUPZ_2PTR(parsed->host, start, end);
  572. if (parse_host_port(parsed))
  573. return 1;
  574. if (!*end) {
  575. parsed->path = strdupz("/");
  576. port_by_proto(parsed);
  577. return 0;
  578. }
  579. parsed->path = strdupz(end);
  580. port_by_proto(parsed);
  581. return 0;
  582. }
  583. void url_t_destroy(url_t *url) {
  584. freez(url->host);
  585. freez(url->path);
  586. freez(url->proto);
  587. }