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.
 
 
 

341 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. urllen = 0;
  88. if (memcmp(bufp, "HTTP/1.1 ", 9) != 0 && memcmp(bufp, "HTTP/1.0 ", 9) != 0) {
  89. DEBUG_PRINTF("No HTTP header, looking for URL line\n");
  90. m0 = memmem(bufp, n, "\n", 1);
  91. if (!m0 || m0 == bufp) {
  92. fprintf(stderr, "Error: expected HTTP headers or URL line, got neither\n");
  93. return 1;
  94. }
  95. m1 = m0;
  96. // Skip over length field, which we don't need.
  97. --m0;
  98. while (m0 > bufp && '0' <= *m0 && *m0 <= '9') --m0;
  99. if (*m0 != ' ') {
  100. fprintf(stderr, "Error: URL line has unexpected format\n");
  101. return 1;
  102. }
  103. // Rest must now be the URL; check that there is a scheme and no CR, LF, or whitespace.
  104. url = bufp;
  105. urllen = m0 - bufp;
  106. if (!memmem(url, urllen, "://", 3)) {
  107. fprintf(stderr, "Error: URL line contains no scheme\n");
  108. return 1;
  109. }
  110. m0 = url;
  111. while (m0 < bufp + urllen && *m0 != '\r' && *m0 != '\n' && *m0 != ' ' && *m0 != '\t') ++m0;
  112. if (m0 != bufp + urllen) {
  113. fprintf(stderr, "Error: URL contains CR, LF, or whitespace\n");
  114. return 1;
  115. }
  116. DEBUG_PRINTF("Found URL: ");
  117. for (unsigned int i = 0; i < (urllen > 64 ? 64 : urllen); ++i) DEBUG_PRINTF(isprint(*(url + i)) ? "%c" : "\\x%02x", *(url + i) & 0xFF);
  118. if (urllen > 64) DEBUG_PRINTF("<...>");
  119. DEBUG_PRINTF("\n");
  120. // Skip over URL line and continue processing below
  121. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 1 - bufp);
  122. n = n - (m1 + 1 - bufp);
  123. bufp = m1 + 1;
  124. }
  125. if (html_fake_base && !url) {
  126. fprintf(stderr, "Error: --html-fake-base requires URL lines\n");
  127. return 1;
  128. }
  129. if (n < 9) {
  130. fprintf(stderr, "Error: too little data before HTTP headers\n");
  131. return 1;
  132. }
  133. if (memcmp(bufp, "HTTP/1.1 ", 9) == 0 || memcmp(bufp, "HTTP/1.0 ", 9) == 0) {
  134. // Got some headers; find transfer encoding, content length, and end of headers
  135. eoh = memmem(bufp, n, "\r\n\r\n", 4);
  136. if (!eoh) {
  137. fprintf(stderr, "Error: end of headers not found\n");
  138. return 1;
  139. }
  140. eoh += 4;
  141. DEBUG_PRINTF("Response body begins at %p (offset %zu)\n", (void*)eoh, eoh - bufp);
  142. m0 = memcasemem(bufp, n, "\r\ncontent-length:", 17);
  143. if (m0 && m0 < eoh) {
  144. DEBUG_PRINTF("Found Content-Length header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  145. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  146. if (!m1) {
  147. fprintf(stderr, "Error: CRLF after Content-Length missing\n");
  148. return 1;
  149. }
  150. m0 += 17;
  151. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  152. if (sscanf(m0, "%zu%ln", &length, &nscan) <= 0) {
  153. fprintf(stderr, "Error: invalid Content-Length\n");
  154. return 1;
  155. }
  156. if (nscan < 0) {
  157. fprintf(stderr, "Error: negative nscan\n");
  158. return 1;
  159. }
  160. if (m0 + nscan > bufp + n) {
  161. fprintf(stderr, "Error: buffer overread\n");
  162. return 1;
  163. }
  164. m0 += nscan;
  165. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  166. if (m0 != m1) {
  167. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  168. return 1;
  169. }
  170. DEBUG_PRINTF("Content length: %zu\n", length);
  171. state = STATE_BODY;
  172. } else {
  173. m0 = memcasemem(bufp, n, "\r\ntransfer-encoding:", 20);
  174. if (!m0 || m0 >= eoh) {
  175. fprintf(stderr, "Error: Content-Length and Transfer-Encoding missing\n");
  176. return 1;
  177. }
  178. DEBUG_PRINTF("Found Transfer-Encoding header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  179. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  180. if (!m1 || m1 >= eoh - 2) {
  181. fprintf(stderr, "Error: CRLF after Transfer-Encoding missing\n");
  182. return 1;
  183. }
  184. m0 += 20;
  185. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  186. if (memcmp(m0, "chunked", 7) != 0) {
  187. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  188. return 1;
  189. }
  190. m0 += 7;
  191. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  192. if (m0 != m1) {
  193. fprintf(stderr, "Error: unsupported Transfer-Encoding\n");
  194. return 1;
  195. }
  196. DEBUG_PRINTF("Chunked transfer encoding\n");
  197. state = STATE_CHUNK_LINE;
  198. }
  199. if (html_fake_base) {
  200. m0 = memcasemem(bufp, n, "\r\ncontent-type:", 15);
  201. if (m0 && m0 < eoh) {
  202. DEBUG_PRINTF("Found Content-Type header at %p (offset %zu)\n", (void*)(m0 + 2), m0 + 2 - bufp);
  203. m1 = memmem(m0 + 1, n - (m0 + 1 - bufp), "\r\n", 2);
  204. if (!m1) {
  205. fprintf(stderr, "Error: CRLF after Content-Type missing\n");
  206. return 1;
  207. }
  208. m0 += 15;
  209. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  210. if (memcmp(m0, "text/html", 9) == 0) {
  211. DEBUG_PRINTF("Is HTML response, inserting fake base tag\n");
  212. fprintf(stdout, "<base href=\"");
  213. fwrite(url, 1, urllen, stdout);
  214. fprintf(stdout, "\">\n");
  215. }
  216. }
  217. }
  218. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", eoh - bufp);
  219. n = n - (eoh - bufp);
  220. bufp = eoh;
  221. bytes_read = 0;
  222. goto checkstate;
  223. } else {
  224. fprintf(stderr, "Error: expected header line, got something else\n");
  225. return 1;
  226. }
  227. } else if (state == STATE_BODY || state == STATE_CHUNK_CONTENTS) {
  228. if (length + (state == STATE_BODY ? 1 : 2) - bytes_read > n) {
  229. // Only got part of the body
  230. DEBUG_PRINTF("Partial body\n");
  231. DEBUG_PRINTF("Copying %zu bytes to stdout\n", n);
  232. fwrite(bufp, 1, n, stdout);
  233. bytes_read += n;
  234. DEBUG_PRINTF("%zu of %zu bytes from this response written\n", bytes_read, length);
  235. } else {
  236. // Remainder of the response is in the buffer. Same logic as above for small records fitting in the buffer with the headers.
  237. DEBUG_PRINTF("Full body\n");
  238. DEBUG_PRINTF("Copying %zu bytes to stdout\n", length - bytes_read);
  239. fwrite(bufp, 1, length - bytes_read, stdout);
  240. fprintf(stdout, "\n");
  241. if (state == STATE_CHUNK_CONTENTS && *(bufp + length - bytes_read) == '\r') {
  242. // Stupid hack to enforce the CRLF
  243. ++length;
  244. }
  245. if (memcmp(bufp + length - bytes_read, "\n", 1) != 0) {
  246. fprintf(stderr, "Error: end of HTTP body not found\n");
  247. return 1;
  248. }
  249. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", length + 1 - bytes_read);
  250. n = n - (length + 1 - bytes_read);
  251. bufp = bufp + length + 1 - bytes_read;
  252. if (n < BUFSIZE) {
  253. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  254. memmove(buf, bufp, n);
  255. bufp = buf;
  256. n += fread(buf + n, 1, BUFSIZE, stdin);
  257. }
  258. if (state == STATE_BODY) {
  259. state = STATE_HEADERS;
  260. } else {
  261. state = STATE_CHUNK_LINE;
  262. }
  263. goto checkstate;
  264. }
  265. } else if (state == STATE_CHUNK_LINE) {
  266. m1 = memmem(bufp, n, "\r\n", 2);
  267. if (!m1) {
  268. fprintf(stderr, "Error: chunk line EOL missing\n");
  269. return 1;
  270. }
  271. m0 = bufp;
  272. while (m0 < bufp + n && (*m0 == ' ' || *m0 == '\t')) ++m0;
  273. if (sscanf(m0, "%zx%ln", &length, &nscan) <= 0) {
  274. fprintf(stderr, "Error: invalid chunk length\n");
  275. return 1;
  276. }
  277. if (nscan < 0) {
  278. fprintf(stderr, "Error: negative nscan\n");
  279. return 1;
  280. }
  281. if (m0 + nscan > bufp + n) {
  282. fprintf(stderr, "Error: buffer overread\n");
  283. return 1;
  284. }
  285. m0 += nscan;
  286. while (m0 < m1 && (*m0 == ' ' || *m0 == '\t')) ++m0;
  287. if (*m0 != ';' && m0 != m1) {
  288. fprintf(stderr, "Error: invalid Content-Length (noise before EOL)\n");
  289. return 1;
  290. }
  291. DEBUG_PRINTF("Chunk length: %zu bytes\n", length);
  292. DEBUG_PRINTF("Adjusting buffer pointer and n by %zu\n", m1 + 2 - bufp);
  293. n = n - (m1 + 2 - bufp);
  294. bufp = m1 + 2;
  295. if (length == 0) {
  296. // End of response, must be followed by CRLF + LF
  297. if (n < 3) {
  298. fprintf(stderr, "Error: buffer exhausted while looking for empty chunk CRLF\n");
  299. return 1;
  300. }
  301. if (*(m1 + 2) != '\r' || *(m1 + 3) != '\n' || *(m1 + 4) != '\n') {
  302. fprintf(stderr, "Error: end of HTTP body not found\n");
  303. return 1;
  304. }
  305. n -= 3;
  306. bufp += 3;
  307. state = STATE_HEADERS;
  308. } else {
  309. state = STATE_CHUNK_CONTENTS;
  310. }
  311. if (n < BUFSIZE) {
  312. DEBUG_PRINTF("Buffer too small (%zu bytes), moving and refilling\n", n);
  313. memmove(buf, bufp, n);
  314. bufp = buf;
  315. n += fread(buf + n, 1, BUFSIZE, stdin);
  316. }
  317. goto checkstate;
  318. }
  319. }
  320. if (state != STATE_HEADERS) {
  321. fprintf(stderr, "Error: incomplete body at the end of input\n");
  322. return 1;
  323. }
  324. }