testhttp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /*
  2. * Copyright (c) 2020 SignalWire, Inc
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in all
  12. * copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. * SOFTWARE.
  21. */
  22. #include "libks/ks.h"
  23. #include <tap.h>
  24. #ifdef _WINDOWS_
  25. #undef unlink
  26. #define unlink _unlink
  27. #endif
  28. static char v4[48] = "";
  29. static char v6[48] = "";
  30. static int mask = 0;
  31. static int tcp_port = 8090;
  32. static char __MSG[] = "TESTING................................................................................/TESTING";
  33. typedef struct ssl_profile_s {
  34. const SSL_METHOD *ssl_method;
  35. SSL_CTX *ssl_ctx;
  36. char cert[512];
  37. char key[512];
  38. char chain[512];
  39. } ssl_profile_t;
  40. static int init_ssl(ssl_profile_t *profile)
  41. {
  42. const char *err = "";
  43. profile->ssl_ctx = SSL_CTX_new(profile->ssl_method); /* create context */
  44. assert(profile->ssl_ctx);
  45. /* Disable SSLv2 */
  46. SSL_CTX_set_options(profile->ssl_ctx, SSL_OP_NO_SSLv2);
  47. /* Disable SSLv3 */
  48. SSL_CTX_set_options(profile->ssl_ctx, SSL_OP_NO_SSLv3);
  49. /* Disable TLSv1 */
  50. SSL_CTX_set_options(profile->ssl_ctx, SSL_OP_NO_TLSv1);
  51. /* Disable Compression CRIME (Compression Ratio Info-leak Made Easy) */
  52. SSL_CTX_set_options(profile->ssl_ctx, SSL_OP_NO_COMPRESSION);
  53. /* set the local certificate from CertFile */
  54. if (!ks_zstr(profile->chain)) {
  55. if (!SSL_CTX_use_certificate_chain_file(profile->ssl_ctx, profile->chain)) {
  56. err = "CERT CHAIN FILE ERROR";
  57. goto fail;
  58. }
  59. }
  60. if (!SSL_CTX_use_certificate_file(profile->ssl_ctx, profile->cert, SSL_FILETYPE_PEM)) {
  61. err = "CERT FILE ERROR";
  62. goto fail;
  63. }
  64. /* set the private key from KeyFile */
  65. if (!SSL_CTX_use_PrivateKey_file(profile->ssl_ctx, profile->key, SSL_FILETYPE_PEM)) {
  66. err = "PRIVATE KEY FILE ERROR";
  67. goto fail;
  68. }
  69. /* verify private key */
  70. if ( !SSL_CTX_check_private_key(profile->ssl_ctx) ) {
  71. err = "PRIVATE KEY FILE ERROR";
  72. goto fail;
  73. }
  74. SSL_CTX_set_cipher_list(profile->ssl_ctx, "HIGH:!DSS:!aNULL@STRENGTH");
  75. return 1;
  76. fail:
  77. ks_log(KS_LOG_ERROR, "SSL ERR: %s\n", err);
  78. return 0;
  79. }
  80. struct tcp_data {
  81. ks_socket_t sock;
  82. ks_sockaddr_t addr;
  83. int ready;
  84. char *ip;
  85. ks_pool_t *pool;
  86. int ssl;
  87. ssl_profile_t client_profile;
  88. ssl_profile_t server_profile;
  89. };
  90. void server_callback(ks_socket_t server_sock, ks_socket_t client_sock, ks_sockaddr_t *addr, void *user_data)
  91. {
  92. struct tcp_data *tcp_data = (struct tcp_data *) user_data;
  93. ks_size_t bytes;
  94. kws_t *kws = NULL;
  95. kws_opcode_t oc;
  96. uint8_t *data;
  97. int keepalive = 0;
  98. kws_request_t *request = NULL;
  99. if (tcp_data->ssl) {
  100. tcp_data->server_profile.ssl_method = SSLv23_server_method();
  101. ks_set_string(tcp_data->server_profile.cert, "./testhttp.pem");
  102. ks_set_string(tcp_data->server_profile.key, "./testhttp.pem");
  103. ks_set_string(tcp_data->server_profile.chain, "./testhttp.pem");
  104. init_ssl(&tcp_data->server_profile);
  105. }
  106. printf("WS %s SERVER SOCK %d connection from %s:%u\n", tcp_data->ssl ? "SSL" : "PLAIN", (int)server_sock, addr->host, addr->port);
  107. int flags = KWS_BLOCK | KWS_STAY_OPEN | KWS_HTTP;
  108. if (kws_init(&kws, client_sock, tcp_data->server_profile.ssl_ctx, NULL, flags, tcp_data->pool) != KS_STATUS_SUCCESS) {
  109. printf("WS SERVER CREATE FAIL\n");
  110. goto end;
  111. }
  112. if (!kws_test_flag(kws, KWS_HTTP)) {
  113. printf("Non HTTP Request\n");
  114. goto end;
  115. }
  116. new_req:
  117. if (kws_parse_header(kws, &request) != KS_STATUS_SUCCESS) {
  118. printf("Parse Header Error\n");
  119. goto end;
  120. }
  121. printf("request uri: %s\n", request->uri);
  122. kws_request_dump(request);
  123. if (!strncmp(request->method, "GET", 3)) {
  124. char data[512];
  125. ks_snprintf(data, sizeof(data),
  126. "HTTP/1.1 200 OK\r\n"
  127. "Content-Length: 0\r\n"
  128. "Content-Type: text/plain\r\n"
  129. "Allow: HEAD,GET,POST,PUT,DELETE,PATCH,OPTIONS\r\n"
  130. "Server: libks\r\n\r\n"
  131. "OK libks");
  132. kws_raw_write(kws, data, strlen(data));
  133. goto end;
  134. }
  135. if (request->content_length && request->content_length > 20) {
  136. char *data = "HTTP/1.1 413 Request Entity Too Large\r\n"
  137. "Content-Length: 0\r\n\r\n";
  138. kws_raw_write(kws, data, strlen(data));
  139. goto end;
  140. }
  141. if (!strncmp(request->method, "POST", 4) && request->content_length && request->content_type &&
  142. !strncmp(request->content_type, "application/x-www-form-urlencoded", 33)) {
  143. char buffer[1024];
  144. ks_ssize_t len = 0, bytes = 0;
  145. while(bytes < (ks_ssize_t)request->content_length) {
  146. len = request->content_length - bytes;
  147. #define WS_BLOCK 1
  148. if ((len = kws_raw_read(kws, buffer + bytes, len, WS_BLOCK)) < 0) {
  149. printf("Read error %d\n", (int)len);
  150. goto end;
  151. }
  152. bytes += len;
  153. }
  154. *(buffer + bytes) = '\0';
  155. kws_parse_qs(request, buffer);
  156. kws_request_dump(request);
  157. const char *a = kws_request_get_header(request, "a");
  158. const char *x = kws_request_get_header(request, "x");
  159. const char *k = a ? "a" : "x";
  160. int clen = a ? strlen(a) : strlen(x);
  161. char data[512];
  162. ks_snprintf(data, sizeof(data),
  163. "HTTP/1.1 200 OK\r\n"
  164. "Content-Length: %d\r\n"
  165. "Content-Type: text/plain\r\n"
  166. "Allow: HEAD,GET,POST,PUT,DELETE,PATCH,OPTIONS\r\n"
  167. "Server: libks\r\n\r\n"
  168. "%s=%s", clen, k, a ? a : x);
  169. kws_raw_write(kws, data, strlen(data));
  170. goto end;
  171. }
  172. if (!strncmp(request->method, "POST", 4) && request->content_length && request->content_type &&
  173. !strncmp(request->content_type, "application/json", 16)) {
  174. uint8_t *buffer;
  175. ks_ssize_t len = 1024;
  176. if ((len = kws_read_buffer(kws, &buffer, 1024, 1)) < 0) {
  177. printf("Read error %d\n", (int)len);
  178. goto end;
  179. }
  180. ks_assert(buffer);
  181. ks_json_t *json = ks_json_parse(buffer);
  182. ks_assert(json);
  183. char *s = ks_json_print(json);
  184. printf("Receved JSON: %s\n", s);
  185. char data[512];
  186. ks_snprintf(data, sizeof(data),
  187. "HTTP/1.1 200 OK\r\n"
  188. "Content-Length: %d\r\n"
  189. "Content-Type: text/plain\r\n"
  190. "Allow: HEAD,GET,POST,PUT,DELETE,PATCH,OPTIONS\r\n"
  191. "Server: libks\r\n\r\n"
  192. "%s", strlen(s), s);
  193. kws_raw_write(kws, data, strlen(data));
  194. goto end;
  195. }
  196. end:
  197. if (request) {
  198. keepalive = request->keepalive;
  199. printf("===================== free\n");
  200. kws_request_free(&request);
  201. }
  202. if (keepalive) {
  203. int pflags = kws_wait_sock(kws, 1000, KS_POLL_READ);
  204. if (pflags > 0 && (pflags & KS_POLL_READ)) {
  205. if (kws_keepalive(kws) == KS_STATUS_SUCCESS) {
  206. printf("socket is going to handle a new request\n");
  207. goto new_req;
  208. }
  209. }
  210. }
  211. ks_socket_close(&client_sock);
  212. kws_destroy(&kws);
  213. if (tcp_data->ssl) {
  214. SSL_CTX_free(tcp_data->server_profile.ssl_ctx);
  215. }
  216. printf("WS SERVER COMPLETE\n");
  217. }
  218. static void *tcp_sock_server(ks_thread_t *thread, void *thread_data)
  219. {
  220. struct tcp_data *tcp_data = (struct tcp_data *) thread_data;
  221. tcp_data->ready = 1;
  222. ks_listen_sock(tcp_data->sock, &tcp_data->addr, 0, server_callback, tcp_data);
  223. printf("WS THREAD DONE\n");
  224. return NULL;
  225. }
  226. static int test_get(char *ip, int ssl)
  227. {
  228. ks_thread_t *thread_p = NULL;
  229. ks_pool_t *pool;
  230. ks_sockaddr_t addr;
  231. int family = AF_INET;
  232. ks_socket_t cl_sock = KS_SOCK_INVALID;
  233. struct tcp_data tcp_data = { 0 };
  234. int r = 1, sanity = 100;
  235. kws_t *kws = NULL;
  236. ks_pool_open(&pool);
  237. tcp_data.pool = pool;
  238. if (ssl) {
  239. tcp_data.ssl = 1;
  240. tcp_data.client_profile.ssl_method = SSLv23_client_method();
  241. ks_set_string(tcp_data.client_profile.cert, "./testhttp.pem");
  242. ks_set_string(tcp_data.client_profile.key, "./testhttp.pem");
  243. ks_set_string(tcp_data.client_profile.chain, "./testhttp.pem");
  244. init_ssl(&tcp_data.client_profile);
  245. }
  246. if (strchr(ip, ':')) {
  247. family = AF_INET6;
  248. }
  249. if (ks_addr_set(&tcp_data.addr, ip, tcp_port, family) != KS_STATUS_SUCCESS) {
  250. r = 0;
  251. printf("WS CLIENT Can't set ADDR\n");
  252. goto end;
  253. }
  254. if ((tcp_data.sock = socket(family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) {
  255. r = 0;
  256. printf("WS CLIENT Can't create sock family %d\n", family);
  257. goto end;
  258. }
  259. ks_socket_option(tcp_data.sock, SO_REUSEADDR, KS_TRUE);
  260. ks_socket_option(tcp_data.sock, TCP_NODELAY, KS_TRUE);
  261. tcp_data.ip = ip;
  262. ks_thread_create(&thread_p, tcp_sock_server, &tcp_data, pool);
  263. while(!tcp_data.ready && --sanity > 0) {
  264. ks_sleep(10000);
  265. }
  266. ks_addr_set(&addr, ip, tcp_port, family);
  267. cl_sock = ks_socket_connect(SOCK_STREAM, IPPROTO_TCP, &addr);
  268. printf("WS %s CLIENT SOCKET %d %s %d\n", ssl ? "SSL" : "PLAIN", (int)cl_sock, addr.host, addr.port);
  269. char data[512];
  270. ks_snprintf(data, sizeof(data),
  271. "GET / HTTP/1.1\r\n"
  272. "HOST: localhost\r\n\r\n");
  273. ks_size_t len = strlen(data);
  274. ks_socket_send(cl_sock, data, &len);
  275. ks_sleep_ms(200);
  276. char buffer[1024];
  277. len = sizeof(buffer);
  278. ks_socket_recv(cl_sock, buffer, &len);
  279. if (len < 100) {
  280. r = 0;
  281. printf("ERROR read\n");
  282. }
  283. printf("HTTP GET CLIENT READ %ld bytes [%s]\n", (long)len, buffer);
  284. if (strstr(buffer, "libks") == NULL) {
  285. r = 0;
  286. }
  287. end:
  288. kws_destroy(&kws);
  289. if (ssl) {
  290. SSL_CTX_free(tcp_data.client_profile.ssl_ctx);
  291. }
  292. if (tcp_data.sock != KS_SOCK_INVALID) {
  293. ks_socket_shutdown(tcp_data.sock, 2);
  294. ks_socket_close(&tcp_data.sock);
  295. }
  296. if (thread_p) {
  297. ks_thread_join(thread_p);
  298. }
  299. ks_socket_close(&cl_sock);
  300. ks_pool_close(&pool);
  301. return r;
  302. }
  303. static int test_post(char *ip)
  304. {
  305. ks_thread_t *thread_p = NULL;
  306. ks_pool_t *pool;
  307. ks_sockaddr_t addr;
  308. int family = AF_INET;
  309. ks_socket_t cl_sock = KS_SOCK_INVALID;
  310. struct tcp_data tcp_data = { 0 };
  311. int r = 1, sanity = 100;
  312. kws_t *kws = NULL;
  313. ks_pool_open(&pool);
  314. tcp_data.pool = pool;
  315. if (strchr(ip, ':')) {
  316. family = AF_INET6;
  317. }
  318. if (ks_addr_set(&tcp_data.addr, ip, tcp_port, family) != KS_STATUS_SUCCESS) {
  319. r = 0;
  320. printf("WS CLIENT Can't set ADDR\n");
  321. goto end;
  322. }
  323. if ((tcp_data.sock = socket(family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) {
  324. r = 0;
  325. printf("WS CLIENT Can't create sock family %d\n", family);
  326. goto end;
  327. }
  328. ks_socket_option(tcp_data.sock, SO_REUSEADDR, KS_TRUE);
  329. ks_socket_option(tcp_data.sock, TCP_NODELAY, KS_TRUE);
  330. tcp_data.ip = ip;
  331. ks_thread_create(&thread_p, tcp_sock_server, &tcp_data, pool);
  332. while(!tcp_data.ready && --sanity > 0) {
  333. ks_sleep(10000);
  334. }
  335. ks_addr_set(&addr, ip, tcp_port, family);
  336. cl_sock = ks_socket_connect(SOCK_STREAM, IPPROTO_TCP, &addr);
  337. printf("HTTP CLIENT SOCKET %d %s %d\n", (int)cl_sock, addr.host, addr.port);
  338. char data[512];
  339. char buffer[1024];
  340. const char *post_data = "a=1&b=2";
  341. ks_snprintf(data, sizeof(data),
  342. "POST /post HTTP/1.1\r\n"
  343. "Content-Length: %d\r\n"
  344. "Content-Type: application/x-www-form-urlencoded\r\n"
  345. "HOST: localhost\r\n\r\n"
  346. "%s", strlen(post_data), post_data);
  347. ks_size_t len = strlen(data);
  348. ks_socket_send(cl_sock, data, &len);
  349. ks_sleep_ms(200);
  350. len = sizeof(buffer);
  351. ks_socket_recv(cl_sock, buffer, &len);
  352. if (len < 100) {
  353. r = 0;
  354. printf("ERROR read\n");
  355. }
  356. printf("HTTP POST CLIENT READ %ld bytes [%s]\n", (long)len, buffer);
  357. if (strstr(buffer, "a=1") == NULL) {
  358. r = 0;
  359. }
  360. end:
  361. kws_destroy(&kws);
  362. if (tcp_data.sock != KS_SOCK_INVALID) {
  363. ks_socket_shutdown(tcp_data.sock, 2);
  364. ks_socket_close(&tcp_data.sock);
  365. }
  366. if (thread_p) {
  367. ks_thread_join(thread_p);
  368. }
  369. ks_socket_close(&cl_sock);
  370. ks_pool_close(&pool);
  371. return r;
  372. }
  373. static int test_post_json_read_buffer(char *ip)
  374. {
  375. ks_thread_t *thread_p = NULL;
  376. ks_pool_t *pool;
  377. ks_sockaddr_t addr;
  378. int family = AF_INET;
  379. ks_socket_t cl_sock = KS_SOCK_INVALID;
  380. struct tcp_data tcp_data = { 0 };
  381. int r = 1, sanity = 100;
  382. kws_t *kws = NULL;
  383. ks_pool_open(&pool);
  384. tcp_data.pool = pool;
  385. if (strchr(ip, ':')) {
  386. family = AF_INET6;
  387. }
  388. if (ks_addr_set(&tcp_data.addr, ip, tcp_port, family) != KS_STATUS_SUCCESS) {
  389. r = 0;
  390. printf("WS CLIENT Can't set ADDR\n");
  391. goto end;
  392. }
  393. if ((tcp_data.sock = socket(family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) {
  394. r = 0;
  395. printf("WS CLIENT Can't create sock family %d\n", family);
  396. goto end;
  397. }
  398. ks_socket_option(tcp_data.sock, SO_REUSEADDR, KS_TRUE);
  399. ks_socket_option(tcp_data.sock, TCP_NODELAY, KS_TRUE);
  400. tcp_data.ip = ip;
  401. ks_thread_create(&thread_p, tcp_sock_server, &tcp_data, pool);
  402. while(!tcp_data.ready && --sanity > 0) {
  403. ks_sleep(10000);
  404. }
  405. ks_addr_set(&addr, ip, tcp_port, family);
  406. cl_sock = ks_socket_connect(SOCK_STREAM, IPPROTO_TCP, &addr);
  407. printf("HTTP CLIENT SOCKET %d %s %d\n", (int)cl_sock, addr.host, addr.port);
  408. char data[512];
  409. char buffer[1024];
  410. const char *post_data = "{\"json\": true}";
  411. ks_snprintf(data, sizeof(data),
  412. "POST /json HTTP/1.1\r\n"
  413. "Content-Length: %d\r\n"
  414. "Content-Type: application/json\r\n"
  415. "HOST: localhost\r\n\r\n"
  416. "%s", strlen(post_data), post_data);
  417. ks_size_t len = strlen(data);
  418. ks_socket_send(cl_sock, data, &len);
  419. ks_sleep_ms(200);
  420. len = sizeof(buffer);
  421. ks_socket_recv(cl_sock, buffer, &len);
  422. if (len < 100) {
  423. r = 0;
  424. printf("ERROR read\n");
  425. }
  426. printf("HTTP POST CLIENT READ %ld bytes [%s]\n", (long)len, buffer);
  427. if (strstr(buffer, "json") == NULL) {
  428. r = 0;
  429. }
  430. end:
  431. kws_destroy(&kws);
  432. if (tcp_data.sock != KS_SOCK_INVALID) {
  433. ks_socket_shutdown(tcp_data.sock, 2);
  434. ks_socket_close(&tcp_data.sock);
  435. }
  436. if (thread_p) {
  437. ks_thread_join(thread_p);
  438. }
  439. ks_socket_close(&cl_sock);
  440. ks_pool_close(&pool);
  441. return r;
  442. }
  443. static int test_keepalive(char *ip)
  444. {
  445. ks_thread_t *thread_p = NULL;
  446. ks_pool_t *pool;
  447. ks_sockaddr_t addr;
  448. int family = AF_INET;
  449. ks_socket_t cl_sock = KS_SOCK_INVALID;
  450. struct tcp_data tcp_data = { 0 };
  451. int r = 1, sanity = 100;
  452. kws_t *kws = NULL;
  453. ks_pool_open(&pool);
  454. tcp_data.pool = pool;
  455. if (strchr(ip, ':')) {
  456. family = AF_INET6;
  457. }
  458. if (ks_addr_set(&tcp_data.addr, ip, tcp_port, family) != KS_STATUS_SUCCESS) {
  459. r = 0;
  460. printf("WS CLIENT Can't set ADDR\n");
  461. goto end;
  462. }
  463. if ((tcp_data.sock = socket(family, SOCK_STREAM, IPPROTO_TCP)) == KS_SOCK_INVALID) {
  464. r = 0;
  465. printf("WS CLIENT Can't create sock family %d\n", family);
  466. goto end;
  467. }
  468. ks_socket_option(tcp_data.sock, SO_REUSEADDR, KS_TRUE);
  469. ks_socket_option(tcp_data.sock, TCP_NODELAY, KS_TRUE);
  470. tcp_data.ip = ip;
  471. ks_thread_create(&thread_p, tcp_sock_server, &tcp_data, pool);
  472. while(!tcp_data.ready && --sanity > 0) {
  473. ks_sleep(10000);
  474. }
  475. ks_addr_set(&addr, ip, tcp_port, family);
  476. cl_sock = ks_socket_connect(SOCK_STREAM, IPPROTO_TCP, &addr);
  477. printf("HTTP CLIENT SOCKET %d %s %d\n", (int)cl_sock, addr.host, addr.port);
  478. char data[512];
  479. char buffer[1024];
  480. const char *post_data = "a=1&b=2";
  481. ks_snprintf(data, sizeof(data),
  482. "POST /post HTTP/1.1\r\n"
  483. "Content-Length: %d\r\n"
  484. "Content-Type: application/x-www-form-urlencoded\r\n"
  485. "HOST: localhost\r\n\r\n"
  486. "%s", strlen(post_data), post_data);
  487. ks_size_t len = strlen(data);
  488. ks_socket_send(cl_sock, data, &len);
  489. ks_sleep_ms(200);
  490. len = sizeof(buffer);
  491. ks_socket_recv(cl_sock, buffer, &len);
  492. if (len < 100) {
  493. r = 0;
  494. printf("ERROR read\n");
  495. }
  496. printf("HTTP POST CLIENT READ %ld bytes [%s]\n", (long)len, buffer);
  497. if (strstr(buffer, "a=1") == NULL) {
  498. r = 0;
  499. }
  500. post_data = "x=1&y=2";
  501. ks_snprintf(data, sizeof(data),
  502. "POST /post HTTP/1.1\r\n"
  503. "Content-Length: %d\r\n"
  504. "Content-Type: application/x-www-form-urlencoded\r\n"
  505. "HOST: localhost\r\n\r\n"
  506. "%s", strlen(post_data), post_data);
  507. len = strlen(data);
  508. ks_socket_send(cl_sock, data, &len);
  509. ks_sleep_ms(200);
  510. len = sizeof(buffer);
  511. ks_socket_recv(cl_sock, buffer, &len);
  512. if (len < 100) {
  513. r = 0;
  514. printf("ERROR read\n");
  515. }
  516. printf("HTTP POST CLIENT READ %ld bytes [%s]\n", (long)len, buffer);
  517. if (strstr(buffer, "x=1") == NULL) {
  518. r = 0;
  519. }
  520. end:
  521. kws_destroy(&kws);
  522. if (tcp_data.sock != KS_SOCK_INVALID) {
  523. ks_socket_shutdown(tcp_data.sock, 2);
  524. ks_socket_close(&tcp_data.sock);
  525. }
  526. if (thread_p) {
  527. ks_thread_join(thread_p);
  528. }
  529. ks_socket_close(&cl_sock);
  530. ks_pool_close(&pool);
  531. return r;
  532. }
  533. int main(void)
  534. {
  535. int have_v4 = 0, have_v6 = 0;
  536. ks_find_local_ip(v4, sizeof(v4), &mask, AF_INET, NULL);
  537. ks_find_local_ip(v6, sizeof(v6), NULL, AF_INET6, NULL);
  538. ks_init();
  539. printf("IPS: v4: [%s] v6: [%s]\n", v4, v6);
  540. have_v4 = ks_zstr_buf(v4) ? 0 : 1;
  541. // have_v6 = ks_zstr_buf(v6) ? 0 : 1;
  542. plan((have_v4 * 5) + (have_v6 * 4) + 1);
  543. ok(have_v4 || have_v6);
  544. if (have_v4 || have_v6) {
  545. ks_gen_cert(".", "testhttp.pem");
  546. }
  547. if (have_v4) {
  548. ok(test_get(v4, 0));
  549. ok(test_get(v4, 1));
  550. ok(test_post(v4));
  551. ok(test_keepalive(v4));
  552. ok(test_post_json_read_buffer(v4));
  553. }
  554. if (have_v6) {
  555. ok(test_get(v6, 0));
  556. ok(test_get(v6, 1));
  557. ok(test_post(v6));
  558. ok(test_keepalive(v6));
  559. }
  560. unlink("./testhttp.pem");
  561. ks_shutdown();
  562. done_testing();
  563. }