http.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. /*
  2. * GRUB -- GRand Unified Bootloader
  3. * Copyright (C) 2010,2011 Free Software Foundation, Inc.
  4. *
  5. * GRUB is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * GRUB is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <grub/misc.h>
  19. #include <grub/net/tcp.h>
  20. #include <grub/net/ip.h>
  21. #include <grub/net/ethernet.h>
  22. #include <grub/net/netbuff.h>
  23. #include <grub/net.h>
  24. #include <grub/mm.h>
  25. #include <grub/dl.h>
  26. #include <grub/file.h>
  27. #include <grub/i18n.h>
  28. GRUB_MOD_LICENSE ("GPLv3+");
  29. #define HTTP_PORT ((grub_uint16_t) 80)
  30. typedef struct http_data
  31. {
  32. char *current_line;
  33. grub_size_t current_line_len;
  34. int headers_recv;
  35. int first_line_recv;
  36. int size_recv;
  37. grub_net_tcp_socket_t sock;
  38. char *filename;
  39. grub_err_t err;
  40. char *errmsg;
  41. int chunked;
  42. grub_size_t chunk_rem;
  43. int in_chunk_len;
  44. } *http_data_t;
  45. static grub_off_t
  46. have_ahead (struct grub_file *file)
  47. {
  48. grub_net_t net = file->device->net;
  49. grub_off_t ret = net->offset;
  50. struct grub_net_packet *pack;
  51. for (pack = net->packs.first; pack; pack = pack->next)
  52. ret += pack->nb->tail - pack->nb->data;
  53. return ret;
  54. }
  55. static grub_err_t
  56. parse_line (grub_file_t file, http_data_t data, char *ptr, grub_size_t len)
  57. {
  58. char *end = ptr + len;
  59. while (end > ptr && *(end - 1) == '\r')
  60. end--;
  61. /* LF without CR. */
  62. if (end == ptr + len)
  63. {
  64. data->errmsg = grub_strdup (_("invalid HTTP header - LF without CR"));
  65. return GRUB_ERR_NONE;
  66. }
  67. *end = 0;
  68. /* Trailing CRLF. */
  69. if (data->in_chunk_len == 1)
  70. {
  71. data->in_chunk_len = 2;
  72. return GRUB_ERR_NONE;
  73. }
  74. if (data->in_chunk_len == 2)
  75. {
  76. data->chunk_rem = grub_strtoul (ptr, 0, 16);
  77. grub_errno = GRUB_ERR_NONE;
  78. if (data->chunk_rem == 0)
  79. {
  80. file->device->net->eof = 1;
  81. file->device->net->stall = 1;
  82. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  83. file->size = have_ahead (file);
  84. }
  85. data->in_chunk_len = 0;
  86. return GRUB_ERR_NONE;
  87. }
  88. if (ptr == end)
  89. {
  90. data->headers_recv = 1;
  91. if (data->chunked)
  92. data->in_chunk_len = 2;
  93. return GRUB_ERR_NONE;
  94. }
  95. if (!data->first_line_recv)
  96. {
  97. int code;
  98. if (grub_memcmp (ptr, "HTTP/1.1 ", sizeof ("HTTP/1.1 ") - 1) != 0)
  99. {
  100. data->errmsg = grub_strdup (_("unsupported HTTP response"));
  101. data->first_line_recv = 1;
  102. return GRUB_ERR_NONE;
  103. }
  104. ptr += sizeof ("HTTP/1.1 ") - 1;
  105. code = grub_strtoul (ptr, (const char **)&ptr, 10);
  106. if (grub_errno)
  107. return grub_errno;
  108. switch (code)
  109. {
  110. case 200:
  111. case 206:
  112. break;
  113. case 404:
  114. data->err = GRUB_ERR_FILE_NOT_FOUND;
  115. data->errmsg = grub_xasprintf (_("file `%s' not found"), data->filename);
  116. return GRUB_ERR_NONE;
  117. default:
  118. data->err = GRUB_ERR_NET_UNKNOWN_ERROR;
  119. /* TRANSLATORS: GRUB HTTP code is pretty young. So even perfectly
  120. valid answers like 403 will trigger this very generic message. */
  121. data->errmsg = grub_xasprintf (_("unsupported HTTP error %d: %s"),
  122. code, ptr);
  123. return GRUB_ERR_NONE;
  124. }
  125. data->first_line_recv = 1;
  126. return GRUB_ERR_NONE;
  127. }
  128. if (grub_memcmp (ptr, "Content-Length: ", sizeof ("Content-Length: ") - 1)
  129. == 0 && !data->size_recv)
  130. {
  131. ptr += sizeof ("Content-Length: ") - 1;
  132. file->size = grub_strtoull (ptr, (const char **)&ptr, 10);
  133. data->size_recv = 1;
  134. return GRUB_ERR_NONE;
  135. }
  136. if (grub_memcmp (ptr, "Transfer-Encoding: chunked",
  137. sizeof ("Transfer-Encoding: chunked") - 1) == 0)
  138. {
  139. data->chunked = 1;
  140. return GRUB_ERR_NONE;
  141. }
  142. return GRUB_ERR_NONE;
  143. }
  144. static void
  145. http_err (grub_net_tcp_socket_t sock __attribute__ ((unused)),
  146. void *f)
  147. {
  148. grub_file_t file = f;
  149. http_data_t data = file->data;
  150. if (data->sock)
  151. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  152. data->sock = 0;
  153. if (data->current_line)
  154. grub_free (data->current_line);
  155. data->current_line = 0;
  156. file->device->net->eof = 1;
  157. file->device->net->stall = 1;
  158. if (file->size == GRUB_FILE_SIZE_UNKNOWN)
  159. file->size = have_ahead (file);
  160. }
  161. static grub_err_t
  162. http_receive (grub_net_tcp_socket_t sock __attribute__ ((unused)),
  163. struct grub_net_buff *nb,
  164. void *f)
  165. {
  166. grub_file_t file = f;
  167. http_data_t data = file->data;
  168. grub_err_t err;
  169. if (!data->sock)
  170. {
  171. grub_netbuff_free (nb);
  172. return GRUB_ERR_NONE;
  173. }
  174. while (1)
  175. {
  176. char *ptr = (char *) nb->data;
  177. if ((!data->headers_recv || data->in_chunk_len) && data->current_line)
  178. {
  179. int have_line = 1;
  180. char *t;
  181. ptr = grub_memchr (nb->data, '\n', nb->tail - nb->data);
  182. if (ptr == NULL)
  183. {
  184. have_line = 0;
  185. ptr = (char *) nb->tail;
  186. }
  187. t = grub_realloc (data->current_line,
  188. data->current_line_len + (ptr - (char *) nb->data));
  189. if (!t)
  190. {
  191. grub_netbuff_free (nb);
  192. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  193. return grub_errno;
  194. }
  195. data->current_line = t;
  196. grub_memcpy (data->current_line + data->current_line_len,
  197. nb->data, ptr - (char *) nb->data);
  198. data->current_line_len += ptr - (char *) nb->data;
  199. if (!have_line)
  200. {
  201. grub_netbuff_free (nb);
  202. return GRUB_ERR_NONE;
  203. }
  204. err = parse_line (file, data, data->current_line,
  205. data->current_line_len);
  206. grub_free (data->current_line);
  207. data->current_line = 0;
  208. data->current_line_len = 0;
  209. if (err)
  210. {
  211. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  212. grub_netbuff_free (nb);
  213. return err;
  214. }
  215. }
  216. while (ptr < (char *) nb->tail && (!data->headers_recv
  217. || data->in_chunk_len))
  218. {
  219. char *ptr2;
  220. ptr2 = grub_memchr (ptr, '\n', (char *) nb->tail - ptr);
  221. if (!ptr2)
  222. {
  223. data->current_line = grub_malloc ((char *) nb->tail - ptr);
  224. if (!data->current_line)
  225. {
  226. grub_netbuff_free (nb);
  227. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  228. return grub_errno;
  229. }
  230. data->current_line_len = (char *) nb->tail - ptr;
  231. grub_memcpy (data->current_line, ptr, data->current_line_len);
  232. grub_netbuff_free (nb);
  233. return GRUB_ERR_NONE;
  234. }
  235. err = parse_line (file, data, ptr, ptr2 - ptr);
  236. if (err)
  237. {
  238. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  239. grub_netbuff_free (nb);
  240. return err;
  241. }
  242. ptr = ptr2 + 1;
  243. }
  244. if (((char *) nb->tail - ptr) <= 0)
  245. {
  246. grub_netbuff_free (nb);
  247. return GRUB_ERR_NONE;
  248. }
  249. err = grub_netbuff_pull (nb, ptr - (char *) nb->data);
  250. if (err)
  251. {
  252. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  253. grub_netbuff_free (nb);
  254. return err;
  255. }
  256. if (!(data->chunked && (grub_ssize_t) data->chunk_rem
  257. < nb->tail - nb->data))
  258. {
  259. grub_net_put_packet (&file->device->net->packs, nb);
  260. if (file->device->net->packs.count >= 20)
  261. file->device->net->stall = 1;
  262. if (file->device->net->packs.count >= 100)
  263. grub_net_tcp_stall (data->sock);
  264. if (data->chunked)
  265. data->chunk_rem -= nb->tail - nb->data;
  266. return GRUB_ERR_NONE;
  267. }
  268. if (data->chunk_rem)
  269. {
  270. struct grub_net_buff *nb2;
  271. nb2 = grub_netbuff_alloc (data->chunk_rem);
  272. if (!nb2)
  273. return grub_errno;
  274. grub_netbuff_put (nb2, data->chunk_rem);
  275. grub_memcpy (nb2->data, nb->data, data->chunk_rem);
  276. if (file->device->net->packs.count >= 20)
  277. {
  278. file->device->net->stall = 1;
  279. grub_net_tcp_stall (data->sock);
  280. }
  281. grub_net_put_packet (&file->device->net->packs, nb2);
  282. grub_netbuff_pull (nb, data->chunk_rem);
  283. }
  284. data->in_chunk_len = 1;
  285. }
  286. }
  287. static grub_err_t
  288. http_establish (struct grub_file *file, grub_off_t offset, int initial)
  289. {
  290. http_data_t data = file->data;
  291. grub_uint8_t *ptr;
  292. int i;
  293. struct grub_net_buff *nb;
  294. grub_err_t err;
  295. char *server = file->device->net->server;
  296. grub_uint16_t port = file->device->net->port;
  297. nb = grub_netbuff_alloc (GRUB_NET_TCP_RESERVE_SIZE
  298. + sizeof ("GET ") - 1
  299. + grub_strlen (data->filename)
  300. + sizeof (" HTTP/1.1\r\nHost: ") - 1
  301. + grub_strlen (server) + sizeof (":XXXXXXXXXX")
  302. + sizeof ("\r\nUser-Agent: " PACKAGE_STRING
  303. "\r\n") - 1
  304. + sizeof ("Range: bytes=XXXXXXXXXXXXXXXXXXXX"
  305. "-\r\n\r\n"));
  306. if (!nb)
  307. return grub_errno;
  308. grub_netbuff_reserve (nb, GRUB_NET_TCP_RESERVE_SIZE);
  309. ptr = nb->tail;
  310. err = grub_netbuff_put (nb, sizeof ("GET ") - 1);
  311. if (err)
  312. {
  313. grub_netbuff_free (nb);
  314. return err;
  315. }
  316. grub_memcpy (ptr, "GET ", sizeof ("GET ") - 1);
  317. ptr = nb->tail;
  318. err = grub_netbuff_put (nb, grub_strlen (data->filename));
  319. if (err)
  320. {
  321. grub_netbuff_free (nb);
  322. return err;
  323. }
  324. grub_memcpy (ptr, data->filename, grub_strlen (data->filename));
  325. ptr = nb->tail;
  326. err = grub_netbuff_put (nb, sizeof (" HTTP/1.1\r\nHost: ") - 1);
  327. if (err)
  328. {
  329. grub_netbuff_free (nb);
  330. return err;
  331. }
  332. grub_memcpy (ptr, " HTTP/1.1\r\nHost: ",
  333. sizeof (" HTTP/1.1\r\nHost: ") - 1);
  334. ptr = nb->tail;
  335. err = grub_netbuff_put (nb, grub_strlen (server));
  336. if (err)
  337. {
  338. grub_netbuff_free (nb);
  339. return err;
  340. }
  341. grub_memcpy (ptr, file->device->net->server,
  342. grub_strlen (file->device->net->server));
  343. if (port)
  344. {
  345. ptr = nb->tail;
  346. grub_snprintf ((char *) ptr, sizeof (":XXXXXXXXXX"), ":%d", port);
  347. }
  348. ptr = nb->tail;
  349. err = grub_netbuff_put (nb,
  350. sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n")
  351. - 1);
  352. if (err)
  353. {
  354. grub_netbuff_free (nb);
  355. return err;
  356. }
  357. grub_memcpy (ptr, "\r\nUser-Agent: " PACKAGE_STRING "\r\n",
  358. sizeof ("\r\nUser-Agent: " PACKAGE_STRING "\r\n") - 1);
  359. if (!initial)
  360. {
  361. ptr = nb->tail;
  362. grub_snprintf ((char *) ptr,
  363. sizeof ("Range: bytes=XXXXXXXXXXXXXXXXXXXX-"
  364. "\r\n"),
  365. "Range: bytes=%" PRIuGRUB_UINT64_T "-\r\n",
  366. offset);
  367. grub_netbuff_put (nb, grub_strlen ((char *) ptr));
  368. }
  369. ptr = nb->tail;
  370. grub_netbuff_put (nb, 2);
  371. grub_memcpy (ptr, "\r\n", 2);
  372. grub_dprintf ("http", "opening path %s on host %s TCP port %d\n",
  373. data->filename, server, port ? port : HTTP_PORT);
  374. data->sock = grub_net_tcp_open (server,
  375. port ? port : HTTP_PORT, http_receive,
  376. http_err, NULL,
  377. file);
  378. if (!data->sock)
  379. {
  380. grub_netbuff_free (nb);
  381. return grub_errno;
  382. }
  383. // grub_net_poll_cards (5000);
  384. err = grub_net_send_tcp_packet (data->sock, nb, 1);
  385. if (err)
  386. {
  387. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  388. return err;
  389. }
  390. for (i = 0; data->sock && !data->headers_recv && i < 100; i++)
  391. {
  392. grub_net_tcp_retransmit ();
  393. grub_net_poll_cards (300, &data->headers_recv);
  394. }
  395. if (!data->headers_recv)
  396. {
  397. if (data->sock)
  398. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  399. if (data->err)
  400. {
  401. char *str = data->errmsg;
  402. err = grub_error (data->err, "%s", str);
  403. grub_free (str);
  404. data->errmsg = 0;
  405. return data->err;
  406. }
  407. return grub_error (GRUB_ERR_TIMEOUT, N_("time out opening `%s'"), data->filename);
  408. }
  409. return GRUB_ERR_NONE;
  410. }
  411. static grub_err_t
  412. http_seek (struct grub_file *file, grub_off_t off)
  413. {
  414. struct http_data *old_data, *data;
  415. grub_err_t err;
  416. old_data = file->data;
  417. /* FIXME: Reuse socket? */
  418. if (old_data->sock)
  419. grub_net_tcp_close (old_data->sock, GRUB_NET_TCP_ABORT);
  420. old_data->sock = 0;
  421. while (file->device->net->packs.first)
  422. {
  423. grub_netbuff_free (file->device->net->packs.first->nb);
  424. grub_net_remove_packet (file->device->net->packs.first);
  425. }
  426. file->device->net->stall = 0;
  427. file->device->net->eof = 0;
  428. file->device->net->offset = off;
  429. data = grub_zalloc (sizeof (*data));
  430. if (!data)
  431. return grub_errno;
  432. data->size_recv = 1;
  433. data->filename = old_data->filename;
  434. if (!data->filename)
  435. {
  436. grub_free (data);
  437. file->data = 0;
  438. return grub_errno;
  439. }
  440. grub_free (old_data);
  441. file->data = data;
  442. err = http_establish (file, off, 0);
  443. if (err)
  444. {
  445. grub_free (data->filename);
  446. grub_free (data);
  447. file->data = 0;
  448. return err;
  449. }
  450. return GRUB_ERR_NONE;
  451. }
  452. static grub_err_t
  453. http_open (struct grub_file *file, const char *filename)
  454. {
  455. grub_err_t err;
  456. struct http_data *data;
  457. data = grub_zalloc (sizeof (*data));
  458. if (!data)
  459. return grub_errno;
  460. file->size = GRUB_FILE_SIZE_UNKNOWN;
  461. data->filename = grub_strdup (filename);
  462. if (!data->filename)
  463. {
  464. grub_free (data);
  465. return grub_errno;
  466. }
  467. file->not_easily_seekable = 0;
  468. file->data = data;
  469. err = http_establish (file, 0, 1);
  470. if (err)
  471. {
  472. grub_free (data->filename);
  473. grub_free (data);
  474. return err;
  475. }
  476. return GRUB_ERR_NONE;
  477. }
  478. static grub_err_t
  479. http_close (struct grub_file *file)
  480. {
  481. http_data_t data = file->data;
  482. if (!data)
  483. return GRUB_ERR_NONE;
  484. if (data->sock)
  485. grub_net_tcp_close (data->sock, GRUB_NET_TCP_ABORT);
  486. if (data->current_line)
  487. grub_free (data->current_line);
  488. grub_free (data->filename);
  489. grub_free (data);
  490. return GRUB_ERR_NONE;
  491. }
  492. static grub_err_t
  493. http_packets_pulled (struct grub_file *file)
  494. {
  495. http_data_t data = file->data;
  496. if (file->device->net->packs.count >= 20)
  497. return 0;
  498. if (!file->device->net->eof)
  499. file->device->net->stall = 0;
  500. if (data && data->sock)
  501. grub_net_tcp_unstall (data->sock);
  502. return 0;
  503. }
  504. static struct grub_net_app_protocol grub_http_protocol =
  505. {
  506. .name = "http",
  507. .open = http_open,
  508. .close = http_close,
  509. .seek = http_seek,
  510. .packets_pulled = http_packets_pulled
  511. };
  512. GRUB_MOD_INIT (http)
  513. {
  514. grub_net_app_level_register (&grub_http_protocol);
  515. }
  516. GRUB_MOD_FINI (http)
  517. {
  518. grub_net_app_level_unregister (&grub_http_protocol);
  519. }