server.c 68 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2018 Andy Green <[email protected]>
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation:
  9. * version 2.1 of the License.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  19. * MA 02110-1301 USA
  20. */
  21. #include "core/private.h"
  22. const char * const method_names[] = {
  23. "GET", "POST", "OPTIONS", "PUT", "PATCH", "DELETE", "CONNECT", "HEAD",
  24. #ifdef LWS_WITH_HTTP2
  25. ":path",
  26. #endif
  27. };
  28. static const char * const intermediates[] = { "private", "public" };
  29. /*
  30. * return 0: all done
  31. * 1: nonfatal error
  32. * <0: fatal error
  33. *
  34. * REQUIRES CONTEXT LOCK HELD
  35. */
  36. int
  37. _lws_vhost_init_server(const struct lws_context_creation_info *info,
  38. struct lws_vhost *vhost)
  39. {
  40. int n, opt = 1, limit = 1;
  41. lws_sockfd_type sockfd;
  42. struct lws_vhost *vh;
  43. struct lws *wsi;
  44. int m = 0, is;
  45. (void)method_names;
  46. (void)opt;
  47. if (info) {
  48. vhost->iface = info->iface;
  49. vhost->listen_port = info->port;
  50. }
  51. /* set up our external listening socket we serve on */
  52. if (vhost->listen_port == CONTEXT_PORT_NO_LISTEN ||
  53. vhost->listen_port == CONTEXT_PORT_NO_LISTEN_SERVER)
  54. return 0;
  55. vh = vhost->context->vhost_list;
  56. while (vh) {
  57. if (vh->listen_port == vhost->listen_port) {
  58. if (((!vhost->iface && !vh->iface) ||
  59. (vhost->iface && vh->iface &&
  60. !strcmp(vhost->iface, vh->iface))) &&
  61. vh->lserv_wsi
  62. ) {
  63. lwsl_notice(" using listen skt from vhost %s\n",
  64. vh->name);
  65. return 0;
  66. }
  67. }
  68. vh = vh->vhost_next;
  69. }
  70. if (vhost->iface) {
  71. /*
  72. * let's check before we do anything else about the disposition
  73. * of the interface he wants to bind to...
  74. */
  75. is = lws_socket_bind(vhost, LWS_SOCK_INVALID, vhost->listen_port,
  76. vhost->iface);
  77. lwsl_debug("initial if check says %d\n", is);
  78. if (is == LWS_ITOSA_BUSY)
  79. /* treat as fatal */
  80. return -1;
  81. deal:
  82. lws_start_foreach_llp(struct lws_vhost **, pv,
  83. vhost->context->no_listener_vhost_list) {
  84. if (is >= LWS_ITOSA_USABLE && *pv == vhost) {
  85. /* on the list and shouldn't be: remove it */
  86. lwsl_debug("deferred iface: removing vh %s\n",
  87. (*pv)->name);
  88. *pv = vhost->no_listener_vhost_list;
  89. vhost->no_listener_vhost_list = NULL;
  90. goto done_list;
  91. }
  92. if (is < LWS_ITOSA_USABLE && *pv == vhost)
  93. goto done_list;
  94. } lws_end_foreach_llp(pv, no_listener_vhost_list);
  95. /* not on the list... */
  96. if (is < LWS_ITOSA_USABLE) {
  97. /* ... but needs to be: so add it */
  98. lwsl_debug("deferred iface: adding vh %s\n", vhost->name);
  99. vhost->no_listener_vhost_list =
  100. vhost->context->no_listener_vhost_list;
  101. vhost->context->no_listener_vhost_list = vhost;
  102. }
  103. done_list:
  104. switch (is) {
  105. default:
  106. break;
  107. case LWS_ITOSA_NOT_EXIST:
  108. /* can't add it */
  109. if (info) /* first time */
  110. lwsl_err("VH %s: iface %s port %d DOESN'T EXIST\n",
  111. vhost->name, vhost->iface, vhost->listen_port);
  112. return 1;
  113. case LWS_ITOSA_NOT_USABLE:
  114. /* can't add it */
  115. if (info) /* first time */
  116. lwsl_err("VH %s: iface %s port %d NOT USABLE\n",
  117. vhost->name, vhost->iface, vhost->listen_port);
  118. return 1;
  119. }
  120. }
  121. (void)n;
  122. #if defined(__linux__)
  123. #ifdef LWS_WITH_UNIX_SOCK
  124. /*
  125. * A Unix domain sockets cannot be bound for several times, even if we set
  126. * the SO_REUSE* options on.
  127. * However, fortunately, each thread is able to independently listen when
  128. * running on a reasonably new Linux kernel. So we can safely assume
  129. * creating just one listening socket for a multi-threaded environment won't
  130. * fail in most cases.
  131. */
  132. if (!LWS_UNIX_SOCK_ENABLED(vhost))
  133. #endif
  134. limit = vhost->context->count_threads;
  135. #endif
  136. for (m = 0; m < limit; m++) {
  137. #ifdef LWS_WITH_UNIX_SOCK
  138. if (LWS_UNIX_SOCK_ENABLED(vhost))
  139. sockfd = socket(AF_UNIX, SOCK_STREAM, 0);
  140. else
  141. #endif
  142. #ifdef LWS_WITH_IPV6
  143. if (LWS_IPV6_ENABLED(vhost))
  144. sockfd = socket(AF_INET6, SOCK_STREAM, 0);
  145. else
  146. #endif
  147. sockfd = socket(AF_INET, SOCK_STREAM, 0);
  148. if (sockfd == LWS_SOCK_INVALID) {
  149. lwsl_err("ERROR opening socket\n");
  150. return 1;
  151. }
  152. #if !defined(LWS_WITH_ESP32)
  153. #if (defined(WIN32) || defined(_WIN32)) && defined(SO_EXCLUSIVEADDRUSE)
  154. /*
  155. * only accept that we are the only listener on the port
  156. * https://msdn.microsoft.com/zh-tw/library/
  157. * windows/desktop/ms740621(v=vs.85).aspx
  158. *
  159. * for lws, to match Linux, we default to exclusive listen
  160. */
  161. if (!lws_check_opt(vhost->options,
  162. LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE)) {
  163. if (setsockopt(sockfd, SOL_SOCKET, SO_EXCLUSIVEADDRUSE,
  164. (const void *)&opt, sizeof(opt)) < 0) {
  165. lwsl_err("reuseaddr failed\n");
  166. compatible_close(sockfd);
  167. return -1;
  168. }
  169. } else
  170. #endif
  171. /*
  172. * allow us to restart even if old sockets in TIME_WAIT
  173. */
  174. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
  175. (const void *)&opt, sizeof(opt)) < 0) {
  176. lwsl_err("reuseaddr failed\n");
  177. compatible_close(sockfd);
  178. return -1;
  179. }
  180. #if defined(LWS_WITH_IPV6) && defined(IPV6_V6ONLY)
  181. if (LWS_IPV6_ENABLED(vhost) &&
  182. vhost->options & LWS_SERVER_OPTION_IPV6_V6ONLY_MODIFY) {
  183. int value = (vhost->options &
  184. LWS_SERVER_OPTION_IPV6_V6ONLY_VALUE) ? 1 : 0;
  185. if (setsockopt(sockfd, IPPROTO_IPV6, IPV6_V6ONLY,
  186. (const void*)&value, sizeof(value)) < 0) {
  187. compatible_close(sockfd);
  188. return -1;
  189. }
  190. }
  191. #endif
  192. #if defined(__linux__) && defined(SO_REUSEPORT)
  193. /* keep coverity happy */
  194. #if LWS_MAX_SMP > 1
  195. n = 1;
  196. #else
  197. n = lws_check_opt(vhost->options,
  198. LWS_SERVER_OPTION_ALLOW_LISTEN_SHARE);
  199. #endif
  200. if (n && vhost->context->count_threads > 1)
  201. if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEPORT,
  202. (const void *)&opt, sizeof(opt)) < 0) {
  203. compatible_close(sockfd);
  204. return -1;
  205. }
  206. #endif
  207. #endif
  208. lws_plat_set_socket_options(vhost, sockfd, 0);
  209. is = lws_socket_bind(vhost, sockfd, vhost->listen_port, vhost->iface);
  210. if (is == LWS_ITOSA_BUSY) {
  211. /* treat as fatal */
  212. compatible_close(sockfd);
  213. return -1;
  214. }
  215. /*
  216. * There is a race where the network device may come up and then
  217. * go away and fail here. So correctly handle unexpected failure
  218. * here despite we earlier confirmed it.
  219. */
  220. if (is < 0) {
  221. lwsl_info("%s: lws_socket_bind says %d\n", __func__, is);
  222. compatible_close(sockfd);
  223. goto deal;
  224. }
  225. wsi = lws_zalloc(sizeof(struct lws), "listen wsi");
  226. if (wsi == NULL) {
  227. lwsl_err("Out of mem\n");
  228. goto bail;
  229. }
  230. #ifdef LWS_WITH_UNIX_SOCK
  231. if (!LWS_UNIX_SOCK_ENABLED(vhost))
  232. #endif
  233. {
  234. wsi->unix_skt = 1;
  235. vhost->listen_port = is;
  236. lwsl_debug("%s: lws_socket_bind says %d\n", __func__, is);
  237. }
  238. wsi->context = vhost->context;
  239. wsi->desc.sockfd = sockfd;
  240. lws_role_transition(wsi, 0, LRS_UNCONNECTED, &role_ops_listen);
  241. wsi->protocol = vhost->protocols;
  242. wsi->tsi = m;
  243. lws_vhost_bind_wsi(vhost, wsi);
  244. wsi->listener = 1;
  245. if (wsi->context->event_loop_ops->init_vhost_listen_wsi)
  246. wsi->context->event_loop_ops->init_vhost_listen_wsi(wsi);
  247. if (__insert_wsi_socket_into_fds(vhost->context, wsi)) {
  248. lwsl_notice("inserting wsi socket into fds failed\n");
  249. goto bail;
  250. }
  251. vhost->context->count_wsi_allocated++;
  252. vhost->lserv_wsi = wsi;
  253. n = listen(wsi->desc.sockfd, LWS_SOMAXCONN);
  254. if (n < 0) {
  255. lwsl_err("listen failed with error %d\n", LWS_ERRNO);
  256. vhost->lserv_wsi = NULL;
  257. vhost->context->count_wsi_allocated--;
  258. __remove_wsi_socket_from_fds(wsi);
  259. goto bail;
  260. }
  261. } /* for each thread able to independently listen */
  262. if (!lws_check_opt(vhost->context->options, LWS_SERVER_OPTION_EXPLICIT_VHOSTS)) {
  263. #ifdef LWS_WITH_UNIX_SOCK
  264. if (LWS_UNIX_SOCK_ENABLED(vhost))
  265. lwsl_info(" Listening on \"%s\"\n", vhost->iface);
  266. else
  267. #endif
  268. lwsl_info(" Listening on port %d\n", vhost->listen_port);
  269. }
  270. // info->port = vhost->listen_port;
  271. return 0;
  272. bail:
  273. compatible_close(sockfd);
  274. return -1;
  275. }
  276. struct lws_vhost *
  277. lws_select_vhost(struct lws_context *context, int port, const char *servername)
  278. {
  279. struct lws_vhost *vhost = context->vhost_list;
  280. const char *p;
  281. int n, colon;
  282. n = (int)strlen(servername);
  283. colon = n;
  284. p = strchr(servername, ':');
  285. if (p)
  286. colon = lws_ptr_diff(p, servername);
  287. /* Priotity 1: first try exact matches */
  288. while (vhost) {
  289. if (port == vhost->listen_port &&
  290. !strncmp(vhost->name, servername, colon)) {
  291. lwsl_info("SNI: Found: %s\n", servername);
  292. return vhost;
  293. }
  294. vhost = vhost->vhost_next;
  295. }
  296. /*
  297. * Priority 2: if no exact matches, try matching *.vhost-name
  298. * unintentional matches are possible but resolve to x.com for *.x.com
  299. * which is reasonable. If exact match exists we already chose it and
  300. * never reach here. SSL will still fail it if the cert doesn't allow
  301. * *.x.com.
  302. */
  303. vhost = context->vhost_list;
  304. while (vhost) {
  305. int m = (int)strlen(vhost->name);
  306. if (port && port == vhost->listen_port &&
  307. m <= (colon - 2) &&
  308. servername[colon - m - 1] == '.' &&
  309. !strncmp(vhost->name, servername + colon - m, m)) {
  310. lwsl_info("SNI: Found %s on wildcard: %s\n",
  311. servername, vhost->name);
  312. return vhost;
  313. }
  314. vhost = vhost->vhost_next;
  315. }
  316. /* Priority 3: match the first vhost on our port */
  317. vhost = context->vhost_list;
  318. while (vhost) {
  319. if (port && port == vhost->listen_port) {
  320. lwsl_info("%s: vhost match to %s based on port %d\n",
  321. __func__, vhost->name, port);
  322. return vhost;
  323. }
  324. vhost = vhost->vhost_next;
  325. }
  326. /* no match */
  327. return NULL;
  328. }
  329. LWS_VISIBLE LWS_EXTERN const char *
  330. lws_get_mimetype(const char *file, const struct lws_http_mount *m)
  331. {
  332. const struct lws_protocol_vhost_options *pvo = NULL;
  333. int n = (int)strlen(file);
  334. if (m)
  335. pvo = m->extra_mimetypes;
  336. if (n < 5)
  337. return NULL;
  338. if (!strcmp(&file[n - 4], ".ico"))
  339. return "image/x-icon";
  340. if (!strcmp(&file[n - 4], ".gif"))
  341. return "image/gif";
  342. if (!strcmp(&file[n - 3], ".js"))
  343. return "text/javascript";
  344. if (!strcmp(&file[n - 4], ".png"))
  345. return "image/png";
  346. if (!strcmp(&file[n - 4], ".jpg"))
  347. return "image/jpeg";
  348. if (!strcmp(&file[n - 3], ".gz"))
  349. return "application/gzip";
  350. if (!strcmp(&file[n - 4], ".JPG"))
  351. return "image/jpeg";
  352. if (!strcmp(&file[n - 5], ".html"))
  353. return "text/html";
  354. if (!strcmp(&file[n - 4], ".css"))
  355. return "text/css";
  356. if (!strcmp(&file[n - 4], ".txt"))
  357. return "text/plain";
  358. if (!strcmp(&file[n - 4], ".svg"))
  359. return "image/svg+xml";
  360. if (!strcmp(&file[n - 4], ".ttf"))
  361. return "application/x-font-ttf";
  362. if (!strcmp(&file[n - 4], ".otf"))
  363. return "application/font-woff";
  364. if (!strcmp(&file[n - 5], ".woff"))
  365. return "application/font-woff";
  366. if (!strcmp(&file[n - 4], ".xml"))
  367. return "application/xml";
  368. while (pvo) {
  369. if (pvo->name[0] == '*') /* ie, match anything */
  370. return pvo->value;
  371. if (!strcmp(&file[n - strlen(pvo->name)], pvo->name))
  372. return pvo->value;
  373. pvo = pvo->next;
  374. }
  375. return NULL;
  376. }
  377. static lws_fop_flags_t
  378. lws_vfs_prepare_flags(struct lws *wsi)
  379. {
  380. lws_fop_flags_t f = 0;
  381. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING))
  382. return f;
  383. if (strstr(lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_ACCEPT_ENCODING),
  384. "gzip")) {
  385. lwsl_info("client indicates GZIP is acceptable\n");
  386. f |= LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP;
  387. }
  388. return f;
  389. }
  390. static int
  391. lws_http_serve(struct lws *wsi, char *uri, const char *origin,
  392. const struct lws_http_mount *m)
  393. {
  394. const struct lws_protocol_vhost_options *pvo = m->interpret;
  395. struct lws_process_html_args args;
  396. const char *mimetype;
  397. #if !defined(_WIN32_WCE)
  398. const struct lws_plat_file_ops *fops;
  399. const char *vpath;
  400. lws_fop_flags_t fflags = LWS_O_RDONLY;
  401. #if defined(WIN32) && defined(LWS_HAVE__STAT32I64)
  402. struct _stat32i64 st;
  403. #else
  404. struct stat st;
  405. #endif
  406. int spin = 0;
  407. #endif
  408. char path[256], sym[2048];
  409. unsigned char *p = (unsigned char *)sym + 32 + LWS_PRE, *start = p;
  410. unsigned char *end = p + sizeof(sym) - 32 - LWS_PRE;
  411. #if !defined(WIN32) && !defined(LWS_WITH_ESP32)
  412. size_t len;
  413. #endif
  414. int n;
  415. wsi->handling_404 = 0;
  416. if (!wsi->vhost)
  417. return -1;
  418. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  419. if (wsi->vhost->http.error_document_404 &&
  420. !strcmp(uri, wsi->vhost->http.error_document_404))
  421. wsi->handling_404 = 1;
  422. #endif
  423. lws_snprintf(path, sizeof(path) - 1, "%s/%s", origin, uri);
  424. #if !defined(_WIN32_WCE)
  425. fflags |= lws_vfs_prepare_flags(wsi);
  426. do {
  427. spin++;
  428. fops = lws_vfs_select_fops(wsi->context->fops, path, &vpath);
  429. if (wsi->http.fop_fd)
  430. lws_vfs_file_close(&wsi->http.fop_fd);
  431. wsi->http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
  432. path, vpath, &fflags);
  433. if (!wsi->http.fop_fd) {
  434. lwsl_info("%s: Unable to open '%s': errno %d\n",
  435. __func__, path, errno);
  436. return 1;
  437. }
  438. /* if it can't be statted, don't try */
  439. if (fflags & LWS_FOP_FLAG_VIRTUAL)
  440. break;
  441. #if defined(LWS_WITH_ESP32)
  442. break;
  443. #endif
  444. #if !defined(WIN32)
  445. if (fstat(wsi->http.fop_fd->fd, &st)) {
  446. lwsl_info("unable to stat %s\n", path);
  447. goto notfound;
  448. }
  449. #else
  450. #if defined(LWS_HAVE__STAT32I64)
  451. if (_stat32i64(path, &st)) {
  452. lwsl_info("unable to stat %s\n", path);
  453. goto notfound;
  454. }
  455. #else
  456. if (stat(path, &st)) {
  457. lwsl_info("unable to stat %s\n", path);
  458. goto notfound;
  459. }
  460. #endif
  461. #endif
  462. wsi->http.fop_fd->mod_time = (uint32_t)st.st_mtime;
  463. fflags |= LWS_FOP_FLAG_MOD_TIME_VALID;
  464. #if !defined(WIN32) && !defined(LWS_WITH_ESP32)
  465. if ((S_IFMT & st.st_mode) == S_IFLNK) {
  466. len = readlink(path, sym, sizeof(sym) - 1);
  467. if (len) {
  468. lwsl_err("Failed to read link %s\n", path);
  469. goto notfound;
  470. }
  471. sym[len] = '\0';
  472. lwsl_debug("symlink %s -> %s\n", path, sym);
  473. lws_snprintf(path, sizeof(path) - 1, "%s", sym);
  474. }
  475. #endif
  476. if ((S_IFMT & st.st_mode) == S_IFDIR) {
  477. lwsl_debug("default filename append to dir\n");
  478. lws_snprintf(path, sizeof(path) - 1, "%s/%s/index.html",
  479. origin, uri);
  480. }
  481. } while ((S_IFMT & st.st_mode) != S_IFREG && spin < 5);
  482. if (spin == 5)
  483. lwsl_err("symlink loop %s \n", path);
  484. n = sprintf(sym, "%08llX%08lX",
  485. (unsigned long long)lws_vfs_get_length(wsi->http.fop_fd),
  486. (unsigned long)lws_vfs_get_mod_time(wsi->http.fop_fd));
  487. /* disable ranges if IF_RANGE token invalid */
  488. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_RANGE))
  489. if (strcmp(sym, lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_IF_RANGE)))
  490. /* differs - defeat Range: */
  491. wsi->http.ah->frag_index[WSI_TOKEN_HTTP_RANGE] = 0;
  492. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_IF_NONE_MATCH)) {
  493. /*
  494. * he thinks he has some version of it already,
  495. * check if the tag matches
  496. */
  497. if (!strcmp(sym, lws_hdr_simple_ptr(wsi,
  498. WSI_TOKEN_HTTP_IF_NONE_MATCH))) {
  499. char cache_control[50], *cc = "no-store";
  500. int cclen = 8;
  501. lwsl_debug("%s: ETAG match %s %s\n", __func__,
  502. uri, origin);
  503. /* we don't need to send the payload */
  504. if (lws_add_http_header_status(wsi,
  505. HTTP_STATUS_NOT_MODIFIED, &p, end)) {
  506. lwsl_err("%s: failed adding not modified\n",
  507. __func__);
  508. return -1;
  509. }
  510. if (lws_add_http_header_by_token(wsi,
  511. WSI_TOKEN_HTTP_ETAG,
  512. (unsigned char *)sym, n, &p, end))
  513. return -1;
  514. /* but we still need to send cache control... */
  515. if (m->cache_max_age && m->cache_reusable) {
  516. if (!m->cache_revalidate) {
  517. cc = cache_control;
  518. cclen = sprintf(cache_control,
  519. "%s, max-age=%u",
  520. intermediates[wsi->cache_intermediaries],
  521. m->cache_max_age);
  522. } else {
  523. cc = cache_control;
  524. cclen = sprintf(cache_control,
  525. "must-revalidate, %s, max-age=%u",
  526. intermediates[wsi->cache_intermediaries],
  527. m->cache_max_age);
  528. }
  529. }
  530. if (lws_add_http_header_by_token(wsi,
  531. WSI_TOKEN_HTTP_CACHE_CONTROL,
  532. (unsigned char *)cc, cclen, &p, end))
  533. return -1;
  534. if (lws_finalize_http_header(wsi, &p, end))
  535. return -1;
  536. n = lws_write(wsi, start, p - start,
  537. LWS_WRITE_HTTP_HEADERS |
  538. LWS_WRITE_H2_STREAM_END);
  539. if (n != (p - start)) {
  540. lwsl_err("_write returned %d from %ld\n", n,
  541. (long)(p - start));
  542. return -1;
  543. }
  544. lws_vfs_file_close(&wsi->http.fop_fd);
  545. if (lws_http_transaction_completed(wsi))
  546. return -1;
  547. return 0;
  548. }
  549. }
  550. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ETAG,
  551. (unsigned char *)sym, n, &p, end))
  552. return -1;
  553. #endif
  554. mimetype = lws_get_mimetype(path, m);
  555. if (!mimetype) {
  556. lwsl_info("unknown mimetype for %s\n", path);
  557. if (lws_return_http_status(wsi,
  558. HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, NULL) ||
  559. lws_http_transaction_completed(wsi))
  560. return -1;
  561. return 0;
  562. }
  563. if (!mimetype[0])
  564. lwsl_debug("sending no mimetype for %s\n", path);
  565. wsi->sending_chunked = 0;
  566. /*
  567. * check if this is in the list of file suffixes to be interpreted by
  568. * a protocol
  569. */
  570. while (pvo) {
  571. n = (int)strlen(path);
  572. if (n > (int)strlen(pvo->name) &&
  573. !strcmp(&path[n - strlen(pvo->name)], pvo->name)) {
  574. wsi->interpreting = 1;
  575. if (!wsi->http2_substream)
  576. wsi->sending_chunked = 1;
  577. wsi->protocol_interpret_idx =
  578. (char)(lws_intptr_t)pvo->value;
  579. lwsl_info("want %s interpreted by %s\n", path,
  580. wsi->vhost->protocols[
  581. (int)(lws_intptr_t)(pvo->value)].name);
  582. wsi->protocol = &wsi->vhost->protocols[
  583. (int)(lws_intptr_t)(pvo->value)];
  584. if (lws_ensure_user_space(wsi))
  585. return -1;
  586. break;
  587. }
  588. pvo = pvo->next;
  589. }
  590. if (m->protocol) {
  591. const struct lws_protocols *pp = lws_vhost_name_to_protocol(
  592. wsi->vhost, m->protocol);
  593. if (lws_bind_protocol(wsi, pp, __func__))
  594. return -1;
  595. args.p = (char *)p;
  596. args.max_len = lws_ptr_diff(end, p);
  597. if (pp->callback(wsi, LWS_CALLBACK_ADD_HEADERS,
  598. wsi->user_space, &args, 0))
  599. return -1;
  600. p = (unsigned char *)args.p;
  601. }
  602. *p = '\0';
  603. n = lws_serve_http_file(wsi, path, mimetype, (char *)start,
  604. lws_ptr_diff(p, start));
  605. if (n < 0 || ((n > 0) && lws_http_transaction_completed(wsi)))
  606. return -1; /* error or can't reuse connection: close the socket */
  607. return 0;
  608. notfound:
  609. return 1;
  610. }
  611. #if defined(LWS_ROLE_H1) || defined(LWS_ROLE_H2)
  612. const struct lws_http_mount *
  613. lws_find_mount(struct lws *wsi, const char *uri_ptr, int uri_len)
  614. {
  615. const struct lws_http_mount *hm, *hit = NULL;
  616. int best = 0;
  617. hm = wsi->vhost->http.mount_list;
  618. while (hm) {
  619. if (uri_len >= hm->mountpoint_len &&
  620. !strncmp(uri_ptr, hm->mountpoint, hm->mountpoint_len) &&
  621. (uri_ptr[hm->mountpoint_len] == '\0' ||
  622. uri_ptr[hm->mountpoint_len] == '/' ||
  623. hm->mountpoint_len == 1)
  624. ) {
  625. if (hm->origin_protocol == LWSMPRO_CALLBACK ||
  626. ((hm->origin_protocol == LWSMPRO_CGI ||
  627. lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI) ||
  628. (wsi->http2_substream &&
  629. lws_hdr_total_length(wsi,
  630. WSI_TOKEN_HTTP_COLON_PATH)) ||
  631. hm->protocol) &&
  632. hm->mountpoint_len > best)) {
  633. best = hm->mountpoint_len;
  634. hit = hm;
  635. }
  636. }
  637. hm = hm->mount_next;
  638. }
  639. return hit;
  640. }
  641. #endif
  642. #if !defined(LWS_WITH_ESP32)
  643. static int
  644. lws_find_string_in_file(const char *filename, const char *string, int stringlen)
  645. {
  646. char buf[128];
  647. int fd, match = 0, pos = 0, n = 0, hit = 0;
  648. fd = lws_open(filename, O_RDONLY);
  649. if (fd < 0) {
  650. lwsl_err("can't open auth file: %s\n", filename);
  651. return 0;
  652. }
  653. while (1) {
  654. if (pos == n) {
  655. n = read(fd, buf, sizeof(buf));
  656. if (n <= 0) {
  657. if (match == stringlen)
  658. hit = 1;
  659. break;
  660. }
  661. pos = 0;
  662. }
  663. if (match == stringlen) {
  664. if (buf[pos] == '\r' || buf[pos] == '\n') {
  665. hit = 1;
  666. break;
  667. }
  668. match = 0;
  669. }
  670. if (buf[pos] == string[match])
  671. match++;
  672. else
  673. match = 0;
  674. pos++;
  675. }
  676. close(fd);
  677. return hit;
  678. }
  679. #endif
  680. static int
  681. lws_unauthorised_basic_auth(struct lws *wsi)
  682. {
  683. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  684. unsigned char *start = pt->serv_buf + LWS_PRE,
  685. *p = start, *end = p + 2048;
  686. char buf[64];
  687. int n;
  688. /* no auth... tell him it is required */
  689. if (lws_add_http_header_status(wsi, HTTP_STATUS_UNAUTHORIZED, &p, end))
  690. return -1;
  691. n = lws_snprintf(buf, sizeof(buf), "Basic realm=\"lwsws\"");
  692. if (lws_add_http_header_by_token(wsi,
  693. WSI_TOKEN_HTTP_WWW_AUTHENTICATE,
  694. (unsigned char *)buf, n, &p, end))
  695. return -1;
  696. if (lws_finalize_http_header(wsi, &p, end))
  697. return -1;
  698. n = lws_write(wsi, start, p - start, LWS_WRITE_HTTP_HEADERS |
  699. LWS_WRITE_H2_STREAM_END);
  700. if (n < 0)
  701. return -1;
  702. return lws_http_transaction_completed(wsi);
  703. }
  704. int lws_clean_url(char *p)
  705. {
  706. if (p[0] == 'h' && p[1] == 't' && p[2] == 't' && p[3] == 'p') {
  707. p += 4;
  708. if (*p == 's')
  709. p++;
  710. if (*p == ':') {
  711. p++;
  712. if (*p == '/')
  713. p++;
  714. }
  715. }
  716. while (*p) {
  717. if (p[0] == '/' && p[1] == '/') {
  718. char *p1 = p;
  719. while (*p1) {
  720. *p1 = p1[1];
  721. p1++;
  722. }
  723. continue;
  724. }
  725. p++;
  726. }
  727. return 0;
  728. }
  729. static const unsigned char methods[] = {
  730. WSI_TOKEN_GET_URI,
  731. WSI_TOKEN_POST_URI,
  732. WSI_TOKEN_OPTIONS_URI,
  733. WSI_TOKEN_PUT_URI,
  734. WSI_TOKEN_PATCH_URI,
  735. WSI_TOKEN_DELETE_URI,
  736. WSI_TOKEN_CONNECT,
  737. WSI_TOKEN_HEAD_URI,
  738. #ifdef LWS_WITH_HTTP2
  739. WSI_TOKEN_HTTP_COLON_PATH,
  740. #endif
  741. };
  742. static int
  743. lws_http_get_uri_and_method(struct lws *wsi, char **puri_ptr, int *puri_len)
  744. {
  745. int n, count = 0;
  746. for (n = 0; n < (int)LWS_ARRAY_SIZE(methods); n++)
  747. if (lws_hdr_total_length(wsi, methods[n]))
  748. count++;
  749. if (!count) {
  750. lwsl_warn("Missing URI in HTTP request\n");
  751. return -1;
  752. }
  753. if (count != 1 &&
  754. !(wsi->http2_substream &&
  755. lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_COLON_PATH))) {
  756. lwsl_warn("multiple methods?\n");
  757. return -1;
  758. }
  759. for (n = 0; n < (int)LWS_ARRAY_SIZE(methods); n++)
  760. if (lws_hdr_total_length(wsi, methods[n])) {
  761. *puri_ptr = lws_hdr_simple_ptr(wsi, methods[n]);
  762. *puri_len = lws_hdr_total_length(wsi, methods[n]);
  763. return n;
  764. }
  765. return -1;
  766. }
  767. static const char * const oprot[] = {
  768. "http://", "https://"
  769. };
  770. int
  771. lws_http_action(struct lws *wsi)
  772. {
  773. struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
  774. const struct lws_http_mount *hit = NULL;
  775. enum http_version request_version;
  776. struct lws_process_html_args args;
  777. enum http_conn_type conn_type;
  778. char content_length_str[32];
  779. char http_version_str[12];
  780. char *uri_ptr = NULL, *s;
  781. int uri_len = 0, meth, m;
  782. char http_conn_str[25];
  783. int http_version_len;
  784. unsigned int n;
  785. meth = lws_http_get_uri_and_method(wsi, &uri_ptr, &uri_len);
  786. if (meth < 0 || meth >= (int)LWS_ARRAY_SIZE(method_names))
  787. goto bail_nuke_ah;
  788. /* we insist on absolute paths */
  789. if (!uri_ptr || uri_ptr[0] != '/') {
  790. lws_return_http_status(wsi, HTTP_STATUS_FORBIDDEN, NULL);
  791. goto bail_nuke_ah;
  792. }
  793. lwsl_info("Method: '%s' (%d), request for '%s'\n", method_names[meth],
  794. meth, uri_ptr);
  795. if (wsi->role_ops && wsi->role_ops->check_upgrades)
  796. switch (wsi->role_ops->check_upgrades(wsi)) {
  797. case LWS_UPG_RET_DONE:
  798. return 0;
  799. case LWS_UPG_RET_CONTINUE:
  800. break;
  801. case LWS_UPG_RET_BAIL:
  802. goto bail_nuke_ah;
  803. }
  804. if (lws_ensure_user_space(wsi))
  805. goto bail_nuke_ah;
  806. /* HTTP header had a content length? */
  807. wsi->http.rx_content_length = 0;
  808. wsi->http.content_length_explicitly_zero = 0;
  809. if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI) ||
  810. lws_hdr_total_length(wsi, WSI_TOKEN_PATCH_URI) ||
  811. lws_hdr_total_length(wsi, WSI_TOKEN_PUT_URI))
  812. wsi->http.rx_content_length = 100 * 1024 * 1024;
  813. if (lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_CONTENT_LENGTH) &&
  814. lws_hdr_copy(wsi, content_length_str,
  815. sizeof(content_length_str) - 1,
  816. WSI_TOKEN_HTTP_CONTENT_LENGTH) > 0) {
  817. wsi->http.rx_content_length = atoll(content_length_str);
  818. if (!wsi->http.rx_content_length) {
  819. wsi->http.content_length_explicitly_zero = 1;
  820. lwsl_debug("%s: explicit 0 content-length\n", __func__);
  821. }
  822. }
  823. if (wsi->http2_substream) {
  824. wsi->http.request_version = HTTP_VERSION_2;
  825. } else {
  826. /* http_version? Default to 1.0, override with token: */
  827. request_version = HTTP_VERSION_1_0;
  828. /* Works for single digit HTTP versions. : */
  829. http_version_len = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP);
  830. if (http_version_len > 7 &&
  831. lws_hdr_copy(wsi, http_version_str,
  832. sizeof(http_version_str) - 1,
  833. WSI_TOKEN_HTTP) > 0 &&
  834. http_version_str[5] == '1' && http_version_str[7] == '1')
  835. request_version = HTTP_VERSION_1_1;
  836. wsi->http.request_version = request_version;
  837. /* HTTP/1.1 defaults to "keep-alive", 1.0 to "close" */
  838. if (request_version == HTTP_VERSION_1_1)
  839. conn_type = HTTP_CONNECTION_KEEP_ALIVE;
  840. else
  841. conn_type = HTTP_CONNECTION_CLOSE;
  842. /* Override default if http "Connection:" header: */
  843. if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECTION) &&
  844. lws_hdr_copy(wsi, http_conn_str, sizeof(http_conn_str) - 1,
  845. WSI_TOKEN_CONNECTION) > 0) {
  846. http_conn_str[sizeof(http_conn_str) - 1] = '\0';
  847. if (!strcasecmp(http_conn_str, "keep-alive"))
  848. conn_type = HTTP_CONNECTION_KEEP_ALIVE;
  849. else
  850. if (!strcasecmp(http_conn_str, "close"))
  851. conn_type = HTTP_CONNECTION_CLOSE;
  852. }
  853. wsi->http.conn_type = conn_type;
  854. }
  855. n = wsi->protocol->callback(wsi, LWS_CALLBACK_FILTER_HTTP_CONNECTION,
  856. wsi->user_space, uri_ptr, uri_len);
  857. if (n) {
  858. lwsl_info("LWS_CALLBACK_HTTP closing\n");
  859. return 1;
  860. }
  861. /*
  862. * if there is content supposed to be coming,
  863. * put a timeout on it having arrived
  864. */
  865. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
  866. wsi->context->timeout_secs);
  867. #ifdef LWS_WITH_TLS
  868. if (wsi->tls.redirect_to_https) {
  869. /*
  870. * we accepted http:// only so we could redirect to
  871. * https://, so issue the redirect. Create the redirection
  872. * URI from the host: header and ignore the path part
  873. */
  874. unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
  875. *end = p + wsi->context->pt_serv_buf_size - LWS_PRE;
  876. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST))
  877. goto bail_nuke_ah;
  878. n = sprintf((char *)end, "https://%s/",
  879. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
  880. n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
  881. end, n, &p, end);
  882. if ((int)n < 0)
  883. goto bail_nuke_ah;
  884. return lws_http_transaction_completed(wsi);
  885. }
  886. #endif
  887. #ifdef LWS_WITH_ACCESS_LOG
  888. lws_prepare_access_log_info(wsi, uri_ptr, uri_len, meth);
  889. #endif
  890. /* can we serve it from the mount list? */
  891. hit = lws_find_mount(wsi, uri_ptr, uri_len);
  892. if (!hit) {
  893. /* deferred cleanup and reset to protocols[0] */
  894. lwsl_info("no hit\n");
  895. if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0],
  896. "no mount hit"))
  897. return 1;
  898. lwsi_set_state(wsi, LRS_DOING_TRANSACTION);
  899. m = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
  900. wsi->user_space, uri_ptr, uri_len);
  901. goto after;
  902. }
  903. s = uri_ptr + hit->mountpoint_len;
  904. /*
  905. * if we have a mountpoint like https://xxx.com/yyy
  906. * there is an implied / at the end for our purposes since
  907. * we can only mount on a "directory".
  908. *
  909. * But if we just go with that, the browser cannot understand
  910. * that he is actually looking down one "directory level", so
  911. * even though we give him /yyy/abc.html he acts like the
  912. * current directory level is /. So relative urls like "x.png"
  913. * wrongly look outside the mountpoint.
  914. *
  915. * Therefore if we didn't come in on a url with an explicit
  916. * / at the end, we must redirect to add it so the browser
  917. * understands he is one "directory level" down.
  918. */
  919. if ((hit->mountpoint_len > 1 ||
  920. (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
  921. hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
  922. (*s != '/' ||
  923. (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
  924. hit->origin_protocol == LWSMPRO_REDIR_HTTPS)) &&
  925. (hit->origin_protocol != LWSMPRO_CGI &&
  926. hit->origin_protocol != LWSMPRO_CALLBACK)) {
  927. unsigned char *start = pt->serv_buf + LWS_PRE, *p = start,
  928. *end = p + wsi->context->pt_serv_buf_size -
  929. LWS_PRE - 512;
  930. lwsl_info("Doing 301 '%s' org %s\n", s, hit->origin);
  931. /* > at start indicates deal with by redirect */
  932. if (hit->origin_protocol == LWSMPRO_REDIR_HTTP ||
  933. hit->origin_protocol == LWSMPRO_REDIR_HTTPS)
  934. n = lws_snprintf((char *)end, 256, "%s%s",
  935. oprot[hit->origin_protocol & 1],
  936. hit->origin);
  937. else {
  938. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
  939. if (!lws_hdr_total_length(wsi,
  940. WSI_TOKEN_HTTP_COLON_AUTHORITY))
  941. goto bail_nuke_ah;
  942. n = lws_snprintf((char *)end, 256,
  943. "%s%s%s/", oprot[!!lws_is_ssl(wsi)],
  944. lws_hdr_simple_ptr(wsi,
  945. WSI_TOKEN_HTTP_COLON_AUTHORITY),
  946. uri_ptr);
  947. } else
  948. n = lws_snprintf((char *)end, 256,
  949. "%s%s%s/", oprot[!!lws_is_ssl(wsi)],
  950. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST),
  951. uri_ptr);
  952. }
  953. lws_clean_url((char *)end);
  954. n = lws_http_redirect(wsi, HTTP_STATUS_MOVED_PERMANENTLY,
  955. end, n, &p, end);
  956. if ((int)n < 0)
  957. goto bail_nuke_ah;
  958. return lws_http_transaction_completed(wsi);
  959. }
  960. /* basic auth? */
  961. if (hit->basic_auth_login_file) {
  962. char b64[160], plain[(sizeof(b64) * 3) / 4], *pcolon;
  963. int m, ml, fi;
  964. /* Did he send auth? */
  965. ml = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_AUTHORIZATION);
  966. if (!ml)
  967. return lws_unauthorised_basic_auth(wsi);
  968. /* Disallow fragmentation monkey business */
  969. fi = wsi->http.ah->frag_index[WSI_TOKEN_HTTP_AUTHORIZATION];
  970. if (wsi->http.ah->frags[fi].nfrag) {
  971. lwsl_err("fragmented basic auth header not allowed\n");
  972. return lws_unauthorised_basic_auth(wsi);
  973. }
  974. n = HTTP_STATUS_FORBIDDEN;
  975. m = lws_hdr_copy(wsi, b64, sizeof(b64),
  976. WSI_TOKEN_HTTP_AUTHORIZATION);
  977. if (m < 7) {
  978. lwsl_err("b64 auth too long\n");
  979. goto transaction_result_n;
  980. }
  981. b64[5] = '\0';
  982. if (strcasecmp(b64, "Basic")) {
  983. lwsl_err("auth missing basic: %s\n", b64);
  984. goto transaction_result_n;
  985. }
  986. /* It'll be like Authorization: Basic QWxhZGRpbjpPcGVuU2VzYW1l */
  987. m = lws_b64_decode_string(b64 + 6, plain, sizeof(plain) - 1);
  988. if (m < 0) {
  989. lwsl_err("plain auth too long\n");
  990. goto transaction_result_n;
  991. }
  992. plain[m] = '\0';
  993. pcolon = strchr(plain, ':');
  994. if (!pcolon) {
  995. lwsl_err("basic auth format broken\n");
  996. return lws_unauthorised_basic_auth(wsi);
  997. }
  998. if (!lws_find_string_in_file(hit->basic_auth_login_file,
  999. plain, m)) {
  1000. lwsl_err("basic auth lookup failed\n");
  1001. return lws_unauthorised_basic_auth(wsi);
  1002. }
  1003. /*
  1004. * Rewrite WSI_TOKEN_HTTP_AUTHORIZATION so it is just the
  1005. * authorized username
  1006. */
  1007. *pcolon = '\0';
  1008. wsi->http.ah->frags[fi].len = lws_ptr_diff(pcolon, plain);
  1009. pcolon = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION);
  1010. strncpy(pcolon, plain, ml - 1);
  1011. pcolon[ml - 1] = '\0';
  1012. lwsl_info("%s: basic auth accepted for %s\n", __func__,
  1013. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP_AUTHORIZATION));
  1014. }
  1015. #if defined(LWS_WITH_HTTP_PROXY)
  1016. /*
  1017. * The mount is a reverse proxy?
  1018. */
  1019. // lwsl_notice("%s: origin_protocol: %d\n", __func__, hit->origin_protocol);
  1020. if (hit->origin_protocol == LWSMPRO_HTTPS ||
  1021. hit->origin_protocol == LWSMPRO_HTTP) {
  1022. char ads[96], rpath[256], *pcolon, *pslash, unix_skt = 0;
  1023. struct lws_client_connect_info i;
  1024. struct lws *cwsi;
  1025. int n, na;
  1026. memset(&i, 0, sizeof(i));
  1027. i.context = lws_get_context(wsi);
  1028. if (hit->origin[0] == '+')
  1029. unix_skt = 1;
  1030. pcolon = strchr(hit->origin, ':');
  1031. pslash = strchr(hit->origin, '/');
  1032. if (!pslash) {
  1033. lwsl_err("Proxy mount origin '%s' must have /\n",
  1034. hit->origin);
  1035. return -1;
  1036. }
  1037. if (unix_skt) {
  1038. if (!pcolon) {
  1039. lwsl_err("Proxy mount origin for unix skt must "
  1040. "have address delimited by :\n");
  1041. return -1;
  1042. }
  1043. n = lws_ptr_diff(pcolon, hit->origin);
  1044. pslash = pcolon;
  1045. } else {
  1046. if (pcolon > pslash)
  1047. pcolon = NULL;
  1048. if (pcolon)
  1049. n = (int)(pcolon - hit->origin);
  1050. else
  1051. n = (int)(pslash - hit->origin);
  1052. if (n >= (int)sizeof(ads) - 2)
  1053. n = sizeof(ads) - 2;
  1054. }
  1055. memcpy(ads, hit->origin, n);
  1056. ads[n] = '\0';
  1057. i.address = ads;
  1058. i.port = 80;
  1059. if (hit->origin_protocol == LWSMPRO_HTTPS) {
  1060. i.port = 443;
  1061. i.ssl_connection = 1;
  1062. }
  1063. if (pcolon)
  1064. i.port = atoi(pcolon + 1);
  1065. n = lws_snprintf(rpath, sizeof(rpath) - 1, "/%s/%s",
  1066. pslash + 1, uri_ptr + hit->mountpoint_len) - 2;
  1067. lws_clean_url(rpath);
  1068. na = lws_hdr_total_length(wsi, WSI_TOKEN_HTTP_URI_ARGS);
  1069. if (na) {
  1070. char *p = rpath + n;
  1071. if (na >= (int)sizeof(rpath) - n - 2) {
  1072. lwsl_info("%s: query string %d longer "
  1073. "than we can handle\n", __func__,
  1074. na);
  1075. return -1;
  1076. }
  1077. *p++ = '?';
  1078. if (lws_hdr_copy(wsi, p,
  1079. (int)(&rpath[sizeof(rpath) - 1] - p),
  1080. WSI_TOKEN_HTTP_URI_ARGS) > 0)
  1081. while (na--) {
  1082. if (*p == '\0')
  1083. *p = '&';
  1084. p++;
  1085. }
  1086. *p = '\0';
  1087. }
  1088. i.path = rpath;
  1089. if (i.address[0] != '+' ||
  1090. !lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST))
  1091. i.host = i.address;
  1092. else
  1093. i.host = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST);
  1094. i.origin = NULL;
  1095. i.method = "GET";
  1096. i.alpn = "http/1.1";
  1097. i.parent_wsi = wsi;
  1098. i.pwsi = &cwsi;
  1099. // i.uri_replace_from = hit->origin;
  1100. // i.uri_replace_to = hit->mountpoint;
  1101. lwsl_info("proxying to %s port %d url %s, ssl %d, "
  1102. "from %s, to %s\n",
  1103. i.address, i.port, i.path, i.ssl_connection,
  1104. i.uri_replace_from, i.uri_replace_to);
  1105. if (!lws_client_connect_via_info(&i)) {
  1106. lwsl_err("proxy connect fail\n");
  1107. /*
  1108. * ... we can't do the proxy action, but we can
  1109. * cleanly return him a 503 and a description
  1110. */
  1111. lws_return_http_status(wsi,
  1112. HTTP_STATUS_SERVICE_UNAVAILABLE,
  1113. "<h1>Service Temporarily Unavailable</h1>"
  1114. "The server is temporarily unable to service "
  1115. "your request due to maintenance downtime or "
  1116. "capacity problems. Please try again later.");
  1117. return 1;
  1118. }
  1119. lwsl_info("%s: setting proxy clientside on %p (parent %p)\n",
  1120. __func__, cwsi, lws_get_parent(cwsi));
  1121. cwsi->http.proxy_clientside = 1;
  1122. return 0;
  1123. }
  1124. #endif
  1125. /*
  1126. * A particular protocol callback is mounted here?
  1127. *
  1128. * For the duration of this http transaction, bind us to the
  1129. * associated protocol
  1130. */
  1131. if (hit->origin_protocol == LWSMPRO_CALLBACK || hit->protocol) {
  1132. const struct lws_protocols *pp;
  1133. const char *name = hit->origin;
  1134. if (hit->protocol)
  1135. name = hit->protocol;
  1136. pp = lws_vhost_name_to_protocol(wsi->vhost, name);
  1137. if (!pp) {
  1138. n = -1;
  1139. lwsl_err("Unable to find plugin '%s'\n",
  1140. hit->origin);
  1141. return 1;
  1142. }
  1143. if (lws_bind_protocol(wsi, pp, "http action CALLBACK bind"))
  1144. return 1;
  1145. args.p = uri_ptr;
  1146. args.len = uri_len;
  1147. args.max_len = hit->auth_mask;
  1148. args.final = 0; /* used to signal callback dealt with it */
  1149. args.chunked = 0;
  1150. n = wsi->protocol->callback(wsi,
  1151. LWS_CALLBACK_CHECK_ACCESS_RIGHTS,
  1152. wsi->user_space, &args, 0);
  1153. if (n) {
  1154. lws_return_http_status(wsi, HTTP_STATUS_UNAUTHORIZED,
  1155. NULL);
  1156. goto bail_nuke_ah;
  1157. }
  1158. if (args.final) /* callback completely handled it well */
  1159. return 0;
  1160. if (hit->cgienv && wsi->protocol->callback(wsi,
  1161. LWS_CALLBACK_HTTP_PMO,
  1162. wsi->user_space, (void *)hit->cgienv, 0))
  1163. return 1;
  1164. if (lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
  1165. m = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
  1166. wsi->user_space,
  1167. uri_ptr + hit->mountpoint_len,
  1168. uri_len - hit->mountpoint_len);
  1169. goto after;
  1170. }
  1171. }
  1172. #ifdef LWS_WITH_CGI
  1173. /* did we hit something with a cgi:// origin? */
  1174. if (hit->origin_protocol == LWSMPRO_CGI) {
  1175. const char *cmd[] = {
  1176. NULL, /* replace with cgi path */
  1177. NULL
  1178. };
  1179. lwsl_debug("%s: cgi\n", __func__);
  1180. cmd[0] = hit->origin;
  1181. n = 5;
  1182. if (hit->cgi_timeout)
  1183. n = hit->cgi_timeout;
  1184. n = lws_cgi(wsi, cmd, hit->mountpoint_len, n,
  1185. hit->cgienv);
  1186. if (n) {
  1187. lwsl_err("%s: cgi failed\n", __func__);
  1188. return -1;
  1189. }
  1190. goto deal_body;
  1191. }
  1192. #endif
  1193. n = uri_len - lws_ptr_diff(s, uri_ptr); // (int)strlen(s);
  1194. if (s[0] == '\0' || (n == 1 && s[n - 1] == '/'))
  1195. s = (char *)hit->def;
  1196. if (!s)
  1197. s = "index.html";
  1198. wsi->cache_secs = hit->cache_max_age;
  1199. wsi->cache_reuse = hit->cache_reusable;
  1200. wsi->cache_revalidate = hit->cache_revalidate;
  1201. wsi->cache_intermediaries = hit->cache_intermediaries;
  1202. m = 1;
  1203. if (hit->origin_protocol == LWSMPRO_FILE)
  1204. m = lws_http_serve(wsi, s, hit->origin, hit);
  1205. if (m > 0) {
  1206. /*
  1207. * lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND, NULL);
  1208. */
  1209. if (hit->protocol) {
  1210. const struct lws_protocols *pp =
  1211. lws_vhost_name_to_protocol(
  1212. wsi->vhost, hit->protocol);
  1213. lwsi_set_state(wsi, LRS_DOING_TRANSACTION);
  1214. if (lws_bind_protocol(wsi, pp, "http_action HTTP"))
  1215. return 1;
  1216. m = pp->callback(wsi, LWS_CALLBACK_HTTP,
  1217. wsi->user_space,
  1218. uri_ptr + hit->mountpoint_len,
  1219. uri_len - hit->mountpoint_len);
  1220. } else
  1221. m = wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP,
  1222. wsi->user_space, uri_ptr, uri_len);
  1223. }
  1224. after:
  1225. if (m) {
  1226. lwsl_info("LWS_CALLBACK_HTTP closing\n");
  1227. return 1;
  1228. }
  1229. #ifdef LWS_WITH_CGI
  1230. deal_body:
  1231. #endif
  1232. /*
  1233. * If we're not issuing a file, check for content_length or
  1234. * HTTP keep-alive. No keep-alive header allocation for
  1235. * ISSUING_FILE, as this uses HTTP/1.0.
  1236. *
  1237. * In any case, return 0 and let lws_read decide how to
  1238. * proceed based on state
  1239. */
  1240. if (lwsi_state(wsi) != LRS_ISSUING_FILE) {
  1241. /* Prepare to read body if we have a content length: */
  1242. lwsl_debug("wsi->http.rx_content_length %lld %d %d\n",
  1243. (long long)wsi->http.rx_content_length,
  1244. wsi->upgraded_to_http2, wsi->http2_substream);
  1245. if (wsi->http.content_length_explicitly_zero &&
  1246. lws_hdr_total_length(wsi, WSI_TOKEN_POST_URI)) {
  1247. /*
  1248. * POST with an explicit content-length of zero
  1249. *
  1250. * If we don't give the user code the empty HTTP_BODY
  1251. * callback, he may become confused to hear the
  1252. * HTTP_BODY_COMPLETION (due to, eg, instantiation of
  1253. * lws_spa never happened).
  1254. *
  1255. * HTTP_BODY_COMPLETION is responsible for sending the
  1256. * result status code and result body if any, and
  1257. * do the transaction complete processing.
  1258. */
  1259. if (wsi->protocol->callback(wsi,
  1260. LWS_CALLBACK_HTTP_BODY,
  1261. wsi->user_space, NULL, 0))
  1262. return 1;
  1263. if (wsi->protocol->callback(wsi,
  1264. LWS_CALLBACK_HTTP_BODY_COMPLETION,
  1265. wsi->user_space, NULL, 0))
  1266. return 1;
  1267. return 0;
  1268. }
  1269. if (wsi->http.rx_content_length > 0) {
  1270. lwsi_set_state(wsi, LRS_BODY);
  1271. lwsl_info("%s: %p: LRS_BODY state set (0x%x)\n",
  1272. __func__, wsi, wsi->wsistate);
  1273. wsi->http.rx_content_remain =
  1274. wsi->http.rx_content_length;
  1275. /*
  1276. * At this point we have transitioned from deferred
  1277. * action to expecting BODY on the stream wsi, if it's
  1278. * in a bundle like h2. So if the stream wsi has its
  1279. * own buflist, we need to deal with that first.
  1280. */
  1281. while (1) {
  1282. struct lws_tokens ebuf;
  1283. int m;
  1284. ebuf.len = (int)lws_buflist_next_segment_len(
  1285. &wsi->buflist,
  1286. (uint8_t **)&ebuf.token);
  1287. if (!ebuf.len)
  1288. break;
  1289. lwsl_notice("%s: consuming %d\n", __func__,
  1290. (int)ebuf.len);
  1291. m = lws_read_h1(wsi, (uint8_t *)ebuf.token,
  1292. ebuf.len);
  1293. if (m < 0)
  1294. return -1;
  1295. if (lws_buflist_aware_consume(wsi, &ebuf, m, 1))
  1296. return -1;
  1297. }
  1298. }
  1299. }
  1300. return 0;
  1301. bail_nuke_ah:
  1302. lws_header_table_detach(wsi, 1);
  1303. return 1;
  1304. transaction_result_n:
  1305. lws_return_http_status(wsi, n, NULL);
  1306. return lws_http_transaction_completed(wsi);
  1307. }
  1308. int
  1309. lws_confirm_host_header(struct lws *wsi)
  1310. {
  1311. struct lws_tokenize ts;
  1312. lws_tokenize_elem e;
  1313. char buf[128];
  1314. int port = 80;
  1315. /*
  1316. * this vhost wants us to validate what the
  1317. * client sent against our vhost name
  1318. */
  1319. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
  1320. lwsl_info("%s: missing host on upgrade\n", __func__);
  1321. return 1;
  1322. }
  1323. #if defined(LWS_WITH_TLS)
  1324. if (wsi->tls.ssl)
  1325. port = 443;
  1326. #endif
  1327. lws_tokenize_init(&ts, buf, LWS_TOKENIZE_F_DOT_NONTERM /* server.com */|
  1328. LWS_TOKENIZE_F_NO_FLOATS /* 1.server.com */|
  1329. LWS_TOKENIZE_F_MINUS_NONTERM /* a-b.com */);
  1330. ts.len = lws_hdr_copy(wsi, buf, sizeof(buf) - 1, WSI_TOKEN_HOST);
  1331. if (ts.len <= 0) {
  1332. lwsl_info("%s: missing or oversize host header\n", __func__);
  1333. return 1;
  1334. }
  1335. if (lws_tokenize(&ts) != LWS_TOKZE_TOKEN)
  1336. goto bad_format;
  1337. if (strncmp(ts.token, wsi->vhost->name, ts.token_len)) {
  1338. buf[(ts.token - buf) + ts.token_len] = '\0';
  1339. lwsl_info("%s: '%s' in host hdr but vhost name %s\n",
  1340. __func__, ts.token, wsi->vhost->name);
  1341. return 1;
  1342. }
  1343. e = lws_tokenize(&ts);
  1344. if (e == LWS_TOKZE_DELIMITER && ts.token[0] == ':') {
  1345. if (lws_tokenize(&ts) != LWS_TOKZE_INTEGER)
  1346. goto bad_format;
  1347. else
  1348. port = atoi(ts.token);
  1349. } else
  1350. if (e != LWS_TOKZE_ENDED)
  1351. goto bad_format;
  1352. if (wsi->vhost->listen_port != port) {
  1353. lwsl_info("%s: host port %d mismatches vhost port %d\n",
  1354. __func__, port, wsi->vhost->listen_port);
  1355. return 1;
  1356. }
  1357. lwsl_debug("%s: host header OK\n", __func__);
  1358. return 0;
  1359. bad_format:
  1360. lwsl_info("%s: bad host header format\n", __func__);
  1361. return 1;
  1362. }
  1363. int
  1364. lws_handshake_server(struct lws *wsi, unsigned char **buf, size_t len)
  1365. {
  1366. struct lws_context *context = lws_get_context(wsi);
  1367. unsigned char *obuf = *buf;
  1368. #if defined(LWS_WITH_HTTP2)
  1369. char tbuf[128], *p;
  1370. #endif
  1371. size_t olen = len;
  1372. int n = 0, m, i;
  1373. if (len >= 10000000) {
  1374. lwsl_err("%s: assert: len %ld\n", __func__, (long)len);
  1375. assert(0);
  1376. }
  1377. if (!wsi->http.ah) {
  1378. lwsl_err("%s: assert: NULL ah\n", __func__);
  1379. assert(0);
  1380. }
  1381. while (len) {
  1382. if (!lwsi_role_server(wsi) || !lwsi_role_http(wsi)) {
  1383. lwsl_err("%s: bad wsi role 0x%x\n", __func__,
  1384. lwsi_role(wsi));
  1385. goto bail_nuke_ah;
  1386. }
  1387. i = (int)len;
  1388. m = lws_parse(wsi, *buf, &i);
  1389. lwsl_info("%s: parsed count %d\n", __func__, (int)len - i);
  1390. (*buf) += (int)len - i;
  1391. len = i;
  1392. if (m) {
  1393. if (m == 2) {
  1394. /*
  1395. * we are transitioning from http with
  1396. * an AH, to raw. Drop the ah and set
  1397. * the mode.
  1398. */
  1399. raw_transition:
  1400. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  1401. lws_bind_protocol(wsi, &wsi->vhost->protocols[
  1402. wsi->vhost->
  1403. raw_protocol_index],
  1404. __func__);
  1405. lwsl_info("transition to raw vh %s prot %d\n",
  1406. wsi->vhost->name,
  1407. wsi->vhost->raw_protocol_index);
  1408. if ((wsi->protocol->callback)(wsi,
  1409. LWS_CALLBACK_RAW_ADOPT,
  1410. wsi->user_space, NULL, 0))
  1411. goto bail_nuke_ah;
  1412. lws_role_transition(wsi, 0, LRS_ESTABLISHED,
  1413. &role_ops_raw_skt);
  1414. lws_header_table_detach(wsi, 1);
  1415. if (wsi->protocol->callback(wsi,
  1416. LWS_CALLBACK_RAW_RX,
  1417. wsi->user_space, obuf, olen))
  1418. return 1;
  1419. return 0;
  1420. }
  1421. lwsl_info("lws_parse failed\n");
  1422. goto bail_nuke_ah;
  1423. }
  1424. if (wsi->http.ah->parser_state != WSI_PARSING_COMPLETE)
  1425. continue;
  1426. lwsl_parser("%s: lws_parse sees parsing complete\n", __func__);
  1427. /* select vhost */
  1428. if (wsi->vhost->listen_port &&
  1429. lws_hdr_total_length(wsi, WSI_TOKEN_HOST)) {
  1430. struct lws_vhost *vhost = lws_select_vhost(
  1431. context, wsi->vhost->listen_port,
  1432. lws_hdr_simple_ptr(wsi, WSI_TOKEN_HOST));
  1433. if (vhost)
  1434. lws_vhost_bind_wsi(vhost, wsi);
  1435. } else
  1436. lwsl_info("no host\n");
  1437. if (!lwsi_role_h2(wsi) || !lwsi_role_server(wsi)) {
  1438. wsi->vhost->conn_stats.h1_trans++;
  1439. if (!wsi->conn_stat_done) {
  1440. wsi->vhost->conn_stats.h1_conn++;
  1441. wsi->conn_stat_done = 1;
  1442. }
  1443. }
  1444. /* check for unwelcome guests */
  1445. if (wsi->context->reject_service_keywords) {
  1446. const struct lws_protocol_vhost_options *rej =
  1447. wsi->context->reject_service_keywords;
  1448. char ua[384], *msg = NULL;
  1449. if (lws_hdr_copy(wsi, ua, sizeof(ua) - 1,
  1450. WSI_TOKEN_HTTP_USER_AGENT) > 0) {
  1451. #ifdef LWS_WITH_ACCESS_LOG
  1452. char *uri_ptr = NULL;
  1453. int meth, uri_len;
  1454. #endif
  1455. ua[sizeof(ua) - 1] = '\0';
  1456. while (rej) {
  1457. if (!strstr(ua, rej->name)) {
  1458. rej = rej->next;
  1459. continue;
  1460. }
  1461. msg = strchr(rej->value, ' ');
  1462. if (msg)
  1463. msg++;
  1464. lws_return_http_status(wsi,
  1465. atoi(rej->value), msg);
  1466. #ifdef LWS_WITH_ACCESS_LOG
  1467. meth = lws_http_get_uri_and_method(wsi,
  1468. &uri_ptr, &uri_len);
  1469. if (meth >= 0)
  1470. lws_prepare_access_log_info(wsi,
  1471. uri_ptr, uri_len, meth);
  1472. /* wsi close will do the log */
  1473. #endif
  1474. wsi->vhost->conn_stats.rejected++;
  1475. /*
  1476. * We don't want anything from
  1477. * this rejected guy. Follow
  1478. * the close flow, not the
  1479. * transaction complete flow.
  1480. */
  1481. goto bail_nuke_ah;
  1482. }
  1483. }
  1484. }
  1485. if (lws_hdr_total_length(wsi, WSI_TOKEN_CONNECT)) {
  1486. lwsl_info("Changing to RAW mode\n");
  1487. m = 0;
  1488. goto raw_transition;
  1489. }
  1490. lwsi_set_state(wsi, LRS_PRE_WS_SERVING_ACCEPT);
  1491. lws_set_timeout(wsi, NO_PENDING_TIMEOUT, 0);
  1492. if (lws_hdr_total_length(wsi, WSI_TOKEN_UPGRADE)) {
  1493. const char *up = lws_hdr_simple_ptr(wsi,
  1494. WSI_TOKEN_UPGRADE);
  1495. if (strcasecmp(up, "websocket") &&
  1496. strcasecmp(up, "h2c")) {
  1497. lwsl_info("Unknown upgrade '%s'\n", up);
  1498. if (lws_return_http_status(wsi,
  1499. HTTP_STATUS_FORBIDDEN, NULL) ||
  1500. lws_http_transaction_completed(wsi))
  1501. goto bail_nuke_ah;
  1502. }
  1503. n = user_callback_handle_rxflow(wsi->protocol->callback,
  1504. wsi, LWS_CALLBACK_HTTP_CONFIRM_UPGRADE,
  1505. wsi->user_space, (char *)up, 0);
  1506. /* just hang up? */
  1507. if (n < 0)
  1508. goto bail_nuke_ah;
  1509. /* callback returned headers already, do t_c? */
  1510. if (n > 0) {
  1511. if (lws_http_transaction_completed(wsi))
  1512. goto bail_nuke_ah;
  1513. /* continue on */
  1514. return 0;
  1515. }
  1516. /* callback said 0, it was allowed */
  1517. if (wsi->vhost->options &
  1518. LWS_SERVER_OPTION_VHOST_UPG_STRICT_HOST_CHECK &&
  1519. lws_confirm_host_header(wsi))
  1520. goto bail_nuke_ah;
  1521. if (!strcasecmp(up, "websocket")) {
  1522. #if defined(LWS_ROLE_WS)
  1523. wsi->vhost->conn_stats.ws_upg++;
  1524. lwsl_info("Upgrade to ws\n");
  1525. goto upgrade_ws;
  1526. #endif
  1527. }
  1528. #if defined(LWS_WITH_HTTP2)
  1529. if (!strcasecmp(up, "h2c")) {
  1530. wsi->vhost->conn_stats.h2_upg++;
  1531. lwsl_info("Upgrade to h2c\n");
  1532. goto upgrade_h2c;
  1533. }
  1534. #endif
  1535. }
  1536. /* no upgrade ack... he remained as HTTP */
  1537. lwsl_info("%s: %p: No upgrade\n", __func__, wsi);
  1538. lwsi_set_state(wsi, LRS_ESTABLISHED);
  1539. wsi->http.fop_fd = NULL;
  1540. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1541. lws_http_compression_validate(wsi);
  1542. #endif
  1543. lwsl_debug("%s: wsi %p: ah %p\n", __func__, (void *)wsi,
  1544. (void *)wsi->http.ah);
  1545. n = lws_http_action(wsi);
  1546. return n;
  1547. #if defined(LWS_WITH_HTTP2)
  1548. upgrade_h2c:
  1549. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HTTP2_SETTINGS)) {
  1550. lwsl_info("missing http2_settings\n");
  1551. goto bail_nuke_ah;
  1552. }
  1553. lwsl_info("h2c upgrade...\n");
  1554. p = lws_hdr_simple_ptr(wsi, WSI_TOKEN_HTTP2_SETTINGS);
  1555. /* convert the peer's HTTP-Settings */
  1556. n = lws_b64_decode_string(p, tbuf, sizeof(tbuf));
  1557. if (n < 0) {
  1558. lwsl_parser("HTTP2_SETTINGS too long\n");
  1559. return 1;
  1560. }
  1561. /* adopt the header info */
  1562. if (!wsi->h2.h2n) {
  1563. wsi->h2.h2n = lws_zalloc(sizeof(*wsi->h2.h2n),
  1564. "h2n");
  1565. if (!wsi->h2.h2n)
  1566. return 1;
  1567. }
  1568. lws_h2_init(wsi);
  1569. /* HTTP2 union */
  1570. lws_h2_settings(wsi, &wsi->h2.h2n->set, (unsigned char *)tbuf, n);
  1571. lws_hpack_dynamic_size(wsi, wsi->h2.h2n->set.s[
  1572. H2SET_HEADER_TABLE_SIZE]);
  1573. strcpy(tbuf, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
  1574. "Connection: Upgrade\x0d\x0a"
  1575. "Upgrade: h2c\x0d\x0a\x0d\x0a");
  1576. m = (int)strlen(tbuf);
  1577. n = lws_issue_raw(wsi, (unsigned char *)tbuf, m);
  1578. if (n != m) {
  1579. lwsl_debug("http2 switch: ERROR writing to socket\n");
  1580. return 1;
  1581. }
  1582. lwsi_set_state(wsi, LRS_H2_AWAIT_PREFACE);
  1583. wsi->upgraded_to_http2 = 1;
  1584. return 0;
  1585. #endif
  1586. #if defined(LWS_ROLE_WS)
  1587. upgrade_ws:
  1588. if (lws_process_ws_upgrade(wsi))
  1589. goto bail_nuke_ah;
  1590. return 0;
  1591. #endif
  1592. } /* while all chars are handled */
  1593. return 0;
  1594. bail_nuke_ah:
  1595. /* drop the header info */
  1596. lws_header_table_detach(wsi, 1);
  1597. return 1;
  1598. }
  1599. LWS_VISIBLE int LWS_WARN_UNUSED_RESULT
  1600. lws_http_transaction_completed(struct lws *wsi)
  1601. {
  1602. int n = NO_PENDING_TIMEOUT;
  1603. if (lws_has_buffered_out(wsi)
  1604. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1605. || wsi->http.comp_ctx.buflist_comp ||
  1606. wsi->http.comp_ctx.may_have_more
  1607. #endif
  1608. ) {
  1609. /*
  1610. * ...so he tried to send something large as the http reply,
  1611. * it went as a partial, but he immediately said the
  1612. * transaction was completed.
  1613. *
  1614. * Defer the transaction completed until the last part of the
  1615. * partial is sent.
  1616. */
  1617. lwsl_debug("%s: %p: deferring due to partial\n", __func__, wsi);
  1618. wsi->http.deferred_transaction_completed = 1;
  1619. lws_callback_on_writable(wsi);
  1620. return 0;
  1621. }
  1622. lwsl_info("%s: wsi %p\n", __func__, wsi);
  1623. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1624. lws_http_compression_destroy(wsi);
  1625. #endif
  1626. lws_access_log(wsi);
  1627. if (!wsi->hdr_parsing_completed) {
  1628. char peer[64];
  1629. lws_get_peer_simple(wsi, peer, sizeof(peer) - 1);
  1630. peer[sizeof(peer) - 1] = '\0';
  1631. lwsl_notice("%s: (from %s) ignoring, ah parsing incomplete\n",
  1632. __func__, peer);
  1633. return 0;
  1634. }
  1635. /* if we can't go back to accept new headers, drop the connection */
  1636. if (wsi->http2_substream)
  1637. return 1;
  1638. if (wsi->seen_zero_length_recv)
  1639. return 1;
  1640. if (wsi->http.conn_type != HTTP_CONNECTION_KEEP_ALIVE) {
  1641. lwsl_info("%s: %p: close connection\n", __func__, wsi);
  1642. return 1;
  1643. }
  1644. if (lws_bind_protocol(wsi, &wsi->vhost->protocols[0], __func__))
  1645. return 1;
  1646. /*
  1647. * otherwise set ourselves up ready to go again, but because we have no
  1648. * idea about the wsi writability, we make put it in a holding state
  1649. * until we can verify POLLOUT. The part of this that confirms POLLOUT
  1650. * with no partials is in lws_server_socket_service() below.
  1651. */
  1652. lwsl_debug("%s: %p: setting DEF_ACT from 0x%x\n", __func__,
  1653. wsi, wsi->wsistate);
  1654. lwsi_set_state(wsi, LRS_DEFERRING_ACTION);
  1655. wsi->http.tx_content_length = 0;
  1656. wsi->http.tx_content_remain = 0;
  1657. wsi->hdr_parsing_completed = 0;
  1658. wsi->sending_chunked = 0;
  1659. #ifdef LWS_WITH_ACCESS_LOG
  1660. wsi->http.access_log.sent = 0;
  1661. #endif
  1662. if (wsi->vhost->keepalive_timeout)
  1663. n = PENDING_TIMEOUT_HTTP_KEEPALIVE_IDLE;
  1664. lws_set_timeout(wsi, n, wsi->vhost->keepalive_timeout);
  1665. /*
  1666. * We already know we are on http1.1 / keepalive and the next thing
  1667. * coming will be another header set.
  1668. *
  1669. * If there is no pending rx and we still have the ah, drop it and
  1670. * reacquire a new ah when the new headers start to arrive. (Otherwise
  1671. * we needlessly hog an ah indefinitely.)
  1672. *
  1673. * However if there is pending rx and we know from the keepalive state
  1674. * that is already at least the start of another header set, simply
  1675. * reset the existing header table and keep it.
  1676. */
  1677. if (wsi->http.ah) {
  1678. // lws_buflist_describe(&wsi->buflist, wsi);
  1679. if (!lws_buflist_next_segment_len(&wsi->buflist, NULL)) {
  1680. lwsl_debug("%s: %p: nothing in buflist, detaching ah\n",
  1681. __func__, wsi);
  1682. lws_header_table_detach(wsi, 1);
  1683. #ifdef LWS_WITH_TLS
  1684. /*
  1685. * additionally... if we are hogging an SSL instance
  1686. * with no pending pipelined headers (or ah now), and
  1687. * SSL is scarce, drop this connection without waiting
  1688. */
  1689. if (wsi->vhost->tls.use_ssl &&
  1690. wsi->context->simultaneous_ssl_restriction &&
  1691. wsi->context->simultaneous_ssl ==
  1692. wsi->context->simultaneous_ssl_restriction) {
  1693. lwsl_info("%s: simultaneous_ssl_restriction\n",
  1694. __func__);
  1695. return 1;
  1696. }
  1697. #endif
  1698. } else {
  1699. lwsl_info("%s: %p: resetting/keeping ah as pipeline\n",
  1700. __func__, wsi);
  1701. lws_header_table_reset(wsi, 0);
  1702. /*
  1703. * If we kept the ah, we should restrict the amount
  1704. * of time we are willing to keep it. Otherwise it
  1705. * will be bound the whole time the connection remains
  1706. * open.
  1707. */
  1708. lws_set_timeout(wsi, PENDING_TIMEOUT_HOLDING_AH,
  1709. wsi->vhost->keepalive_timeout);
  1710. }
  1711. /* If we're (re)starting on headers, need other implied init */
  1712. if (wsi->http.ah)
  1713. wsi->http.ah->ues = URIES_IDLE;
  1714. //lwsi_set_state(wsi, LRS_ESTABLISHED); // !!!
  1715. } else
  1716. if (lws_buflist_next_segment_len(&wsi->buflist, NULL))
  1717. if (lws_header_table_attach(wsi, 0))
  1718. lwsl_debug("acquired ah\n");
  1719. lwsl_debug("%s: %p: keep-alive await new transaction (state 0x%x)\n",
  1720. __func__, wsi, wsi->wsistate);
  1721. lws_callback_on_writable(wsi);
  1722. return 0;
  1723. }
  1724. LWS_VISIBLE int
  1725. lws_serve_http_file(struct lws *wsi, const char *file, const char *content_type,
  1726. const char *other_headers, int other_headers_len)
  1727. {
  1728. struct lws_context *context = lws_get_context(wsi);
  1729. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  1730. unsigned char *response = pt->serv_buf + LWS_PRE;
  1731. #if defined(LWS_WITH_RANGES)
  1732. struct lws_range_parsing *rp = &wsi->http.range;
  1733. #endif
  1734. int ret = 0, cclen = 8, n = HTTP_STATUS_OK;
  1735. char cache_control[50], *cc = "no-store";
  1736. lws_fop_flags_t fflags = LWS_O_RDONLY;
  1737. const struct lws_plat_file_ops *fops;
  1738. lws_filepos_t total_content_length;
  1739. unsigned char *p = response;
  1740. unsigned char *end = p + context->pt_serv_buf_size - LWS_PRE;
  1741. const char *vpath;
  1742. #if defined(LWS_WITH_RANGES)
  1743. int ranges;
  1744. #endif
  1745. if (wsi->handling_404)
  1746. n = HTTP_STATUS_NOT_FOUND;
  1747. /*
  1748. * We either call the platform fops .open with first arg platform fops,
  1749. * or we call fops_zip .open with first arg platform fops, and fops_zip
  1750. * open will decide whether to switch to fops_zip or stay with fops_def.
  1751. *
  1752. * If wsi->http.fop_fd is already set, the caller already opened it
  1753. */
  1754. if (!wsi->http.fop_fd) {
  1755. fops = lws_vfs_select_fops(wsi->context->fops, file, &vpath);
  1756. fflags |= lws_vfs_prepare_flags(wsi);
  1757. wsi->http.fop_fd = fops->LWS_FOP_OPEN(wsi->context->fops,
  1758. file, vpath, &fflags);
  1759. if (!wsi->http.fop_fd) {
  1760. lwsl_info("%s: Unable to open: '%s': errno %d\n",
  1761. __func__, file, errno);
  1762. if (lws_return_http_status(wsi, HTTP_STATUS_NOT_FOUND,
  1763. NULL))
  1764. return -1;
  1765. return !wsi->http2_substream;
  1766. }
  1767. }
  1768. wsi->http.filelen = lws_vfs_get_length(wsi->http.fop_fd);
  1769. total_content_length = wsi->http.filelen;
  1770. #if defined(LWS_WITH_RANGES)
  1771. ranges = lws_ranges_init(wsi, rp, wsi->http.filelen);
  1772. lwsl_debug("Range count %d\n", ranges);
  1773. /*
  1774. * no ranges -> 200;
  1775. * 1 range -> 206 + Content-Type: normal; Content-Range;
  1776. * more -> 206 + Content-Type: multipart/byteranges
  1777. * Repeat the true Content-Type in each multipart header
  1778. * along with Content-Range
  1779. */
  1780. if (ranges < 0) {
  1781. /* it means he expressed a range in Range:, but it was illegal */
  1782. lws_return_http_status(wsi,
  1783. HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, NULL);
  1784. if (lws_http_transaction_completed(wsi))
  1785. return -1; /* <0 means just hang up */
  1786. lws_vfs_file_close(&wsi->http.fop_fd);
  1787. return 0; /* == 0 means we did the transaction complete */
  1788. }
  1789. if (ranges)
  1790. n = HTTP_STATUS_PARTIAL_CONTENT;
  1791. #endif
  1792. if (lws_add_http_header_status(wsi, n, &p, end))
  1793. return -1;
  1794. if ((wsi->http.fop_fd->flags & (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP |
  1795. LWS_FOP_FLAG_COMPR_IS_GZIP)) ==
  1796. (LWS_FOP_FLAG_COMPR_ACCEPTABLE_GZIP | LWS_FOP_FLAG_COMPR_IS_GZIP)) {
  1797. if (lws_add_http_header_by_token(wsi,
  1798. WSI_TOKEN_HTTP_CONTENT_ENCODING,
  1799. (unsigned char *)"gzip", 4, &p, end))
  1800. return -1;
  1801. lwsl_info("file is being provided in gzip\n");
  1802. }
  1803. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1804. else {
  1805. /*
  1806. * if we know its very compressible, and we can use
  1807. * compression, then use the most preferred compression
  1808. * method that the client said he will accept
  1809. */
  1810. if (!strncmp(content_type, "text/", 5) ||
  1811. !strcmp(content_type, "application/javascript") ||
  1812. !strcmp(content_type, "image/svg+xml"))
  1813. lws_http_compression_apply(wsi, NULL, &p, end, 0);
  1814. }
  1815. #endif
  1816. if (
  1817. #if defined(LWS_WITH_RANGES)
  1818. ranges < 2 &&
  1819. #endif
  1820. content_type && content_type[0])
  1821. if (lws_add_http_header_by_token(wsi,
  1822. WSI_TOKEN_HTTP_CONTENT_TYPE,
  1823. (unsigned char *)content_type,
  1824. (int)strlen(content_type),
  1825. &p, end))
  1826. return -1;
  1827. #if defined(LWS_WITH_RANGES)
  1828. if (ranges >= 2) { /* multipart byteranges */
  1829. lws_strncpy(wsi->http.multipart_content_type, content_type,
  1830. sizeof(wsi->http.multipart_content_type));
  1831. if (lws_add_http_header_by_token(wsi,
  1832. WSI_TOKEN_HTTP_CONTENT_TYPE,
  1833. (unsigned char *)
  1834. "multipart/byteranges; "
  1835. "boundary=_lws",
  1836. 20, &p, end))
  1837. return -1;
  1838. /*
  1839. * our overall content length has to include
  1840. *
  1841. * - (n + 1) x "_lws\r\n"
  1842. * - n x Content-Type: xxx/xxx\r\n
  1843. * - n x Content-Range: bytes xxx-yyy/zzz\r\n
  1844. * - n x /r/n
  1845. * - the actual payloads (aggregated in rp->agg)
  1846. *
  1847. * Precompute it for the main response header
  1848. */
  1849. total_content_length = (lws_filepos_t)rp->agg +
  1850. 6 /* final _lws\r\n */;
  1851. lws_ranges_reset(rp);
  1852. while (lws_ranges_next(rp)) {
  1853. n = lws_snprintf(cache_control, sizeof(cache_control),
  1854. "bytes %llu-%llu/%llu",
  1855. rp->start, rp->end, rp->extent);
  1856. total_content_length +=
  1857. 6 /* header _lws\r\n */ +
  1858. /* Content-Type: xxx/xxx\r\n */
  1859. 14 + strlen(content_type) + 2 +
  1860. /* Content-Range: xxxx\r\n */
  1861. 15 + n + 2 +
  1862. 2; /* /r/n */
  1863. }
  1864. lws_ranges_reset(rp);
  1865. lws_ranges_next(rp);
  1866. }
  1867. if (ranges == 1) {
  1868. total_content_length = (lws_filepos_t)rp->agg;
  1869. n = lws_snprintf(cache_control, sizeof(cache_control),
  1870. "bytes %llu-%llu/%llu",
  1871. rp->start, rp->end, rp->extent);
  1872. if (lws_add_http_header_by_token(wsi,
  1873. WSI_TOKEN_HTTP_CONTENT_RANGE,
  1874. (unsigned char *)cache_control,
  1875. n, &p, end))
  1876. return -1;
  1877. }
  1878. wsi->http.range.inside = 0;
  1879. if (lws_add_http_header_by_token(wsi, WSI_TOKEN_HTTP_ACCEPT_RANGES,
  1880. (unsigned char *)"bytes", 5, &p, end))
  1881. return -1;
  1882. #endif
  1883. if (!wsi->http2_substream) {
  1884. /* for http/1.1 ... */
  1885. if (!wsi->sending_chunked
  1886. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1887. && !wsi->http.lcs
  1888. #endif
  1889. ) {
  1890. /* ... if not already using chunked and not using an
  1891. * http compression translation, then send the naive
  1892. * content length
  1893. */
  1894. if (lws_add_http_header_content_length(wsi,
  1895. total_content_length, &p, end))
  1896. return -1;
  1897. } else {
  1898. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1899. if (wsi->http.lcs) {
  1900. /* ...otherwise, for http 1 it must go chunked.
  1901. * For the compression case, the reason is we
  1902. * compress on the fly and do not know the
  1903. * compressed content-length until it has all
  1904. * been sent. Http/1.1 pipelining must be able
  1905. * to know where the transaction boundaries are
  1906. * ... so chunking...
  1907. */
  1908. if (lws_add_http_header_by_token(wsi,
  1909. WSI_TOKEN_HTTP_TRANSFER_ENCODING,
  1910. (unsigned char *)"chunked", 7,
  1911. &p, end))
  1912. return -1;
  1913. /*
  1914. * ...this is fun, isn't it :-) For h1 that is
  1915. * using an http compression translation, the
  1916. * compressor must chunk its output privately.
  1917. *
  1918. * h2 doesn't need (or support) any of this
  1919. * crap.
  1920. */
  1921. lwsl_debug("setting chunking\n");
  1922. wsi->http.comp_ctx.chunking = 1;
  1923. }
  1924. #endif
  1925. }
  1926. }
  1927. if (wsi->cache_secs && wsi->cache_reuse) {
  1928. if (!wsi->cache_revalidate) {
  1929. cc = cache_control;
  1930. cclen = sprintf(cache_control, "%s, max-age=%u",
  1931. intermediates[wsi->cache_intermediaries],
  1932. wsi->cache_secs);
  1933. } else {
  1934. cc = cache_control;
  1935. cclen = sprintf(cache_control,
  1936. "must-revalidate, %s, max-age=%u",
  1937. intermediates[wsi->cache_intermediaries],
  1938. wsi->cache_secs);
  1939. }
  1940. }
  1941. /* Only add cache control if its not specified by any other_headers. */
  1942. if (!other_headers ||
  1943. (!strstr(other_headers, "cache-control") &&
  1944. !strstr(other_headers, "Cache-Control"))) {
  1945. if (lws_add_http_header_by_token(wsi,
  1946. WSI_TOKEN_HTTP_CACHE_CONTROL,
  1947. (unsigned char *)cc, cclen, &p, end))
  1948. return -1;
  1949. }
  1950. if (other_headers) {
  1951. if ((end - p) < other_headers_len)
  1952. return -1;
  1953. memcpy(p, other_headers, other_headers_len);
  1954. p += other_headers_len;
  1955. }
  1956. if (lws_finalize_http_header(wsi, &p, end))
  1957. return -1;
  1958. ret = lws_write(wsi, response, p - response, LWS_WRITE_HTTP_HEADERS);
  1959. if (ret != (p - response)) {
  1960. lwsl_err("_write returned %d from %ld\n", ret,
  1961. (long)(p - response));
  1962. return -1;
  1963. }
  1964. wsi->http.filepos = 0;
  1965. lwsi_set_state(wsi, LRS_ISSUING_FILE);
  1966. lws_callback_on_writable(wsi);
  1967. return 0;
  1968. }
  1969. LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
  1970. {
  1971. struct lws_context *context = wsi->context;
  1972. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  1973. struct lws_process_html_args args;
  1974. lws_filepos_t amount, poss;
  1975. unsigned char *p, *pstart;
  1976. #if defined(LWS_WITH_RANGES)
  1977. unsigned char finished = 0;
  1978. #endif
  1979. int n, m;
  1980. lwsl_debug("wsi->http2_substream %d\n", wsi->http2_substream);
  1981. do {
  1982. /* priority 1: buffered output */
  1983. if (lws_has_buffered_out(wsi)) {
  1984. if (lws_issue_raw(wsi, NULL, 0) < 0) {
  1985. lwsl_info("%s: closing\n", __func__);
  1986. goto file_had_it;
  1987. }
  1988. break;
  1989. }
  1990. /* priority 2: buffered pre-compression-transform */
  1991. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  1992. if (wsi->http.comp_ctx.buflist_comp ||
  1993. wsi->http.comp_ctx.may_have_more) {
  1994. enum lws_write_protocol wp = LWS_WRITE_HTTP;
  1995. lwsl_info("%s: completing comp partial (buflist %p, may %d)\n",
  1996. __func__, wsi->http.comp_ctx.buflist_comp,
  1997. wsi->http.comp_ctx.may_have_more);
  1998. if (wsi->role_ops->write_role_protocol(wsi, NULL, 0, &wp) < 0) {
  1999. lwsl_info("%s signalling to close\n", __func__);
  2000. goto file_had_it;
  2001. }
  2002. lws_callback_on_writable(wsi);
  2003. break;
  2004. }
  2005. #endif
  2006. if (wsi->http.filepos == wsi->http.filelen)
  2007. goto all_sent;
  2008. n = 0;
  2009. pstart = pt->serv_buf + LWS_H2_FRAME_HEADER_LENGTH;
  2010. p = pstart;
  2011. #if defined(LWS_WITH_RANGES)
  2012. if (wsi->http.range.count_ranges && !wsi->http.range.inside) {
  2013. lwsl_notice("%s: doing range start %llu\n", __func__,
  2014. wsi->http.range.start);
  2015. if ((long long)lws_vfs_file_seek_cur(wsi->http.fop_fd,
  2016. wsi->http.range.start -
  2017. wsi->http.filepos) < 0)
  2018. goto file_had_it;
  2019. wsi->http.filepos = wsi->http.range.start;
  2020. if (wsi->http.range.count_ranges > 1) {
  2021. n = lws_snprintf((char *)p,
  2022. context->pt_serv_buf_size -
  2023. LWS_H2_FRAME_HEADER_LENGTH,
  2024. "_lws\x0d\x0a"
  2025. "Content-Type: %s\x0d\x0a"
  2026. "Content-Range: bytes "
  2027. "%llu-%llu/%llu\x0d\x0a"
  2028. "\x0d\x0a",
  2029. wsi->http.multipart_content_type,
  2030. wsi->http.range.start,
  2031. wsi->http.range.end,
  2032. wsi->http.range.extent);
  2033. p += n;
  2034. }
  2035. wsi->http.range.budget = wsi->http.range.end -
  2036. wsi->http.range.start + 1;
  2037. wsi->http.range.inside = 1;
  2038. }
  2039. #endif
  2040. poss = context->pt_serv_buf_size - n -
  2041. LWS_H2_FRAME_HEADER_LENGTH;
  2042. if (wsi->http.tx_content_length)
  2043. if (poss > wsi->http.tx_content_remain)
  2044. poss = wsi->http.tx_content_remain;
  2045. /*
  2046. * if there is a hint about how much we will do well to send at
  2047. * one time, restrict ourselves to only trying to send that.
  2048. */
  2049. if (wsi->protocol->tx_packet_size &&
  2050. poss > wsi->protocol->tx_packet_size)
  2051. poss = wsi->protocol->tx_packet_size;
  2052. if (wsi->role_ops->tx_credit) {
  2053. lws_filepos_t txc = wsi->role_ops->tx_credit(wsi);
  2054. if (!txc) {
  2055. lwsl_info("%s: came here with no tx credit\n",
  2056. __func__);
  2057. return 0;
  2058. }
  2059. if (txc < poss)
  2060. poss = txc;
  2061. /*
  2062. * consumption of the actual payload amount sent will be
  2063. * handled when the role data frame is sent
  2064. */
  2065. }
  2066. #if defined(LWS_WITH_RANGES)
  2067. if (wsi->http.range.count_ranges) {
  2068. if (wsi->http.range.count_ranges > 1)
  2069. poss -= 7; /* allow for final boundary */
  2070. if (poss > wsi->http.range.budget)
  2071. poss = wsi->http.range.budget;
  2072. }
  2073. #endif
  2074. if (wsi->sending_chunked) {
  2075. /* we need to drop the chunk size in here */
  2076. p += 10;
  2077. /* allow for the chunk to grow by 128 in translation */
  2078. poss -= 10 + 128;
  2079. }
  2080. if (lws_vfs_file_read(wsi->http.fop_fd, &amount, p, poss) < 0)
  2081. goto file_had_it; /* caller will close */
  2082. if (wsi->sending_chunked)
  2083. n = (int)amount;
  2084. else
  2085. n = lws_ptr_diff(p, pstart) + (int)amount;
  2086. lwsl_debug("%s: sending %d\n", __func__, n);
  2087. if (n) {
  2088. lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
  2089. context->timeout_secs);
  2090. if (wsi->interpreting) {
  2091. args.p = (char *)p;
  2092. args.len = n;
  2093. args.max_len = (unsigned int)poss + 128;
  2094. args.final = wsi->http.filepos + n ==
  2095. wsi->http.filelen;
  2096. args.chunked = wsi->sending_chunked;
  2097. if (user_callback_handle_rxflow(
  2098. wsi->vhost->protocols[
  2099. (int)wsi->protocol_interpret_idx].callback,
  2100. wsi, LWS_CALLBACK_PROCESS_HTML,
  2101. wsi->user_space, &args, 0) < 0)
  2102. goto file_had_it;
  2103. n = args.len;
  2104. p = (unsigned char *)args.p;
  2105. } else
  2106. p = pstart;
  2107. #if defined(LWS_WITH_RANGES)
  2108. if (wsi->http.range.send_ctr + 1 ==
  2109. wsi->http.range.count_ranges && // last range
  2110. wsi->http.range.count_ranges > 1 && // was 2+ ranges (ie, multipart)
  2111. wsi->http.range.budget - amount == 0) {// final part
  2112. n += lws_snprintf((char *)pstart + n, 6,
  2113. "_lws\x0d\x0a"); // append trailing boundary
  2114. lwsl_debug("added trailing boundary\n");
  2115. }
  2116. #endif
  2117. m = lws_write(wsi, p, n, wsi->http.filepos + amount ==
  2118. wsi->http.filelen ?
  2119. LWS_WRITE_HTTP_FINAL : LWS_WRITE_HTTP);
  2120. if (m < 0)
  2121. goto file_had_it;
  2122. wsi->http.filepos += amount;
  2123. #if defined(LWS_WITH_RANGES)
  2124. if (wsi->http.range.count_ranges >= 1) {
  2125. wsi->http.range.budget -= amount;
  2126. if (wsi->http.range.budget == 0) {
  2127. lwsl_notice("range budget exhausted\n");
  2128. wsi->http.range.inside = 0;
  2129. wsi->http.range.send_ctr++;
  2130. if (lws_ranges_next(&wsi->http.range) < 1) {
  2131. finished = 1;
  2132. goto all_sent;
  2133. }
  2134. }
  2135. }
  2136. #endif
  2137. if (m != n) {
  2138. /* adjust for what was not sent */
  2139. if (lws_vfs_file_seek_cur(wsi->http.fop_fd,
  2140. m - n) ==
  2141. (lws_fileofs_t)-1)
  2142. goto file_had_it;
  2143. }
  2144. }
  2145. all_sent:
  2146. if ((!lws_has_buffered_out(wsi)
  2147. #if defined(LWS_WITH_HTTP_STREAM_COMPRESSION)
  2148. && !wsi->http.comp_ctx.buflist_comp &&
  2149. !wsi->http.comp_ctx.may_have_more
  2150. #endif
  2151. ) && (wsi->http.filepos >= wsi->http.filelen
  2152. #if defined(LWS_WITH_RANGES)
  2153. || finished)
  2154. #else
  2155. )
  2156. #endif
  2157. )
  2158. {
  2159. lwsi_set_state(wsi, LRS_ESTABLISHED);
  2160. /* we might be in keepalive, so close it off here */
  2161. lws_vfs_file_close(&wsi->http.fop_fd);
  2162. lwsl_debug("file completed\n");
  2163. if (wsi->protocol->callback &&
  2164. user_callback_handle_rxflow(wsi->protocol->callback,
  2165. wsi, LWS_CALLBACK_HTTP_FILE_COMPLETION,
  2166. wsi->user_space, NULL, 0) < 0) {
  2167. /*
  2168. * For http/1.x, the choices from
  2169. * transaction_completed are either
  2170. * 0 to use the connection for pipelined
  2171. * or nonzero to hang it up.
  2172. *
  2173. * However for http/2. while we are
  2174. * still interested in hanging up the
  2175. * nwsi if there was a network-level
  2176. * fatal error, simply completing the
  2177. * transaction is a matter of the stream
  2178. * state, not the root connection at the
  2179. * network level
  2180. */
  2181. if (wsi->http2_substream)
  2182. return 1;
  2183. else
  2184. return -1;
  2185. }
  2186. return 1; /* >0 indicates completed */
  2187. }
  2188. } while (1); //(!lws_send_pipe_choked(wsi));
  2189. lws_callback_on_writable(wsi);
  2190. return 0; /* indicates further processing must be done */
  2191. file_had_it:
  2192. lws_vfs_file_close(&wsi->http.fop_fd);
  2193. return -1;
  2194. }
  2195. LWS_VISIBLE void
  2196. lws_server_get_canonical_hostname(struct lws_context *context,
  2197. const struct lws_context_creation_info *info)
  2198. {
  2199. if (lws_check_opt(info->options,
  2200. LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME))
  2201. return;
  2202. #if !defined(LWS_WITH_ESP32)
  2203. /* find canonical hostname */
  2204. gethostname((char *)context->canonical_hostname,
  2205. sizeof(context->canonical_hostname) - 1);
  2206. lwsl_info(" canonical_hostname = %s\n", context->canonical_hostname);
  2207. #else
  2208. (void)context;
  2209. #endif
  2210. }
  2211. LWS_VISIBLE LWS_EXTERN int
  2212. lws_chunked_html_process(struct lws_process_html_args *args,
  2213. struct lws_process_html_state *s)
  2214. {
  2215. char *sp, buffer[32];
  2216. const char *pc;
  2217. int old_len, n;
  2218. /* do replacements */
  2219. sp = args->p;
  2220. old_len = args->len;
  2221. args->len = 0;
  2222. s->start = sp;
  2223. while (sp < args->p + old_len) {
  2224. if (args->len + 7 >= args->max_len) {
  2225. lwsl_err("Used up interpret padding\n");
  2226. return -1;
  2227. }
  2228. if ((!s->pos && *sp == '$') || s->pos) {
  2229. int hits = 0, hit = 0;
  2230. if (!s->pos)
  2231. s->start = sp;
  2232. s->swallow[s->pos++] = *sp;
  2233. if (s->pos == sizeof(s->swallow) - 1)
  2234. goto skip;
  2235. for (n = 0; n < s->count_vars; n++)
  2236. if (!strncmp(s->swallow, s->vars[n], s->pos)) {
  2237. hits++;
  2238. hit = n;
  2239. }
  2240. if (!hits) {
  2241. skip:
  2242. s->swallow[s->pos] = '\0';
  2243. memcpy(s->start, s->swallow, s->pos);
  2244. args->len++;
  2245. s->pos = 0;
  2246. sp = s->start + 1;
  2247. continue;
  2248. }
  2249. if (hits == 1 && s->pos == (int)strlen(s->vars[hit])) {
  2250. pc = s->replace(s->data, hit);
  2251. if (!pc)
  2252. pc = "NULL";
  2253. n = (int)strlen(pc);
  2254. s->swallow[s->pos] = '\0';
  2255. if (n != s->pos) {
  2256. memmove(s->start + n, s->start + s->pos,
  2257. old_len - (sp - args->p));
  2258. old_len += (n - s->pos) + 1;
  2259. }
  2260. memcpy(s->start, pc, n);
  2261. args->len++;
  2262. sp = s->start + 1;
  2263. s->pos = 0;
  2264. }
  2265. sp++;
  2266. continue;
  2267. }
  2268. args->len++;
  2269. sp++;
  2270. }
  2271. if (args->chunked) {
  2272. /* no space left for final chunk trailer */
  2273. if (args->final && args->len + 7 >= args->max_len)
  2274. return -1;
  2275. n = sprintf(buffer, "%X\x0d\x0a", args->len);
  2276. args->p -= n;
  2277. memcpy(args->p, buffer, n);
  2278. args->len += n;
  2279. if (args->final) {
  2280. sp = args->p + args->len;
  2281. *sp++ = '\x0d';
  2282. *sp++ = '\x0a';
  2283. *sp++ = '0';
  2284. *sp++ = '\x0d';
  2285. *sp++ = '\x0a';
  2286. *sp++ = '\x0d';
  2287. *sp++ = '\x0a';
  2288. args->len += 7;
  2289. } else {
  2290. sp = args->p + args->len;
  2291. *sp++ = '\x0d';
  2292. *sp++ = '\x0a';
  2293. args->len += 2;
  2294. }
  2295. }
  2296. return 0;
  2297. }