minihttptestserver.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656
  1. /* $Id: minihttptestserver.c,v 1.18 2015/07/15 12:41:15 nanard Exp $ */
  2. /* Project : miniUPnP
  3. * Author : Thomas Bernard
  4. * Copyright (c) 2011-2015 Thomas Bernard
  5. * This software is subject to the conditions detailed in the
  6. * LICENCE file provided in this distribution.
  7. * */
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <unistd.h>
  12. #include <sys/types.h>
  13. #include <sys/socket.h>
  14. #include <sys/wait.h>
  15. #include <arpa/inet.h>
  16. #include <netinet/in.h>
  17. #include <signal.h>
  18. #include <time.h>
  19. #include <errno.h>
  20. #define CRAP_LENGTH (2048)
  21. volatile sig_atomic_t quit = 0;
  22. volatile sig_atomic_t child_to_wait_for = 0;
  23. /**
  24. * signal handler for SIGCHLD (child status has changed)
  25. */
  26. void handle_signal_chld(int sig)
  27. {
  28. (void)sig;
  29. /* printf("handle_signal_chld(%d)\n", sig); */
  30. ++child_to_wait_for;
  31. }
  32. /**
  33. * signal handler for SIGINT (CRTL C)
  34. */
  35. void handle_signal_int(int sig)
  36. {
  37. (void)sig;
  38. /* printf("handle_signal_int(%d)\n", sig); */
  39. quit = 1;
  40. }
  41. /**
  42. * build a text/plain content of the specified length
  43. */
  44. void build_content(char * p, int n)
  45. {
  46. char line_buffer[80];
  47. int k;
  48. int i = 0;
  49. while(n > 0) {
  50. k = snprintf(line_buffer, sizeof(line_buffer),
  51. "%04d_ABCDEFGHIJKL_This_line_is_64_bytes_long_ABCDEFGHIJKL_%04d\r\n",
  52. i, i);
  53. if(k != 64) {
  54. fprintf(stderr, "snprintf() returned %d in build_content()\n", k);
  55. }
  56. ++i;
  57. if(n >= 64) {
  58. memcpy(p, line_buffer, 64);
  59. p += 64;
  60. n -= 64;
  61. } else {
  62. memcpy(p, line_buffer, n);
  63. p += n;
  64. n = 0;
  65. }
  66. }
  67. }
  68. /**
  69. * build crappy content
  70. */
  71. void build_crap(char * p, int n)
  72. {
  73. static const char crap[] = "_CRAP_\r\n";
  74. int i;
  75. while(n > 0) {
  76. i = sizeof(crap) - 1;
  77. if(i > n)
  78. i = n;
  79. memcpy(p, crap, i);
  80. p += i;
  81. n -= i;
  82. }
  83. }
  84. /**
  85. * build chunked response.
  86. * return a malloc'ed buffer
  87. */
  88. char * build_chunked_response(int content_length, int * response_len)
  89. {
  90. char * response_buffer;
  91. char * content_buffer;
  92. int buffer_length;
  93. int i, n;
  94. /* allocate to have some margin */
  95. buffer_length = 256 + content_length + (content_length >> 4);
  96. response_buffer = malloc(buffer_length);
  97. if(response_buffer == NULL)
  98. return NULL;
  99. *response_len = snprintf(response_buffer, buffer_length,
  100. "HTTP/1.1 200 OK\r\n"
  101. "Content-Type: text/plain\r\n"
  102. "Transfer-Encoding: chunked\r\n"
  103. "\r\n");
  104. /* build the content */
  105. content_buffer = malloc(content_length);
  106. if(content_buffer == NULL) {
  107. free(response_buffer);
  108. return NULL;
  109. }
  110. build_content(content_buffer, content_length);
  111. /* chunk it */
  112. i = 0;
  113. while(i < content_length) {
  114. n = (rand() % 199) + 1;
  115. if(i + n > content_length) {
  116. n = content_length - i;
  117. }
  118. /* TODO : check buffer size ! */
  119. *response_len += snprintf(response_buffer + *response_len,
  120. buffer_length - *response_len,
  121. "%x\r\n", n);
  122. memcpy(response_buffer + *response_len, content_buffer + i, n);
  123. *response_len += n;
  124. i += n;
  125. response_buffer[(*response_len)++] = '\r';
  126. response_buffer[(*response_len)++] = '\n';
  127. }
  128. /* the last chunk : "0\r\n" a empty body and then
  129. * the final "\r\n" */
  130. memcpy(response_buffer + *response_len, "0\r\n\r\n", 5);
  131. *response_len += 5;
  132. free(content_buffer);
  133. printf("resp_length=%d buffer_length=%d content_length=%d\n",
  134. *response_len, buffer_length, content_length);
  135. return response_buffer;
  136. }
  137. /* favicon.ico generator */
  138. #ifdef OLD_HEADER
  139. #define FAVICON_LENGTH (6 + 16 + 12 + 8 + 32 * 4)
  140. #else
  141. #define FAVICON_LENGTH (6 + 16 + 40 + 8 + 32 * 4)
  142. #endif
  143. void build_favicon_content(char * p, int n)
  144. {
  145. int i;
  146. if(n < FAVICON_LENGTH)
  147. return;
  148. /* header : 6 bytes */
  149. *p++ = 0;
  150. *p++ = 0;
  151. *p++ = 1; /* type : ICO */
  152. *p++ = 0;
  153. *p++ = 1; /* number of images in file */
  154. *p++ = 0;
  155. /* image directory (1 entry) : 16 bytes */
  156. *p++ = 16; /* width */
  157. *p++ = 16; /* height */
  158. *p++ = 2; /* number of colors in the palette. 0 = no palette */
  159. *p++ = 0; /* reserved */
  160. *p++ = 1; /* color planes */
  161. *p++ = 0; /* " */
  162. *p++ = 1; /* bpp */
  163. *p++ = 0; /* " */
  164. #ifdef OLD_HEADER
  165. *p++ = 12 + 8 + 32 * 4; /* bmp size */
  166. #else
  167. *p++ = 40 + 8 + 32 * 4; /* bmp size */
  168. #endif
  169. *p++ = 0; /* " */
  170. *p++ = 0; /* " */
  171. *p++ = 0; /* " */
  172. *p++ = 6 + 16; /* bmp offset */
  173. *p++ = 0; /* " */
  174. *p++ = 0; /* " */
  175. *p++ = 0; /* " */
  176. /* BMP */
  177. #ifdef OLD_HEADER
  178. /* BITMAPCOREHEADER */
  179. *p++ = 12; /* size of this header */
  180. *p++ = 0; /* " */
  181. *p++ = 0; /* " */
  182. *p++ = 0; /* " */
  183. *p++ = 16; /* width */
  184. *p++ = 0; /* " */
  185. *p++ = 16 * 2; /* height x 2 ! */
  186. *p++ = 0; /* " */
  187. *p++ = 1; /* color planes */
  188. *p++ = 0; /* " */
  189. *p++ = 1; /* bpp */
  190. *p++ = 0; /* " */
  191. #else
  192. /* BITMAPINFOHEADER */
  193. *p++ = 40; /* size of this header */
  194. *p++ = 0; /* " */
  195. *p++ = 0; /* " */
  196. *p++ = 0; /* " */
  197. *p++ = 16; /* width */
  198. *p++ = 0; /* " */
  199. *p++ = 0; /* " */
  200. *p++ = 0; /* " */
  201. *p++ = 16 * 2; /* height x 2 ! */
  202. *p++ = 0; /* " */
  203. *p++ = 0; /* " */
  204. *p++ = 0; /* " */
  205. *p++ = 1; /* color planes */
  206. *p++ = 0; /* " */
  207. *p++ = 1; /* bpp */
  208. *p++ = 0; /* " */
  209. /* compression method, image size, ppm x, ppm y */
  210. /* colors in the palette ? */
  211. /* important colors */
  212. for(i = 4 * 6; i > 0; --i)
  213. *p++ = 0;
  214. #endif
  215. /* palette */
  216. *p++ = 0; /* b */
  217. *p++ = 0; /* g */
  218. *p++ = 0; /* r */
  219. *p++ = 0; /* reserved */
  220. *p++ = 255; /* b */
  221. *p++ = 255; /* g */
  222. *p++ = 255; /* r */
  223. *p++ = 0; /* reserved */
  224. /* pixel data */
  225. for(i = 16; i > 0; --i) {
  226. if(i & 1) {
  227. *p++ = 0125;
  228. *p++ = 0125;
  229. } else {
  230. *p++ = 0252;
  231. *p++ = 0252;
  232. }
  233. *p++ = 0;
  234. *p++ = 0;
  235. }
  236. /* Opacity MASK */
  237. for(i = 16 * 4; i > 0; --i) {
  238. *p++ = 0;
  239. }
  240. }
  241. enum modes {
  242. MODE_INVALID, MODE_CHUNKED, MODE_ADDCRAP, MODE_NORMAL, MODE_FAVICON
  243. };
  244. const struct {
  245. const enum modes mode;
  246. const char * text;
  247. } modes_array[] = {
  248. {MODE_CHUNKED, "chunked"},
  249. {MODE_ADDCRAP, "addcrap"},
  250. {MODE_NORMAL, "normal"},
  251. {MODE_FAVICON, "favicon.ico"},
  252. {MODE_INVALID, NULL}
  253. };
  254. /**
  255. * write the response with random behaviour !
  256. */
  257. void send_response(int c, const char * buffer, int len)
  258. {
  259. int n;
  260. while(len > 0) {
  261. n = (rand() % 99) + 1;
  262. if(n > len)
  263. n = len;
  264. n = write(c, buffer, n);
  265. if(n < 0) {
  266. if(errno != EINTR) {
  267. perror("write");
  268. return;
  269. }
  270. /* if errno == EINTR, try again */
  271. } else {
  272. len -= n;
  273. buffer += n;
  274. }
  275. usleep(10000); /* 10ms */
  276. }
  277. }
  278. /**
  279. * handle the HTTP connection
  280. */
  281. void handle_http_connection(int c)
  282. {
  283. char request_buffer[2048];
  284. int request_len = 0;
  285. int headers_found = 0;
  286. int n, i;
  287. char request_method[16];
  288. char request_uri[256];
  289. char http_version[16];
  290. char * p;
  291. char * response_buffer;
  292. int response_len;
  293. enum modes mode;
  294. int content_length = 16*1024;
  295. /* read the request */
  296. while(request_len < (int)sizeof(request_buffer) && !headers_found) {
  297. n = read(c,
  298. request_buffer + request_len,
  299. sizeof(request_buffer) - request_len);
  300. if(n < 0) {
  301. if(errno == EINTR)
  302. continue;
  303. perror("read");
  304. return;
  305. } else if(n==0) {
  306. /* remote host closed the connection */
  307. break;
  308. } else {
  309. request_len += n;
  310. for(i = 0; i < request_len - 3; i++) {
  311. if(0 == memcmp(request_buffer + i, "\r\n\r\n", 4)) {
  312. /* found the end of headers */
  313. headers_found = 1;
  314. break;
  315. }
  316. }
  317. }
  318. }
  319. if(!headers_found) {
  320. /* error */
  321. printf("no HTTP header found in the request\n");
  322. return;
  323. }
  324. printf("headers :\n%.*s", request_len, request_buffer);
  325. /* the request have been received, now parse the request line */
  326. p = request_buffer;
  327. for(i = 0; i < (int)sizeof(request_method) - 1; i++) {
  328. if(*p == ' ' || *p == '\r')
  329. break;
  330. request_method[i] = *p;
  331. ++p;
  332. }
  333. request_method[i] = '\0';
  334. while(*p == ' ')
  335. p++;
  336. for(i = 0; i < (int)sizeof(request_uri) - 1; i++) {
  337. if(*p == ' ' || *p == '\r')
  338. break;
  339. request_uri[i] = *p;
  340. ++p;
  341. }
  342. request_uri[i] = '\0';
  343. while(*p == ' ')
  344. p++;
  345. for(i = 0; i < (int)sizeof(http_version) - 1; i++) {
  346. if(*p == ' ' || *p == '\r')
  347. break;
  348. http_version[i] = *p;
  349. ++p;
  350. }
  351. http_version[i] = '\0';
  352. printf("Method = %s, URI = %s, %s\n",
  353. request_method, request_uri, http_version);
  354. /* check if the request method is allowed */
  355. if(0 != strcmp(request_method, "GET")) {
  356. const char response405[] = "HTTP/1.1 405 Method Not Allowed\r\n"
  357. "Allow: GET\r\n\r\n";
  358. const char * pc;
  359. /* 405 Method Not Allowed */
  360. /* The response MUST include an Allow header containing a list
  361. * of valid methods for the requested resource. */
  362. n = sizeof(response405) - 1;
  363. pc = response405;
  364. while(n > 0) {
  365. i = write(c, pc, n);
  366. if(i<0) {
  367. if(errno != EINTR) {
  368. perror("write");
  369. return;
  370. }
  371. } else {
  372. n -= i;
  373. pc += i;
  374. }
  375. }
  376. return;
  377. }
  378. mode = MODE_INVALID;
  379. /* use the request URI to know what to do */
  380. for(i = 0; modes_array[i].mode != MODE_INVALID; i++) {
  381. if(strstr(request_uri, modes_array[i].text)) {
  382. mode = modes_array[i].mode; /* found */
  383. break;
  384. }
  385. }
  386. switch(mode) {
  387. case MODE_CHUNKED:
  388. response_buffer = build_chunked_response(content_length, &response_len);
  389. break;
  390. case MODE_ADDCRAP:
  391. response_len = content_length+256;
  392. response_buffer = malloc(response_len);
  393. if(!response_buffer)
  394. break;
  395. n = snprintf(response_buffer, response_len,
  396. "HTTP/1.1 200 OK\r\n"
  397. "Server: minihttptestserver\r\n"
  398. "Content-Type: text/plain\r\n"
  399. "Content-Length: %d\r\n"
  400. "\r\n", content_length);
  401. response_len = content_length+n+CRAP_LENGTH;
  402. p = realloc(response_buffer, response_len);
  403. if(p == NULL) {
  404. /* error 500 */
  405. free(response_buffer);
  406. response_buffer = NULL;
  407. break;
  408. }
  409. response_buffer = p;
  410. build_content(response_buffer + n, content_length);
  411. build_crap(response_buffer + n + content_length, CRAP_LENGTH);
  412. break;
  413. case MODE_FAVICON:
  414. content_length = FAVICON_LENGTH;
  415. response_len = content_length + 256;
  416. response_buffer = malloc(response_len);
  417. if(!response_buffer)
  418. break;
  419. n = snprintf(response_buffer, response_len,
  420. "HTTP/1.1 200 OK\r\n"
  421. "Server: minihttptestserver\r\n"
  422. "Content-Type: image/vnd.microsoft.icon\r\n"
  423. "Content-Length: %d\r\n"
  424. "\r\n", content_length);
  425. /* image/x-icon */
  426. build_favicon_content(response_buffer + n, content_length);
  427. response_len = content_length + n;
  428. break;
  429. default:
  430. response_len = content_length+256;
  431. response_buffer = malloc(response_len);
  432. if(!response_buffer)
  433. break;
  434. n = snprintf(response_buffer, response_len,
  435. "HTTP/1.1 200 OK\r\n"
  436. "Server: minihttptestserver\r\n"
  437. "Content-Type: text/plain\r\n"
  438. "\r\n");
  439. response_len = content_length+n;
  440. p = realloc(response_buffer, response_len);
  441. if(p == NULL) {
  442. /* Error 500 */
  443. free(response_buffer);
  444. response_buffer = NULL;
  445. break;
  446. }
  447. response_buffer = p;
  448. build_content(response_buffer + n, response_len - n);
  449. }
  450. if(response_buffer) {
  451. send_response(c, response_buffer, response_len);
  452. free(response_buffer);
  453. } else {
  454. /* Error 500 */
  455. }
  456. }
  457. /**
  458. */
  459. int main(int argc, char * * argv) {
  460. int ipv6 = 0;
  461. int s, c, i;
  462. unsigned short port = 0;
  463. struct sockaddr_storage server_addr;
  464. socklen_t server_addrlen;
  465. struct sockaddr_storage client_addr;
  466. socklen_t client_addrlen;
  467. pid_t pid;
  468. int child = 0;
  469. int status;
  470. const char * expected_file_name = NULL;
  471. struct sigaction sa;
  472. for(i = 1; i < argc; i++) {
  473. if(argv[i][0] == '-') {
  474. switch(argv[i][1]) {
  475. case '6':
  476. ipv6 = 1;
  477. break;
  478. case 'e':
  479. /* write expected file ! */
  480. expected_file_name = argv[++i];
  481. break;
  482. case 'p':
  483. /* port */
  484. if(++i < argc) {
  485. port = (unsigned short)atoi(argv[i]);
  486. }
  487. break;
  488. default:
  489. fprintf(stderr, "unknown command line switch '%s'\n", argv[i]);
  490. }
  491. } else {
  492. fprintf(stderr, "unkown command line argument '%s'\n", argv[i]);
  493. }
  494. }
  495. srand(time(NULL));
  496. memset(&sa, 0, sizeof(struct sigaction));
  497. /*signal(SIGCHLD, handle_signal_chld);*/
  498. sa.sa_handler = handle_signal_chld;
  499. if(sigaction(SIGCHLD, &sa, NULL) < 0) {
  500. perror("sigaction");
  501. return 1;
  502. }
  503. /*signal(SIGINT, handle_signal_int);*/
  504. sa.sa_handler = handle_signal_int;
  505. if(sigaction(SIGINT, &sa, NULL) < 0) {
  506. perror("sigaction");
  507. return 1;
  508. }
  509. s = socket(ipv6 ? AF_INET6 : AF_INET, SOCK_STREAM, 0);
  510. if(s < 0) {
  511. perror("socket");
  512. return 1;
  513. }
  514. memset(&server_addr, 0, sizeof(struct sockaddr_storage));
  515. memset(&client_addr, 0, sizeof(struct sockaddr_storage));
  516. if(ipv6) {
  517. struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
  518. addr->sin6_family = AF_INET6;
  519. addr->sin6_port = htons(port);
  520. addr->sin6_addr = in6addr_loopback;
  521. } else {
  522. struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
  523. addr->sin_family = AF_INET;
  524. addr->sin_port = htons(port);
  525. addr->sin_addr.s_addr = htonl(INADDR_LOOPBACK);
  526. }
  527. if(bind(s, (struct sockaddr *)&server_addr,
  528. ipv6 ? sizeof(struct sockaddr_in6) : sizeof(struct sockaddr_in)) < 0) {
  529. perror("bind");
  530. return 1;
  531. }
  532. if(listen(s, 5) < 0) {
  533. perror("listen");
  534. }
  535. if(port == 0) {
  536. server_addrlen = sizeof(struct sockaddr_storage);
  537. if(getsockname(s, (struct sockaddr *)&server_addr, &server_addrlen) < 0) {
  538. perror("getsockname");
  539. return 1;
  540. }
  541. if(ipv6) {
  542. struct sockaddr_in6 * addr = (struct sockaddr_in6 *)&server_addr;
  543. port = ntohs(addr->sin6_port);
  544. } else {
  545. struct sockaddr_in * addr = (struct sockaddr_in *)&server_addr;
  546. port = ntohs(addr->sin_port);
  547. }
  548. printf("Listening on port %hu\n", port);
  549. fflush(stdout);
  550. }
  551. /* write expected file */
  552. if(expected_file_name) {
  553. FILE * f;
  554. f = fopen(expected_file_name, "wb");
  555. if(f) {
  556. char * buffer;
  557. buffer = malloc(16*1024);
  558. if(buffer == NULL) {
  559. fprintf(stderr, "memory allocation error\n");
  560. } else {
  561. build_content(buffer, 16*1024);
  562. i = fwrite(buffer, 1, 16*1024, f);
  563. if(i != 16*1024) {
  564. fprintf(stderr, "error writing to file %s : %dbytes written (out of %d)\n", expected_file_name, i, 16*1024);
  565. }
  566. free(buffer);
  567. }
  568. fclose(f);
  569. } else {
  570. fprintf(stderr, "error opening file %s for writing\n", expected_file_name);
  571. }
  572. }
  573. /* fork() loop */
  574. while(!child && !quit) {
  575. while(child_to_wait_for > 0) {
  576. pid = wait(&status);
  577. if(pid < 0) {
  578. perror("wait");
  579. } else {
  580. printf("child(%d) terminated with status %d\n", pid, status);
  581. }
  582. --child_to_wait_for;
  583. }
  584. client_addrlen = sizeof(struct sockaddr_storage);
  585. c = accept(s, (struct sockaddr *)&client_addr,
  586. &client_addrlen);
  587. if(c < 0) {
  588. if(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)
  589. continue;
  590. perror("accept");
  591. return 1;
  592. }
  593. printf("accept...\n");
  594. pid = fork();
  595. if(pid < 0) {
  596. perror("fork");
  597. return 1;
  598. } else if(pid == 0) {
  599. /* child */
  600. child = 1;
  601. close(s);
  602. s = -1;
  603. handle_http_connection(c);
  604. }
  605. close(c);
  606. }
  607. if(s >= 0) {
  608. close(s);
  609. s = -1;
  610. }
  611. if(!child) {
  612. while(child_to_wait_for > 0) {
  613. pid = wait(&status);
  614. if(pid < 0) {
  615. perror("wait");
  616. } else {
  617. printf("child(%d) terminated with status %d\n", pid, status);
  618. }
  619. --child_to_wait_for;
  620. }
  621. printf("Bye...\n");
  622. }
  623. return 0;
  624. }