minihttptestserver.c 15 KB

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