ucmd_websrv.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658
  1. /* websrv: a minimal HTTP/1.1 + WebDAV server.
  2. *
  3. * The TCP stack and the NIC stay in the kernel (see tcp.c/sys_net.c); this
  4. * command owns the receiver, parses one request per connection (always
  5. * "Connection: close") and maps WebDAV onto the VFS:
  6. *
  7. * / the mounted filesystems (collections)
  8. * /ramfs/... read-only (the command pack)
  9. * /floppy1/... FAT, writable with 8.3 names only
  10. * /share/... writable RAM store with long names
  11. *
  12. * Methods: OPTIONS, GET, HEAD, PUT, DELETE, MKCOL, PROPFIND, PROPPATCH,
  13. * MOVE, LOCK, UNLOCK. No authentication: this is a trusted-LAN service.
  14. *
  15. * A file body is buffered whole (4 MB, or 1 MB when the heap is small)
  16. * because the VFS has no open/read/write-at interface.
  17. */
  18. #include "ucmd_lib.h"
  19. #define REQ_MAX 4096u
  20. #define BODY_MAX (4u * 1024u * 1024u)
  21. #define BODY_FALLBACK (1024u * 1024u)
  22. #define PATH_MAX 192u
  23. #define XML_MAX 3072u
  24. #define CHUNK_MAX 3072u
  25. #define SOCK_MS 1000u
  26. #define HDR_TIMEOUT_MS 10000u
  27. #define REQ_TIMEOUT_MS 60000u
  28. #define MAX_MOUNTS 24u
  29. static char hdr[REQ_MAX];
  30. static unsigned int hdr_len;
  31. static char rbuf[REQ_MAX];
  32. static unsigned char *body;
  33. static unsigned int body_max;
  34. static char resp[1024];
  35. static unsigned int resp_len;
  36. static char xml[XML_MAX];
  37. static unsigned int xml_len;
  38. static char cbuf[CHUNK_MAX];
  39. static struct sys_mount mounts[MAX_MOUNTS];
  40. static unsigned int mount_count;
  41. static unsigned int req_start;
  42. static int status;
  43. static const char *extra_hdrs;
  44. struct req {
  45. char method[12];
  46. char target[PATH_MAX];
  47. char path[PATH_MAX];
  48. char dest[PATH_MAX];
  49. int http11;
  50. int have_len;
  51. unsigned int content_len;
  52. int chunked;
  53. int depth;
  54. int expect_continue;
  55. int overwrite;
  56. int have_dest;
  57. };
  58. static struct req rq;
  59. /* ---- small string helpers ---- */
  60. static unsigned int s_len(const char *s)
  61. {
  62. unsigned int n = 0;
  63. while (s[n])
  64. n++;
  65. return n;
  66. }
  67. static int s_eq(const char *a, const char *b)
  68. {
  69. unsigned int i = 0;
  70. while (a[i] && b[i] && a[i] == b[i])
  71. i++;
  72. return a[i] == '\0' && b[i] == '\0';
  73. }
  74. static int s_ieq(const char *a, const char *b)
  75. {
  76. unsigned int i = 0;
  77. for (;;) {
  78. char ca = a[i], cb = b[i];
  79. if (ca >= 'A' && ca <= 'Z')
  80. ca = (char)(ca - 'A' + 'a');
  81. if (cb >= 'A' && cb <= 'Z')
  82. cb = (char)(cb - 'A' + 'a');
  83. if (ca != cb)
  84. return 0;
  85. if (!ca)
  86. return 1;
  87. i++;
  88. }
  89. }
  90. static int s_contains_ci(const char *hay, const char *needle)
  91. {
  92. unsigned int i, j, n = s_len(needle);
  93. if (!n)
  94. return 1;
  95. for (i = 0; hay[i]; i++) {
  96. for (j = 0; j < n; j++) {
  97. char a = hay[i + j], b = needle[j];
  98. if (!a)
  99. return 0;
  100. if (a >= 'A' && a <= 'Z')
  101. a = (char)(a - 'A' + 'a');
  102. if (b >= 'A' && b <= 'Z')
  103. b = (char)(b - 'A' + 'a');
  104. if (a != b)
  105. break;
  106. }
  107. if (j == n)
  108. return 1;
  109. }
  110. return 0;
  111. }
  112. static void s_copy(char *d, const char *s, unsigned int cap)
  113. {
  114. unsigned int i = 0;
  115. while (i + 1 < cap && s[i]) {
  116. d[i] = s[i];
  117. i++;
  118. }
  119. d[i] = '\0';
  120. }
  121. static void mem_copy(void *d, const void *s, unsigned int n)
  122. {
  123. unsigned char *dd = d;
  124. const unsigned char *ss = s;
  125. while (n--)
  126. *dd++ = *ss++;
  127. }
  128. static int hexval(int c)
  129. {
  130. if (c >= '0' && c <= '9')
  131. return c - '0';
  132. if (c >= 'a' && c <= 'f')
  133. return c - 'a' + 10;
  134. if (c >= 'A' && c <= 'F')
  135. return c - 'A' + 10;
  136. return -1;
  137. }
  138. /* ---- response buffer ---- */
  139. static void ra(const char *s)
  140. {
  141. while (*s && resp_len + 1 < sizeof(resp))
  142. resp[resp_len++] = *s++;
  143. resp[resp_len] = '\0';
  144. }
  145. static void rad(unsigned int v)
  146. {
  147. char tmp[12];
  148. unsigned int i = sizeof(tmp) - 1;
  149. tmp[i] = '\0';
  150. if (!v) {
  151. ra("0");
  152. return;
  153. }
  154. while (v && i > 0) {
  155. tmp[--i] = (char)('0' + (v % 10));
  156. v /= 10;
  157. }
  158. ra(tmp + i);
  159. }
  160. static const char *reason_phrase(int code)
  161. {
  162. switch (code) {
  163. case 100: return "Continue";
  164. case 200: return "OK";
  165. case 201: return "Created";
  166. case 204: return "No Content";
  167. case 207: return "Multi-Status";
  168. case 301: return "Moved Permanently";
  169. case 400: return "Bad Request";
  170. case 403: return "Forbidden";
  171. case 404: return "Not Found";
  172. case 405: return "Method Not Allowed";
  173. case 408: return "Request Timeout";
  174. case 409: return "Conflict";
  175. case 411: return "Length Required";
  176. case 412: return "Precondition Failed";
  177. case 413: return "Payload Too Large";
  178. case 414: return "URI Too Long";
  179. case 431: return "Request Header Fields Too Large";
  180. case 500: return "Internal Server Error";
  181. case 501: return "Not Implemented";
  182. case 507: return "Insufficient Storage";
  183. default: return "Error";
  184. }
  185. }
  186. /* Send the status line + headers. `extra` is appended verbatim (or NULL). */
  187. static int send_headers(int code, const char *reason, const char *ctype,
  188. unsigned int len)
  189. {
  190. resp_len = 0;
  191. ra("HTTP/1.1 ");
  192. rad((unsigned int)code);
  193. ra(" ");
  194. ra(reason);
  195. ra("\r\nServer: Nebs OS/0.2.0\r\n");
  196. ra("Connection: close\r\nCache-Control: no-store\r\n");
  197. if (extra_hdrs) {
  198. ra(extra_hdrs);
  199. ra("\r\n");
  200. }
  201. if (ctype) {
  202. ra("Content-Type: ");
  203. ra(ctype);
  204. ra("\r\n");
  205. }
  206. ra("Content-Length: ");
  207. rad(len);
  208. ra("\r\n\r\n");
  209. status = code;
  210. return u_tcp_srv_send(resp, resp_len);
  211. }
  212. /* Small response with an optional body. */
  213. static int send_body(int code, const char *reason, const char *ctype,
  214. const void *data, unsigned int len)
  215. {
  216. if (send_headers(code, reason, ctype, len) != 0)
  217. return -1;
  218. if (len && u_tcp_srv_send(data, len) != 0)
  219. return -1;
  220. return 0;
  221. }
  222. static int send_error(int code)
  223. {
  224. char msg[64];
  225. unsigned int n = 0;
  226. const char *reason = reason_phrase(code);
  227. msg[n++] = (char)('0' + (code / 100) % 10);
  228. msg[n++] = (char)('0' + (code / 10) % 10);
  229. msg[n++] = (char)('0' + code % 10);
  230. msg[n++] = ' ';
  231. while (*reason && n + 2 < sizeof(msg))
  232. msg[n++] = *reason++;
  233. msg[n++] = '\n';
  234. return send_body(code, reason_phrase(code), "text/plain", msg, n);
  235. }
  236. static int send_empty(int code)
  237. {
  238. return send_headers(code, reason_phrase(code), 0, 0);
  239. }
  240. /* Refuse a request and leave a human-readable reason in the terminal log
  241. * (the WebDAV client only sees the status code). */
  242. static int send_refuse(int code, const char *why)
  243. {
  244. u_puts("websrv: refused: ");
  245. u_puts(why);
  246. u_putc('\n');
  247. return send_error(code);
  248. }
  249. /* ---- chunked response (PROPFIND) ---- */
  250. static int chunk_send(const char *p, unsigned int n)
  251. {
  252. static const char hex[] = "0123456789abcdef";
  253. unsigned int l = 0, shift = 0, started = 0;
  254. if (n + 24 > sizeof(cbuf))
  255. return -1;
  256. if (!n) {
  257. mem_copy(cbuf, "0\r\n\r\n", 5);
  258. return u_tcp_srv_send(cbuf, 5);
  259. }
  260. /* hex size, most significant nibble first */
  261. for (shift = 28; ; shift -= 4) {
  262. unsigned int d = (n >> shift) & 0xF;
  263. if (d || started || shift == 0) {
  264. cbuf[l++] = hex[d];
  265. started = 1;
  266. }
  267. if (shift == 0)
  268. break;
  269. }
  270. cbuf[l++] = '\r';
  271. cbuf[l++] = '\n';
  272. mem_copy(cbuf + l, p, n);
  273. l += n;
  274. cbuf[l++] = '\r';
  275. cbuf[l++] = '\n';
  276. return u_tcp_srv_send(cbuf, l);
  277. }
  278. static const char multistatus_open[] =
  279. "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  280. "<D:multistatus xmlns:D=\"DAV:\">";
  281. static int send_multistatus_begin(void)
  282. {
  283. resp_len = 0;
  284. ra("HTTP/1.1 207 Multi-Status\r\n");
  285. ra("Server: Nebs OS/0.2.0\r\n");
  286. ra("Content-Type: text/xml; charset=\"utf-8\"\r\n");
  287. ra("Transfer-Encoding: chunked\r\n");
  288. ra("Connection: close\r\nCache-Control: no-store\r\n\r\n");
  289. status = 207;
  290. if (u_tcp_srv_send(resp, resp_len) != 0)
  291. return -1;
  292. return chunk_send(multistatus_open, s_len(multistatus_open));
  293. }
  294. static int end_multistatus(void)
  295. {
  296. static const char close_tag[] = "</D:multistatus>";
  297. if (chunk_send(close_tag, s_len(close_tag)) != 0)
  298. return -1;
  299. return chunk_send("", 0);
  300. }
  301. /* ---- XML building ---- */
  302. static void xa(const char *s)
  303. {
  304. while (*s && xml_len + 1 < sizeof(xml))
  305. xml[xml_len++] = *s++;
  306. xml[xml_len] = '\0';
  307. }
  308. static void xa_esc(const char *s)
  309. {
  310. while (*s && xml_len + 6 < sizeof(xml)) {
  311. char c = *s++;
  312. switch (c) {
  313. case '&': xa("&amp;"); break;
  314. case '<': xa("&lt;"); break;
  315. case '>': xa("&gt;"); break;
  316. case '"': xa("&quot;"); break;
  317. case '\'': xa("&apos;"); break;
  318. default: xml[xml_len++] = c; break;
  319. }
  320. }
  321. xml[xml_len] = '\0';
  322. }
  323. static void xa_dec(unsigned int v)
  324. {
  325. char tmp[12];
  326. unsigned int i = sizeof(tmp) - 1;
  327. tmp[i] = '\0';
  328. if (!v) {
  329. xa("0");
  330. return;
  331. }
  332. while (v && i > 0) {
  333. tmp[--i] = (char)('0' + (v % 10));
  334. v /= 10;
  335. }
  336. xa(tmp + i);
  337. }
  338. /* Percent-encode a path for an href (keep '/' and unreserved characters). */
  339. static void url_escape(const char *src, char *dst, unsigned int cap)
  340. {
  341. static const char hex[] = "0123456789ABCDEF";
  342. unsigned int n = 0;
  343. while (*src && n + 4 < cap) {
  344. unsigned char c = (unsigned char)*src++;
  345. if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
  346. (c >= '0' && c <= '9') || c == '/' || c == '-' ||
  347. c == '_' || c == '.' || c == '~') {
  348. dst[n++] = (char)c;
  349. } else {
  350. dst[n++] = '%';
  351. dst[n++] = hex[c >> 4];
  352. dst[n++] = hex[c & 0xF];
  353. }
  354. }
  355. dst[n] = '\0';
  356. }
  357. /* ---- request reader ---- */
  358. static unsigned char *rptr;
  359. static unsigned int rlen, rpos;
  360. static void rb_init(unsigned int off)
  361. {
  362. rptr = (unsigned char *)hdr + off;
  363. rlen = hdr_len - off;
  364. rpos = 0;
  365. }
  366. static int rb_read(unsigned char *dst, unsigned int n)
  367. {
  368. while (n) {
  369. unsigned int avail = rlen - rpos;
  370. if (!avail) {
  371. int r = u_tcp_srv_recv(rbuf, sizeof(rbuf), SOCK_MS);
  372. if (r <= 0)
  373. return -1;
  374. rptr = (unsigned char *)rbuf;
  375. rlen = (unsigned int)r;
  376. rpos = 0;
  377. continue;
  378. }
  379. {
  380. unsigned int k = avail < n ? avail : n;
  381. mem_copy(dst, rptr + rpos, k);
  382. rpos += k;
  383. dst += k;
  384. n -= k;
  385. }
  386. if (u_ticks_ms() - req_start > REQ_TIMEOUT_MS)
  387. return -1;
  388. }
  389. return 0;
  390. }
  391. static int rb_line(char *out, unsigned int cap)
  392. {
  393. unsigned int n = 0;
  394. for (;;) {
  395. unsigned char c;
  396. int r = rb_read(&c, 1);
  397. if (r != 0)
  398. return -1;
  399. if (c == '\n')
  400. break;
  401. if (n + 1 < cap)
  402. out[n++] = (char)c;
  403. else
  404. return -1;
  405. }
  406. if (n && out[n - 1] == '\r')
  407. n--;
  408. out[n] = '\0';
  409. return (int)n;
  410. }
  411. static int find_hdr_end(void)
  412. {
  413. unsigned int i;
  414. if (hdr_len < 4)
  415. return -1;
  416. for (i = 0; i + 3 < hdr_len; i++)
  417. if (hdr[i] == '\r' && hdr[i + 1] == '\n' &&
  418. hdr[i + 2] == '\r' && hdr[i + 3] == '\n')
  419. return (int)i;
  420. return -1;
  421. }
  422. /* A URL/file target -> absolute VFS path, no trailing slash, "" for root.
  423. * Accepts a path or an absolute URI (MOVE's Destination). */
  424. static int decode_path(const char *src, char *out, unsigned int cap)
  425. {
  426. const char *p = src;
  427. unsigned int o = 0;
  428. out[0] = '\0';
  429. if (p[0] != '/') {
  430. const char *q = p;
  431. while (*q && *q != ':' && *q != '/')
  432. q++;
  433. if (*q != ':')
  434. return -1;
  435. q++;
  436. while (*q == '/')
  437. q++;
  438. while (*q && *q != '/')
  439. q++;
  440. if (!*q) {
  441. out[0] = '\0';
  442. return 0;
  443. }
  444. p = q;
  445. }
  446. while (*p && *p != '?' && *p != '#') {
  447. char comp[PATH_MAX];
  448. unsigned int cl = 0;
  449. while (*p == '/')
  450. p++;
  451. while (*p && *p != '/' && *p != '?' && *p != '#') {
  452. int c = (unsigned char)*p++;
  453. if (c == '%') {
  454. int hi = p[0] ? hexval(p[0]) : -1;
  455. int lo = p[1] ? hexval(p[1]) : -1;
  456. if (hi < 0 || lo < 0)
  457. return -1;
  458. c = hi * 16 + lo;
  459. p += 2;
  460. if (c == 0 || c == '/')
  461. return -1;
  462. }
  463. if (cl + 1 >= sizeof(comp))
  464. return -1;
  465. comp[cl++] = (char)c;
  466. }
  467. comp[cl] = '\0';
  468. if (cl == 0 || (cl == 1 && comp[0] == '.'))
  469. continue;
  470. if (cl == 2 && comp[0] == '.' && comp[1] == '.') {
  471. while (o > 0 && out[o - 1] != '/')
  472. o--;
  473. if (o > 0)
  474. o--;
  475. out[o] = '\0';
  476. continue;
  477. }
  478. if (o + 1 + cl + 1 >= cap)
  479. return -1;
  480. out[o++] = '/';
  481. mem_copy(out + o, comp, cl);
  482. o += cl;
  483. out[o] = '\0';
  484. }
  485. return 0;
  486. }
  487. static void trim(char **s)
  488. {
  489. char *p = *s;
  490. unsigned int n;
  491. while (*p == ' ' || *p == '\t')
  492. p++;
  493. n = s_len(p);
  494. while (n && (p[n - 1] == ' ' || p[n - 1] == '\t'))
  495. p[--n] = '\0';
  496. *s = p;
  497. }
  498. /* Parse the request line and headers. Returns 0 or an HTTP error code. */
  499. static int parse_request(void)
  500. {
  501. char *line = hdr, *next;
  502. char *sp1, *sp2;
  503. /* request line */
  504. next = line;
  505. while (*next && *next != '\n')
  506. next++;
  507. if (*next == '\n')
  508. *next++ = '\0';
  509. if (s_len(line) && line[s_len(line) - 1] == '\r')
  510. line[s_len(line) - 1] = '\0';
  511. sp1 = line;
  512. while (*sp1 && *sp1 != ' ')
  513. sp1++;
  514. if (!*sp1)
  515. return 400;
  516. *sp1++ = '\0';
  517. sp2 = sp1;
  518. while (*sp2 && *sp2 != ' ')
  519. sp2++;
  520. if (!*sp2)
  521. return 400;
  522. *sp2++ = '\0';
  523. if (s_len(line) >= sizeof(rq.method))
  524. return 414;
  525. s_copy(rq.method, line, sizeof(rq.method));
  526. if (s_len(sp1) >= sizeof(rq.target))
  527. return 414;
  528. s_copy(rq.target, sp1, sizeof(rq.target));
  529. rq.http11 = s_eq(sp2, "HTTP/1.1");
  530. if (s_eq(sp1, "*")) {
  531. rq.path[0] = '\0';
  532. } else if (decode_path(sp1, rq.path, sizeof(rq.path)) != 0) {
  533. return 400;
  534. }
  535. /* headers */
  536. rq.depth = 1;
  537. rq.overwrite = 1;
  538. for (line = next; *line; ) {
  539. char *end = line;
  540. char *colon, *value;
  541. while (*end && *end != '\n')
  542. end++;
  543. if (*end == '\n')
  544. *end++ = '\0';
  545. if (s_len(line) && line[s_len(line) - 1] == '\r')
  546. line[s_len(line) - 1] = '\0';
  547. if (!line[0])
  548. break;
  549. colon = line;
  550. while (*colon && *colon != ':')
  551. colon++;
  552. if (!*colon) {
  553. line = end;
  554. continue;
  555. }
  556. *colon++ = '\0';
  557. value = colon;
  558. trim(&value);
  559. if (s_ieq(line, "Content-Length")) {
  560. unsigned int v = 0;
  561. char *q = value;
  562. if (!*q)
  563. return 400;
  564. while (*q) {
  565. if (*q < '0' || *q > '9')
  566. return 400;
  567. if (v > 0xFFFFFFFFu / 10)
  568. return 413;
  569. v = v * 10 + (unsigned int)(*q - '0');
  570. q++;
  571. }
  572. rq.have_len = 1;
  573. rq.content_len = v;
  574. } else if (s_ieq(line, "Transfer-Encoding")) {
  575. if (s_contains_ci(value, "chunked"))
  576. rq.chunked = 1;
  577. } else if (s_ieq(line, "Depth")) {
  578. if (value[0] == '0')
  579. rq.depth = 0;
  580. else if (value[0] == '1')
  581. rq.depth = 1;
  582. else
  583. rq.depth = 1; /* infinity: serve one level */
  584. } else if (s_ieq(line, "Destination")) {
  585. if (decode_path(value, rq.dest, sizeof(rq.dest)) == 0)
  586. rq.have_dest = 1;
  587. } else if (s_ieq(line, "Overwrite")) {
  588. rq.overwrite = !(value[0] == 'F' || value[0] == 'f');
  589. } else if (s_ieq(line, "Expect")) {
  590. if (s_contains_ci(value, "100-continue"))
  591. rq.expect_continue = 1;
  592. }
  593. line = end;
  594. }
  595. return 0;
  596. }
  597. static int read_request(void)
  598. {
  599. unsigned int start = u_ticks_ms();
  600. req_start = start;
  601. hdr_len = 0;
  602. rq.method[0] = '\0';
  603. rq.target[0] = '\0';
  604. rq.path[0] = '\0';
  605. rq.dest[0] = '\0';
  606. rq.http11 = 0;
  607. rq.have_len = 0;
  608. rq.content_len = 0;
  609. rq.chunked = 0;
  610. rq.depth = 1;
  611. rq.expect_continue = 0;
  612. rq.overwrite = 1;
  613. rq.have_dest = 0;
  614. for (;;) {
  615. int end = find_hdr_end();
  616. if (end >= 0) {
  617. hdr[end] = '\0';
  618. {
  619. int r = parse_request();
  620. if (r != 0)
  621. return r;
  622. }
  623. rb_init((unsigned int)end + 4);
  624. return 0;
  625. }
  626. if (hdr_len >= REQ_MAX)
  627. return 431;
  628. if (u_ticks_ms() - start > HDR_TIMEOUT_MS)
  629. return 408;
  630. {
  631. int n = u_tcp_srv_recv(hdr + hdr_len,
  632. REQ_MAX - hdr_len, SOCK_MS);
  633. if (n > 0) {
  634. hdr_len += (unsigned int)n;
  635. } else if (n == 0 || n == -2) {
  636. return hdr_len ? 400 : -2;
  637. } else if (hdr_len == 0 &&
  638. u_ticks_ms() - start > 2000u) {
  639. /* Speculative connection with no request. */
  640. return -2;
  641. }
  642. }
  643. }
  644. }
  645. /* ---- body reader ---- */
  646. /* 0 ok, -1 I/O/timeout, -2 larger than max. Handles Content-Length and
  647. * chunked transfer encoding. */
  648. static int read_content(unsigned char *dst, unsigned int max,
  649. unsigned int *out_len)
  650. {
  651. unsigned int off = 0;
  652. *out_len = 0;
  653. if (rq.chunked) {
  654. for (;;) {
  655. char line[32];
  656. unsigned int sz = 0, i;
  657. int ln = rb_line(line, sizeof(line));
  658. if (ln <= 0)
  659. return -1;
  660. for (i = 0; line[i] && line[i] != ';'; i++) {
  661. int v = hexval(line[i]);
  662. if (v < 0)
  663. return -1;
  664. if (sz > (0xFFFFFFFFu >> 4))
  665. return -2;
  666. sz = (sz << 4) | (unsigned int)v;
  667. }
  668. if (sz == 0) {
  669. for (;;) {
  670. char t[32];
  671. int tl = rb_line(t, sizeof(t));
  672. if (tl < 0)
  673. return -1;
  674. if (tl == 0)
  675. break;
  676. }
  677. break;
  678. }
  679. if (off > max || sz > max - off)
  680. return -2;
  681. if (rb_read(dst + off, sz) != 0)
  682. return -1;
  683. off += sz;
  684. {
  685. unsigned char crlf[2];
  686. if (rb_read(crlf, 2) != 0)
  687. return -1;
  688. if (crlf[0] != '\r' || crlf[1] != '\n')
  689. return -1;
  690. }
  691. }
  692. } else if (rq.have_len) {
  693. if (rq.content_len > max)
  694. return -2;
  695. if (rq.content_len && rb_read(dst, rq.content_len) != 0)
  696. return -1;
  697. off = rq.content_len;
  698. }
  699. *out_len = off;
  700. return 0;
  701. }
  702. /* Read and discard a (small) request body so the response does not leave
  703. * unread data behind on the connection. */
  704. static int drain_body(void)
  705. {
  706. unsigned int n;
  707. int r = read_content(body, body_max < 65536u ? body_max : 65536u, &n);
  708. if (r == -2)
  709. return send_error(413), -1;
  710. if (r != 0)
  711. return send_error(400), -1;
  712. return 0;
  713. }
  714. /* Read and discard n bytes (a refused Content-Length body). Responding 413
  715. * while the client is still writing would make it see a reset, which some
  716. * gvfs versions treat as a dead mount. */
  717. static void discard_body(unsigned int n)
  718. {
  719. unsigned char tmp[512];
  720. while (n) {
  721. unsigned int k = n > sizeof(tmp) ? sizeof(tmp) : n;
  722. if (rb_read(tmp, k) != 0)
  723. return;
  724. n -= k;
  725. }
  726. }
  727. /* ---- mounts and paths ---- */
  728. static void load_mounts(void)
  729. {
  730. mount_count = 0;
  731. while (mount_count < MAX_MOUNTS &&
  732. u_syscall2(SYS_LIST_MOUNTS, (int)mount_count,
  733. (int)&mounts[mount_count]) == 1)
  734. mount_count++;
  735. }
  736. static const char *vpath(const char *p)
  737. {
  738. return p[0] ? p : "/";
  739. }
  740. /* Split "/mount/rel" into the mount name and the relative path. Returns 0
  741. * for the root, 1 for a mount path, -1 for a malformed path. */
  742. static int split_mount(const char *p, char *mount, unsigned int cap,
  743. const char **rel)
  744. {
  745. unsigned int i = 1, n = 0;
  746. if (p[0] != '/')
  747. return -1;
  748. if (!p[1])
  749. return 0;
  750. while (p[i] && p[i] != '/') {
  751. if (n + 1 >= cap)
  752. return -1;
  753. mount[n++] = p[i++];
  754. }
  755. mount[n] = '\0';
  756. *rel = p[i] ? p + i + 1 : p + i;
  757. return 1;
  758. }
  759. static struct sys_mount *mount_by_name(const char *name)
  760. {
  761. unsigned int i;
  762. for (i = 0; i < mount_count; i++)
  763. if (s_eq(mounts[i].name, name))
  764. return &mounts[i];
  765. return 0;
  766. }
  767. static struct sys_mount *mount_of(const char *p)
  768. {
  769. char name[16];
  770. const char *rel;
  771. if (split_mount(p, name, sizeof(name), &rel) != 1)
  772. return 0;
  773. return mount_by_name(name);
  774. }
  775. static int path_ro(const char *p)
  776. {
  777. struct sys_mount *m = mount_of(p);
  778. return !m || m->ro;
  779. }
  780. static int path_on_fat(const char *p)
  781. {
  782. struct sys_mount *m = mount_of(p);
  783. return m && (s_ieq(m->type, "FAT12") || s_ieq(m->type, "FAT16") ||
  784. s_ieq(m->type, "FAT32"));
  785. }
  786. /* "/share", "/ramfs", "/floppy1": the mount itself, not a file in it. */
  787. static int is_mount_root(const char *p)
  788. {
  789. char name[16];
  790. const char *rel;
  791. return split_mount(p, name, sizeof(name), &rel) == 1 && rel[0] == '\0';
  792. }
  793. static const char *base_name(const char *p)
  794. {
  795. const char *b = p, *q = p;
  796. while (*q) {
  797. if (*q == '/')
  798. b = q + 1;
  799. q++;
  800. }
  801. return b;
  802. }
  803. static int join_path(const char *dir, const char *name, char *out,
  804. unsigned int cap)
  805. {
  806. unsigned int n = 0;
  807. if (dir[0]) {
  808. while (dir[n] && n + 1 < cap) {
  809. out[n] = dir[n];
  810. n++;
  811. }
  812. }
  813. if (n + 1 >= cap)
  814. return -1;
  815. out[n++] = '/';
  816. {
  817. unsigned int i = 0;
  818. while (name[i] && n + 1 < cap)
  819. out[n++] = name[i++];
  820. }
  821. out[n] = '\0';
  822. return 0;
  823. }
  824. static void parent_path(const char *p, char *out, unsigned int cap)
  825. {
  826. int i = (int)s_len(p) - 1;
  827. while (i >= 0 && p[i] != '/')
  828. i--;
  829. if (i <= 0) {
  830. out[0] = '\0';
  831. return;
  832. }
  833. if ((unsigned int)i >= cap)
  834. i = (int)cap - 1;
  835. {
  836. int j;
  837. for (j = 0; j < i; j++)
  838. out[j] = p[j];
  839. out[i] = '\0';
  840. }
  841. }
  842. /* FAT accepts names that fat_name_to_8_3() can represent without silently
  843. * truncating: 1..8 base chars, optional 0..3 extension chars. */
  844. static int fat_char_ok(char c)
  845. {
  846. if (c >= 'A' && c <= 'Z')
  847. return 1;
  848. if (c >= 'a' && c <= 'z')
  849. return 1;
  850. if (c >= '0' && c <= '9')
  851. return 1;
  852. switch (c) {
  853. case '!': case '#': case '$': case '%': case '&': case '\'':
  854. case '(': case ')': case '-': case '@': case '^': case '_':
  855. case '`': case '{': case '}': case '~':
  856. return 1;
  857. }
  858. return 0;
  859. }
  860. static int fat_name_ok(const char *name)
  861. {
  862. unsigned int i, base = 0, ext = 0;
  863. int dot = 0;
  864. for (i = 0; name[i]; i++) {
  865. if (name[i] == '.') {
  866. if (dot)
  867. return 0;
  868. dot = 1;
  869. continue;
  870. }
  871. if (!fat_char_ok(name[i]))
  872. return 0;
  873. if (dot)
  874. ext++;
  875. else
  876. base++;
  877. }
  878. if (base == 0 || base > 8 || ext > 3)
  879. return 0;
  880. return 1;
  881. }
  882. /* The last component must be representable when creating a FAT name. */
  883. static int fat_path_ok(const char *p)
  884. {
  885. const char *b = base_name(p);
  886. return *b && fat_name_ok(b);
  887. }
  888. static const char *content_type(const char *p)
  889. {
  890. const char *d = p, *q = p;
  891. while (*q) {
  892. if (*q == '.')
  893. d = q;
  894. q++;
  895. }
  896. if (!*d)
  897. return "application/octet-stream";
  898. if (s_ieq(d, ".txt") || s_ieq(d, ".md") || s_ieq(d, ".c") ||
  899. s_ieq(d, ".h") || s_ieq(d, ".log") || s_ieq(d, ".ini"))
  900. return "text/plain; charset=utf-8";
  901. if (s_ieq(d, ".html") || s_ieq(d, ".htm"))
  902. return "text/html; charset=utf-8";
  903. if (s_ieq(d, ".png"))
  904. return "image/png";
  905. if (s_ieq(d, ".jpg") || s_ieq(d, ".jpeg"))
  906. return "image/jpeg";
  907. if (s_ieq(d, ".gif"))
  908. return "image/gif";
  909. if (s_ieq(d, ".bmp"))
  910. return "image/bmp";
  911. if (s_ieq(d, ".tga"))
  912. return "image/x-tga";
  913. if (s_ieq(d, ".pnm") || s_ieq(d, ".ppm") || s_ieq(d, ".pgm"))
  914. return "image/x-portable-anymap";
  915. return "application/octet-stream";
  916. }
  917. static int http_from_vfs(int r)
  918. {
  919. switch (r) {
  920. case SYS_EROFS: return 403;
  921. case SYS_ENOENT: return 404;
  922. case SYS_ENODEV: return 404;
  923. case SYS_ENOFS: return 404;
  924. case SYS_EINVAL: return 409;
  925. case SYS_ENOSPC: return 507;
  926. case SYS_EWRITE: return 507;
  927. default: return 500;
  928. }
  929. }
  930. /* ---- WebDAV helpers ---- */
  931. static int emit_prop(const char *p, int is_dir, unsigned int size,
  932. const char *display)
  933. {
  934. char href[PATH_MAX * 3];
  935. url_escape(p[0] ? p : "/", href, sizeof(href));
  936. if (is_dir && href[s_len(href) - 1] != '/') {
  937. unsigned int n = s_len(href);
  938. if (n + 1 < sizeof(href)) {
  939. href[n++] = '/';
  940. href[n] = '\0';
  941. }
  942. }
  943. xml_len = 0;
  944. xa("<D:response><D:href>");
  945. xa_esc(href);
  946. xa("</D:href><D:propstat><D:prop><D:resourcetype>");
  947. if (is_dir)
  948. xa("<D:collection/>");
  949. xa("</D:resourcetype>");
  950. if (!is_dir) {
  951. xa("<D:getcontentlength>");
  952. xa_dec(size);
  953. xa("</D:getcontentlength><D:getcontenttype>");
  954. xa_esc(content_type(p));
  955. xa("</D:getcontenttype>");
  956. }
  957. xa("<D:displayname>");
  958. xa_esc(display);
  959. xa("</D:displayname>");
  960. xa("<D:getlastmodified>Thu, 01 Jan 2026 00:00:00 GMT</D:getlastmodified>");
  961. xa("</D:prop><D:status>HTTP/1.1 200 OK</D:status></D:propstat>");
  962. xa("</D:response>");
  963. return chunk_send(xml, xml_len);
  964. }
  965. static int list_child(unsigned int index, const char *dir, char *out,
  966. int *is_dir, unsigned int *size)
  967. {
  968. struct sys_dirent d;
  969. char disp[64];
  970. if (u_syscall3(SYS_LIST_DIR, (int)vpath(dir), (int)index,
  971. (int)&d) != 1)
  972. return 0;
  973. if (d.raw_83)
  974. u_fat_name(d.name, disp, sizeof(disp));
  975. else
  976. s_copy(disp, d.name, sizeof(disp));
  977. if (!disp[0])
  978. return 0;
  979. if (join_path(dir, disp, out, PATH_MAX) != 0)
  980. return 0;
  981. *is_dir = (int)d.is_dir;
  982. *size = d.size;
  983. return 1;
  984. }
  985. /* ---- method handlers ---- */
  986. static void do_options2(void)
  987. {
  988. resp_len = 0;
  989. ra("HTTP/1.1 200 OK\r\nServer: Nebs OS/0.2.0\r\n");
  990. ra("DAV: 1, 2\r\nMS-Author-Via: DAV\r\n");
  991. ra("Allow: OPTIONS, GET, HEAD, PUT, DELETE, MKCOL, PROPFIND, "
  992. "PROPPATCH, MOVE, LOCK, UNLOCK\r\n");
  993. ra("Connection: close\r\nCache-Control: no-store\r\n");
  994. ra("Content-Length: 0\r\n\r\n");
  995. status = 200;
  996. u_tcp_srv_send(resp, resp_len);
  997. }
  998. static void do_get(int head)
  999. {
  1000. struct sys_stat st;
  1001. unsigned int off;
  1002. if (!rq.path[0]) {
  1003. send_error(405);
  1004. return;
  1005. }
  1006. if (u_stat(vpath(rq.path), &st) != 0) {
  1007. send_error(404);
  1008. return;
  1009. }
  1010. if (st.is_dir) {
  1011. send_error(405);
  1012. return;
  1013. }
  1014. if (st.size > body_max) {
  1015. send_error(413);
  1016. return;
  1017. }
  1018. {
  1019. int n = u_syscall3(SYS_READ_FILE, (int)rq.path, (int)body,
  1020. (int)body_max);
  1021. if (n < 0) {
  1022. send_error(http_from_vfs(n));
  1023. return;
  1024. }
  1025. if (send_headers(200, "OK", content_type(rq.path),
  1026. (unsigned int)n) != 0)
  1027. return;
  1028. if (head)
  1029. return;
  1030. for (off = 0; off < (unsigned int)n; ) {
  1031. unsigned int k = (unsigned int)n - off;
  1032. if (k > 65536u)
  1033. k = 65536u;
  1034. if (u_tcp_srv_send(body + off, k) != 0)
  1035. return;
  1036. off += k;
  1037. }
  1038. }
  1039. }
  1040. static void do_put(void)
  1041. {
  1042. struct sys_stat st;
  1043. char parent[PATH_MAX];
  1044. unsigned int len;
  1045. int existed = 0, r;
  1046. if (!rq.path[0]) {
  1047. send_error(403);
  1048. return;
  1049. }
  1050. if (!rq.have_len && !rq.chunked) {
  1051. send_error(411);
  1052. return;
  1053. }
  1054. if (rq.have_len && rq.content_len > body_max) {
  1055. /* Drain a body that is already on the wire (not when the client
  1056. * is still waiting for 100 Continue), then refuse cleanly. */
  1057. if (!rq.expect_continue &&
  1058. rq.content_len <= 64u * 1024u * 1024u)
  1059. discard_body(rq.content_len);
  1060. send_error(413);
  1061. return;
  1062. }
  1063. if (path_ro(rq.path)) {
  1064. send_error(403);
  1065. return;
  1066. }
  1067. if (path_on_fat(rq.path) && !fat_path_ok(rq.path)) {
  1068. send_refuse(409, "FAT name is not 8.3 (use /share)");
  1069. return;
  1070. }
  1071. parent_path(rq.path, parent, sizeof(parent));
  1072. if (parent[0]) {
  1073. if (u_stat(vpath(parent), &st) != 0 || !st.is_dir) {
  1074. send_refuse(409, "PUT parent directory is missing");
  1075. return;
  1076. }
  1077. }
  1078. if (u_stat(vpath(rq.path), &st) == 0) {
  1079. if (st.is_dir) {
  1080. send_refuse(409, "PUT target is a directory");
  1081. return;
  1082. }
  1083. existed = 1;
  1084. }
  1085. if (rq.expect_continue && rq.http11)
  1086. u_tcp_srv_send("HTTP/1.1 100 Continue\r\n\r\n", 25);
  1087. r = read_content(body, body_max, &len);
  1088. if (r == -2) {
  1089. send_error(413);
  1090. return;
  1091. }
  1092. if (r != 0) {
  1093. send_error(400);
  1094. return;
  1095. }
  1096. r = u_syscall3(SYS_WRITE_FILE, (int)rq.path, (int)body, (int)len);
  1097. if (r != 0) {
  1098. u_puts("websrv: write failed code ");
  1099. u_putdec((unsigned int)-r);
  1100. u_putc('\n');
  1101. send_error(http_from_vfs(r));
  1102. return;
  1103. }
  1104. send_empty(existed ? 204 : 201);
  1105. }
  1106. static void do_mkcol(void)
  1107. {
  1108. struct sys_stat st;
  1109. char parent[PATH_MAX];
  1110. int r;
  1111. if (!rq.path[0] || path_ro(rq.path)) {
  1112. send_error(403);
  1113. return;
  1114. }
  1115. if (path_on_fat(rq.path) && !fat_path_ok(rq.path)) {
  1116. send_refuse(409, "MKCOL FAT name is not 8.3 (use /share)");
  1117. return;
  1118. }
  1119. if (u_stat(vpath(rq.path), &st) == 0) {
  1120. send_error(405);
  1121. return;
  1122. }
  1123. parent_path(rq.path, parent, sizeof(parent));
  1124. if (parent[0]) {
  1125. if (u_stat(vpath(parent), &st) != 0 || !st.is_dir) {
  1126. send_refuse(409, "MKCOL parent directory is missing");
  1127. return;
  1128. }
  1129. }
  1130. r = u_syscall1(SYS_MKDIR, (int)rq.path);
  1131. if (r != 0) {
  1132. u_puts("websrv: mkdir failed code ");
  1133. u_putdec((unsigned int)-r);
  1134. u_putc('\n');
  1135. send_error(http_from_vfs(r));
  1136. return;
  1137. }
  1138. send_empty(201);
  1139. }
  1140. static int delete_tree(const char *p, unsigned int depth)
  1141. {
  1142. struct sys_stat st;
  1143. unsigned int i;
  1144. int r;
  1145. if (depth > 8)
  1146. return SYS_EINVAL;
  1147. if (u_stat(vpath(p), &st) != 0)
  1148. return SYS_ENOENT;
  1149. if (st.is_dir) {
  1150. for (i = 0; ; i++) {
  1151. char child[PATH_MAX];
  1152. int is_dir;
  1153. unsigned int size;
  1154. if (!list_child(i, p, child, &is_dir, &size))
  1155. break;
  1156. r = delete_tree(child, depth + 1);
  1157. if (r != 0)
  1158. return r;
  1159. }
  1160. return u_syscall1(SYS_RMDIR, (int)p);
  1161. }
  1162. return u_syscall1(SYS_UNLINK, (int)p);
  1163. }
  1164. static void do_delete(void)
  1165. {
  1166. int r;
  1167. if (!rq.path[0] || is_mount_root(rq.path) || path_ro(rq.path)) {
  1168. send_error(403);
  1169. return;
  1170. }
  1171. r = delete_tree(rq.path, 0);
  1172. if (r != 0) {
  1173. send_error(http_from_vfs(r));
  1174. return;
  1175. }
  1176. send_empty(204);
  1177. }
  1178. static void do_move(void)
  1179. {
  1180. struct sys_stat st, dst;
  1181. unsigned int len;
  1182. int existed = 0, r;
  1183. if (!rq.path[0] || path_ro(rq.path)) {
  1184. send_error(403);
  1185. return;
  1186. }
  1187. if (!rq.have_dest || !rq.dest[0]) {
  1188. send_error(400);
  1189. return;
  1190. }
  1191. if (s_eq(rq.path, rq.dest)) {
  1192. send_empty(204);
  1193. return;
  1194. }
  1195. if (path_ro(rq.dest)) {
  1196. send_error(403);
  1197. return;
  1198. }
  1199. if (path_on_fat(rq.dest) && !fat_path_ok(rq.dest)) {
  1200. send_refuse(409, "MOVE destination FAT name is not 8.3");
  1201. return;
  1202. }
  1203. if (u_stat(vpath(rq.path), &st) != 0) {
  1204. send_error(404);
  1205. return;
  1206. }
  1207. if (st.is_dir) {
  1208. send_error(501); /* directory renames are not supported */
  1209. return;
  1210. }
  1211. if (st.size > body_max) {
  1212. send_error(413);
  1213. return;
  1214. }
  1215. {
  1216. char parent[PATH_MAX];
  1217. parent_path(rq.dest, parent, sizeof(parent));
  1218. if (parent[0] &&
  1219. (u_stat(vpath(parent), &dst) != 0 || !dst.is_dir)) {
  1220. send_refuse(409, "MOVE destination parent is missing");
  1221. return;
  1222. }
  1223. }
  1224. if (u_stat(vpath(rq.dest), &dst) == 0) {
  1225. if (dst.is_dir) {
  1226. send_refuse(409, "MOVE destination is a directory");
  1227. return;
  1228. }
  1229. if (!rq.overwrite) {
  1230. send_error(412);
  1231. return;
  1232. }
  1233. existed = 1;
  1234. }
  1235. r = u_syscall3(SYS_READ_FILE, (int)rq.path, (int)body,
  1236. (int)body_max);
  1237. if (r < 0) {
  1238. send_error(http_from_vfs(r));
  1239. return;
  1240. }
  1241. len = (unsigned int)r;
  1242. r = u_syscall3(SYS_WRITE_FILE, (int)rq.dest, (int)body, (int)len);
  1243. if (r != 0) {
  1244. send_error(http_from_vfs(r));
  1245. return;
  1246. }
  1247. r = u_syscall1(SYS_UNLINK, (int)rq.path);
  1248. if (r != 0 && r != SYS_ENOENT) {
  1249. send_error(http_from_vfs(r));
  1250. return;
  1251. }
  1252. send_empty(existed ? 204 : 201);
  1253. }
  1254. static void do_propfind(void)
  1255. {
  1256. struct sys_stat st;
  1257. unsigned int i;
  1258. if (drain_body() != 0)
  1259. return;
  1260. if (!rq.path[0]) {
  1261. if (send_multistatus_begin() != 0)
  1262. return;
  1263. if (emit_prop("", 1, 0, "/") != 0) {
  1264. end_multistatus();
  1265. return;
  1266. }
  1267. if (rq.depth) {
  1268. for (i = 0; i < mount_count; i++) {
  1269. char mpath[32];
  1270. if (join_path("", mounts[i].name, mpath,
  1271. sizeof(mpath)) != 0)
  1272. continue;
  1273. if (emit_prop(mpath, 1, 0, mounts[i].name) != 0)
  1274. break;
  1275. }
  1276. }
  1277. end_multistatus();
  1278. return;
  1279. }
  1280. if (u_stat(vpath(rq.path), &st) != 0) {
  1281. send_error(404);
  1282. return;
  1283. }
  1284. if (send_multistatus_begin() != 0)
  1285. return;
  1286. if (emit_prop(rq.path, st.is_dir, st.size, base_name(rq.path)) != 0) {
  1287. end_multistatus();
  1288. return;
  1289. }
  1290. if (st.is_dir && rq.depth) {
  1291. for (i = 0; ; i++) {
  1292. char child[PATH_MAX];
  1293. int is_dir;
  1294. unsigned int size;
  1295. if (!list_child(i, rq.path, child, &is_dir, &size))
  1296. break;
  1297. if (emit_prop(child, is_dir, size,
  1298. base_name(child)) != 0)
  1299. break;
  1300. }
  1301. }
  1302. end_multistatus();
  1303. }
  1304. static void do_proppatch(void)
  1305. {
  1306. char href[PATH_MAX * 3];
  1307. unsigned int n = 0;
  1308. if (drain_body() != 0)
  1309. return;
  1310. url_escape(rq.path[0] ? rq.path : "/", href, sizeof(href));
  1311. xml_len = 0;
  1312. xa("<?xml version=\"1.0\" encoding=\"utf-8\"?>");
  1313. xa("<D:multistatus xmlns:D=\"DAV:\"><D:response><D:href>");
  1314. xa_esc(href);
  1315. xa("</D:href><D:propstat><D:prop/>"
  1316. "<D:status>HTTP/1.1 200 OK</D:status></D:propstat>"
  1317. "</D:response></D:multistatus>");
  1318. n = xml_len;
  1319. send_body(207, "Multi-Status", "text/xml; charset=\"utf-8\"",
  1320. xml, n);
  1321. }
  1322. static void do_lock(void)
  1323. {
  1324. static const char lockbody[] =
  1325. "<?xml version=\"1.0\" encoding=\"utf-8\"?>"
  1326. "<D:prop xmlns:D=\"DAV:\"><D:lockdiscovery><D:activelock>"
  1327. "<D:locktype><D:write/></D:locktype>"
  1328. "<D:depth>infinity</D:depth>"
  1329. "<D:timeout>Second-3600</D:timeout>"
  1330. "<D:locktoken><D:href>opaquelocktoken:nebs</D:href>"
  1331. "</D:locktoken></D:activelock></D:lockdiscovery></D:prop>";
  1332. (void)drain_body();
  1333. extra_hdrs = "Lock-Token: <opaquelocktoken:nebs>";
  1334. send_body(200, "OK", "text/xml; charset=\"utf-8\"",
  1335. lockbody, sizeof(lockbody) - 1);
  1336. }
  1337. /* ---- request dispatch ---- */
  1338. static void serve(void)
  1339. {
  1340. int r = read_request();
  1341. status = 0;
  1342. extra_hdrs = 0;
  1343. if (r == -2)
  1344. return;
  1345. if (r != 0) {
  1346. send_error(r);
  1347. u_puts("websrv: bad request (");
  1348. u_putdec((unsigned int)r);
  1349. u_puts(")\n");
  1350. return;
  1351. }
  1352. load_mounts();
  1353. if (s_eq(rq.method, "OPTIONS"))
  1354. do_options2();
  1355. else if (s_eq(rq.method, "GET"))
  1356. do_get(0);
  1357. else if (s_eq(rq.method, "HEAD"))
  1358. do_get(1);
  1359. else if (s_eq(rq.method, "PUT"))
  1360. do_put();
  1361. else if (s_eq(rq.method, "DELETE"))
  1362. do_delete();
  1363. else if (s_eq(rq.method, "MKCOL"))
  1364. do_mkcol();
  1365. else if (s_eq(rq.method, "PROPFIND"))
  1366. do_propfind();
  1367. else if (s_eq(rq.method, "PROPPATCH"))
  1368. do_proppatch();
  1369. else if (s_eq(rq.method, "MOVE"))
  1370. do_move();
  1371. else if (s_eq(rq.method, "LOCK"))
  1372. do_lock();
  1373. else if (s_eq(rq.method, "UNLOCK")) {
  1374. (void)drain_body();
  1375. send_empty(204);
  1376. } else {
  1377. (void)drain_body();
  1378. send_error(405);
  1379. }
  1380. if (status) {
  1381. u_puts("websrv: ");
  1382. u_puts(rq.method);
  1383. u_putc(' ');
  1384. u_puts(rq.path[0] ? rq.path : "/");
  1385. u_puts(" -> ");
  1386. u_putdec((unsigned int)status);
  1387. u_putc('\n');
  1388. }
  1389. }
  1390. static unsigned int parse_port(const char *arg)
  1391. {
  1392. unsigned int p = 0;
  1393. if (!arg)
  1394. return 0;
  1395. while (*arg) {
  1396. if (*arg < '0' || *arg > '9')
  1397. return 0;
  1398. p = p * 10 + (unsigned int)(*arg - '0');
  1399. if (p > 65535)
  1400. return 0;
  1401. arg++;
  1402. }
  1403. return p;
  1404. }
  1405. void _start(const char *arg)
  1406. {
  1407. unsigned int port = parse_port(arg);
  1408. unsigned char ip[4];
  1409. if (!port)
  1410. port = 8080;
  1411. if (u_syscall0(SYS_NET_ACQUIRE) != 0) {
  1412. u_puts("websrv: network busy (another command owns the NIC)\n");
  1413. u_exit(1);
  1414. }
  1415. u_syscall1(SYS_NET_GET_IP, (int)ip);
  1416. body = u_mem_alloc(BODY_MAX);
  1417. body_max = BODY_MAX;
  1418. if (!body) {
  1419. body = u_mem_alloc(BODY_FALLBACK);
  1420. body_max = BODY_FALLBACK;
  1421. }
  1422. if (!body) {
  1423. u_puts("websrv: no memory for the transfer buffer\n");
  1424. u_syscall0(SYS_NET_RELEASE);
  1425. u_exit(1);
  1426. }
  1427. load_mounts();
  1428. if (u_tcp_listen(port) != 0) {
  1429. u_puts("websrv: cannot listen\n");
  1430. u_syscall0(SYS_NET_RELEASE);
  1431. u_exit(1);
  1432. }
  1433. u_puts("websrv: transfer buffer ");
  1434. u_putdec(body_max / 1024u);
  1435. u_puts(" KB\n");
  1436. u_puts("websrv: serving http://");
  1437. u_put_ip(ip);
  1438. u_putc(':');
  1439. u_putdec(port);
  1440. u_puts("/ (WebDAV: dav://");
  1441. u_put_ip(ip);
  1442. u_putc(':');
  1443. u_putdec(port);
  1444. u_puts("/)\n");
  1445. for (;;) {
  1446. int r = u_tcp_accept();
  1447. if (r == 1) {
  1448. unsigned char peer[4];
  1449. unsigned int pport = 0;
  1450. if (u_tcp_peer(peer, &pport) == 0) {
  1451. u_puts("websrv: connection from ");
  1452. u_put_ip(peer);
  1453. u_putc(':');
  1454. u_putdec(pport);
  1455. u_putc('\n');
  1456. }
  1457. serve();
  1458. u_tcp_srv_close();
  1459. continue;
  1460. }
  1461. if (r == -3) {
  1462. u_puts("websrv: netkexec requested, stopping\n");
  1463. break;
  1464. }
  1465. if (r == -2)
  1466. break;
  1467. u_sleep_ms(2);
  1468. }
  1469. u_tcp_srv_close();
  1470. u_syscall0(SYS_NET_RELEASE);
  1471. u_puts("websrv: stopped\n");
  1472. u_exit(0);
  1473. }