The little things give you away... A collection of various small helper stuff
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

340 lines
11 KiB

  1. #define _GNU_SOURCE
  2. #include <ctype.h>
  3. #include <stdbool.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #ifndef BUFSIZE
  8. #define BUFSIZE 1048576
  9. #endif
  10. #define STATE_HEADERS 0
  11. #define STATE_BODY 1 // Body with a Content-Length header
  12. #define STATE_CHUNK_LINE 2
  13. #define STATE_CHUNK_CONTENTS 3
  14. #ifdef DEBUG
  15. #define DEBUG_PRINTF(...) do { fprintf(stderr, __VA_ARGS__); } while (false)
  16. #else
  17. #define DEBUG_PRINTF(...) do {} while (false)
  18. #endif
  19. char* memcasemem(char* haystack, size_t haystacklen, char* needle, size_t needlelen) {
  20. // Case-insensitive (for ASCII) slower version of memmem
  21. // needle must already be in lower-case.
  22. if (needlelen > haystacklen) {
  23. // A needle longer than the haystack can never be in there.
  24. return NULL;
  25. }
  26. char* p1;
  27. char* p2;
  28. char* p3;
  29. for (p1 = haystack; p1 < haystack + haystacklen - needlelen; ++p1) {
  30. if (tolower((unsigned char)*p1) == *needle) {
  31. // Found a first char match, check the rest
  32. // No need to constrain p2; due to the needlelen>haystacklen check above and the limits on p1, p2 can never exceed the haystack.
  33. for (p2 = p1 + 1, p3 = needle + 1; p3 < needle + needlelen; p2++, p3++) {
  34. if (tolower((unsigned char)*p2) != *p3) {
  35. break;
  36. }
  37. }
  38. if (p3 == needle + needlelen) {
  39. // Full match
  40. return p1;
  41. }
  42. }
  43. }
  44. return NULL;
  45. }
  46. int main(int argc, char* argv[]) {
  47. // Read stdin, decode HTTP responses, dump all bodies to stdout.
  48. // stdin may contain an extra 'URL LENGTH\n' line before each response (--meta output from warc-dump-responses).
  49. // One LF is inserted at the end of each response to ensure that a new response always begins on a new line.
  50. // If --html-fake-base is provided and the input contains URL data, every HTML response (Content-Type: text/html header) is prefixed with one line containing a fake <base> tag: <base href="URL">. The line is terminated with a LF.
  51. // Headers and chunk lines must fit into BUFSIZE.
  52. // Does not fully comply with the HTTP spec. For example, continuation lines are unsupported.
  53. char buf[2 * BUFSIZE];
  54. size_t n;
  55. int state = STATE_HEADERS;
  56. char* bufp;
  57. char* m0;
  58. char* m1;
  59. char* eoh;
  60. long int nscan;
  61. size_t bytes_read;
  62. size_t length;
  63. bool html_fake_base = false;
  64. char* url = NULL; // Warning, pointer is only valid within the STATE_HEADERS block below.
  65. size_t urllen;
  66. if (argc == 2 && strcmp(argv[1], "--html-fake-base") == 0) {
  67. html_fake_base = true;
  68. }
  69. while ((n = fread(buf, 1, BUFSIZE, stdin)) > 0) {
  70. bufp = buf;
  71. checkstate:
  72. DEBUG_PRINTF("Have %zu bytes of buffer (at %p)\n", n, (void*)bufp);
  73. DEBUG_PRINTF("Beginning of buffer: ");
  74. for (int i = 0; i < (n > 64 ? 64 : n); ++i) DEBUG_PRINTF(isprint(*(bufp + i)) ? "%c" : "\\x%02x", *(bufp + i) & 0xFF);
  75. DEBUG_PRINTF("\n");
  76. if (n == 0) {
  77. break;
  78. }
  79. DEBUG_PRINTF("State: %d\n", state);
  80. if (state == STATE_HEADERS) {
  81. if (n < 9) {
  82. fprintf(stderr, "Error: too little data before HTTP headers\n");
  83. return 1;
  84. }
  85. // Handle optional URL + Length line
  86. url = NULL;
  87. if (memcmp(bufp, "HTTP/1.1 ", 9) != 0 && memcmp(bufp, "HTTP/1.0 ", 9) != 0) {
  88. DEBUG_PRINTF("No HTTP header, looking for URL line\n");
  89. m0 = memmem(bufp, n, "\n", 1);
  90. if (!m0 || m0 == bufp) {
  91. fprintf(stderr, "Error: expected HTTP headers or URL line, got neither\n");
  92. return 1;
  93. }
  94. m1 = m0;
  95. // Skip over length field, which we don't need.
  96. --m0;
  97. while (m0 > bufp && '0' <= *m0 && *m0 <= '9') --m0;
  98. if (*m0 != ' ') {
  99. fprintf(stderr, "Error: URL line has unexpected format\n");
  100. return 1;
  101. }
  102. // Rest must now be the URL; check that there is a scheme and no CR, LF, or whitespace.
  103. url = bufp;
  104. urllen = m0 - bufp;
  105. if (!memmem(url, urllen, "://", 3)) {
  106. fprintf(stderr, "Error: URL line contains no scheme\n");
  107. return 1;
  108. }
  109. m0 = url;
  110. while (m0 < bufp + urllen && *m0 != '\r' && *m0 != '\n' && *m0 != ' ' && *m0 != '\t') ++m0;
  111. if (m0 != bufp + urllen) {
  112. fprintf(stderr, "Error: URL contains CR, LF, or whitespace\n");
  113. return 1;
  114. }
  115. DEBUG_PRINTF("Found URL: ");
  116. for (int i = 0; i < (urllen > 64 ? 64 : urllen); ++i) DEBUG_PRINTF(isprint(*(url + i)) ? "%c" : "\\x%02x", *(url + i) & 0xFF);
  117. if (urllen > 64) DEBUG_PRINTF("<...>");
  118. DEBUG_PRINTF("\n");
  119. // Skip over URL line and continue processing below
  120. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 1 - bufp);
  121. n = n - (m1 + 1 - bufp);
  122. bufp = m1 + 1;
  123. }
  124. if (html_fake_base && !url) {
  125. fprintf(stderr, "Error: --html-fake-base requires URL lines\n");
  126. return 1;
  127. }
  128. if (n < 9) {
  129. fprintf(stderr, "Error: too little data before HTTP headers\n");
  130. return 1;
  131. }
  132. if (memcmp(bufp, "HTTP/1.1 ", 9) == 0 || memcmp(bufp, "HTTP/1.0 ", 9) == 0) {
  133. // Got some headers; find transfer encoding, content length, and end of headers
  134. eoh = memmem(bufp, n, "\r\n\r\n", 4);
  135. if (!eoh) {
  136. fprintf(stderr, "Error: end of headers not found\n");
  137. return 1;
  138. }
  139. eoh += 4;
  140. DEBUG_PRINTF("Response body begins at %p (offset %zu)\n", (void*)eoh, eoh - bufp);
  141. m0 = memcasemem(bufp, n, "\r\ncontent-length:", 17);
  142. if (m0 && m0 < eoh) {
  143. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  144. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  145. if (!m1) {
  146. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  147. return 1;
  148. }
  149. m0 += 17;
  150. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  151. if (sscanf(m0, "%zu%ln", &length, &nscan) <= 0) {
  152. fprintf(stderr, "Error: invalid Content-Length\n");
  153. return 1;
  154. }
  155. if (nscan < 0) {
  156. fprintf(stderr, "Error: negative nscan\n");
  157. return 1;
  158. }
  159. if (nscan > n - (m0 - bufp)) {
  160. fprintf(stderr, "Error: buffer overread\n");
  161. return 1;
  162. }
  163. m0 += nscan;
  164. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  165. if (m0 != m1) {
  166. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  167. return 1;
  168. }
  169. DEBUG_PRINTF("Content length: %zu\n", length);
  170. state = STATE_BODY;
  171. } else {
  172. m0 = memcasemem(bufp, n, "\r\ntransfer-encoding:", 20);
  173. if (!m0 || m0 >= eoh) {
  174. fprintf(stderr, "Error: Content-Length and Transfer-Encoding missing\n");
  175. return 1;
  176. }
  177. DEBUG_PRINTF("Found Transfer-Encoding header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  178. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  179. if (!m1 || m1 >= eoh - 2) {
  180. fprintf(stderr, "Error: CRLF after Transfer-Encoding missing\n");
  181. return 1;
  182. }
  183. m0 += 20;
  184. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  185. if (memcmp(m0, "chunked", 7) != 0) {
  186. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  187. return 1;
  188. }
  189. m0 += 7;
  190. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  191. if (m0 != m1) {
  192. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  193. return 1;
  194. }
  195. DEBUG_PRINTF("Chunked transfer encoding\n");
  196. state = STATE_CHUNK_LINE;
  197. }
  198. if (html_fake_base) {
  199. m0 = memcasemem(bufp, n, "\r\ncontent-type:", 15);
  200. if (m0 && m0 < eoh) {
  201. DEBUG_PRINTF("Found Content-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  202. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  203. if (!m1) {
  204. fprintf(stderr, "Error: CRLF after Content-Type missing\n");
  205. return 1;
  206. }
  207. m0 += 15;
  208. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  209. if (memcmp(m0, "text/html", 9) == 0) {
  210. DEBUG_PRINTF("Is HTML response, inserting fake base tag\n");
  211. fprintf(stdout, "<base href=\"");
  212. fwrite(url, 1, urllen, stdout);
  213. fprintf(stdout, "\">\n");
  214. }
  215. }
  216. }
  217. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", eoh - bufp);
  218. n = n - (eoh - bufp);
  219. bufp = eoh;
  220. bytes_read = 0;
  221. goto checkstate;
  222. } else {
  223. fprintf(stderr, "Error: expected header line, got something else\n");
  224. return 1;
  225. }
  226. } else if (state == STATE_BODY || state == STATE_CHUNK_CONTENTS) {
  227. if (length + (state == STATE_BODY ? 1 : 2) - bytes_read > n) {
  228. // Only got part of the body
  229. DEBUG_PRINTF("Partial body\n");
  230. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  231. fwrite(bufp, 1, n, stdout);
  232. bytes_read += n;
  233. DEBUG_PRINTF("%zu of %zu bytes from this response written\n", bytes_read, length);
  234. } else {
  235. // Remainder of the response is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  236. DEBUG_PRINTF("Full body\n");
  237. DEBUG_PRINTF("Copying %zu bytes to stdout\n", length - bytes_read);
  238. fwrite(bufp, 1, length - bytes_read, stdout);
  239. fprintf(stdout, "\n");
  240. if (state == STATE_CHUNK_CONTENTS && *(bufp + length - bytes_read) == '\r') {
  241. // Stupid hack to enforce the CRLF
  242. ++length;
  243. }
  244. if (memcmp(bufp + length - bytes_read, "\n", 1) != 0) {
  245. fprintf(stderr, "Error: end of HTTP body not found\n");
  246. return 1;
  247. }
  248. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", length + 1 - bytes_read);
  249. n = n - (length + 1 - bytes_read);
  250. bufp = bufp + length + 1 - bytes_read;
  251. if (n < BUFSIZE) {
  252. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  253. memmove(buf, bufp, n);
  254. bufp = buf;
  255. n += fread(buf + n, 1, BUFSIZE, stdin);
  256. }
  257. if (state == STATE_BODY) {
  258. state = STATE_HEADERS;
  259. } else {
  260. state = STATE_CHUNK_LINE;
  261. }
  262. goto checkstate;
  263. }
  264. } else if (state == STATE_CHUNK_LINE) {
  265. m1 = memmem(bufp, n, "\r\n", 2);
  266. if (!m1) {
  267. fprintf(stderr, "Error: chunk line EOL missing\n");
  268. return 1;
  269. }
  270. m0 = bufp;
  271. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  272. if (sscanf(m0, "%zx%ln", &length, &nscan) <= 0) {
  273. fprintf(stderr, "Error: invalid chunk length\n");
  274. return 1;
  275. }
  276. if (nscan < 0) {
  277. fprintf(stderr, "Error: negative nscan\n");
  278. return 1;
  279. }
  280. if (nscan > n - (m0 - bufp)) {
  281. fprintf(stderr, "Error: buffer overread\n");
  282. return 1;
  283. }
  284. m0 += nscan;
  285. while (m0 < m1 && (*m0 == ' ' || *m0 == '\t')) ++m0;
  286. if (*m0 != ';' && m0 != m1) {
  287. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  288. return 1;
  289. }
  290. DEBUG_PRINTF("Chunk length: %zu bytes\n", length);
  291. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 2 - bufp);
  292. n = n - (m1 + 2 - bufp);
  293. bufp = m1 + 2;
  294. if (length == 0) {
  295. // End of response, must be followed by CRLF + LF
  296. if (n < 3) {
  297. fprintf(stderr, "Error: buffer exhausted while looking for empty chunk CRLF\n");
  298. return 1;
  299. }
  300. if (*(m1 + 2) != '\r' || *(m1 + 3) != '\n' || *(m1 + 4) != '\n') {
  301. fprintf(stderr, "Error: end of HTTP body not found\n");
  302. return 1;
  303. }
  304. n -= 3;
  305. bufp += 3;
  306. state = STATE_HEADERS;
  307. } else {
  308. state = STATE_CHUNK_CONTENTS;
  309. }
  310. if (n < BUFSIZE) {
  311. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  312. memmove(buf, bufp, n);
  313. bufp = buf;
  314. n += fread(buf + n, 1, BUFSIZE, stdin);
  315. }
  316. goto checkstate;
  317. }
  318. }
  319. if (state != STATE_HEADERS) {
  320. fprintf(stderr, "Error: incomplete body at the end of input\n");
  321. return 1;
  322. }
  323. }