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.
 
 
 

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