server-handshake.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * libwebsockets - small server side websockets and web server implementation
  3. *
  4. * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com>
  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 "private-libwebsockets.h"
  22. #define LWS_CPYAPP(ptr, str) { strcpy(ptr, str); ptr += strlen(str); }
  23. #ifndef LWS_NO_EXTENSIONS
  24. static int
  25. lws_extension_server_handshake(struct lws *wsi, char **p, int budget)
  26. {
  27. struct lws_context *context = wsi->context;
  28. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  29. char ext_name[64], *args, *end = (*p) + budget - 1;
  30. const struct lws_ext_options *opts, *po;
  31. const struct lws_extension *ext;
  32. struct lws_ext_option_arg oa;
  33. int n, m, more = 1;
  34. int ext_count = 0;
  35. char ignore;
  36. char *c;
  37. /*
  38. * Figure out which extensions the client has that we want to
  39. * enable on this connection, and give him back the list
  40. */
  41. if (!lws_hdr_total_length(wsi, WSI_TOKEN_EXTENSIONS))
  42. return 0;
  43. /*
  44. * break down the list of client extensions
  45. * and go through them
  46. */
  47. if (lws_hdr_copy(wsi, (char *)pt->serv_buf, context->pt_serv_buf_size,
  48. WSI_TOKEN_EXTENSIONS) < 0)
  49. return 1;
  50. c = (char *)pt->serv_buf;
  51. lwsl_parser("WSI_TOKEN_EXTENSIONS = '%s'\n", c);
  52. wsi->count_act_ext = 0;
  53. ignore = 0;
  54. n = 0;
  55. args = NULL;
  56. /*
  57. * We may get a simple request
  58. *
  59. * Sec-WebSocket-Extensions: permessage-deflate
  60. *
  61. * or an elaborated one with requested options
  62. *
  63. * Sec-WebSocket-Extensions: permessage-deflate; \
  64. * server_no_context_takeover; \
  65. * client_no_context_takeover
  66. */
  67. while (more) {
  68. if (*c && (*c != ',' && *c != '\t')) {
  69. if (*c == ';') {
  70. ignore = 1;
  71. args = c + 1;
  72. }
  73. if (ignore || *c == ' ') {
  74. c++;
  75. continue;
  76. }
  77. ext_name[n] = *c++;
  78. if (n < sizeof(ext_name) - 1)
  79. n++;
  80. continue;
  81. }
  82. ext_name[n] = '\0';
  83. ignore = 0;
  84. if (!*c)
  85. more = 0;
  86. else {
  87. c++;
  88. if (!n)
  89. continue;
  90. }
  91. while (args && *args && *args == ' ')
  92. args++;
  93. /* check a client's extension against our support */
  94. ext = wsi->vhost->extensions;
  95. while (ext && ext->callback) {
  96. if (strcmp(ext_name, ext->name)) {
  97. ext++;
  98. continue;
  99. }
  100. /*
  101. * oh, we do support this one he asked for... but let's
  102. * confirm he only gave it once
  103. */
  104. for (m = 0; m < wsi->count_act_ext; m++)
  105. if (wsi->active_extensions[m] == ext) {
  106. lwsl_info("extension mentioned twice\n");
  107. return 1; /* shenanigans */
  108. }
  109. /*
  110. * ask user code if it's OK to apply it on this
  111. * particular connection + protocol
  112. */
  113. m = wsi->vhost->protocols[0].callback(wsi,
  114. LWS_CALLBACK_CONFIRM_EXTENSION_OKAY,
  115. wsi->user_space, ext_name, 0);
  116. /*
  117. * zero return from callback means go ahead and allow
  118. * the extension, it's what we get if the callback is
  119. * unhandled
  120. */
  121. if (m) {
  122. ext++;
  123. continue;
  124. }
  125. /* apply it */
  126. ext_count++;
  127. /* instantiate the extension on this conn */
  128. wsi->active_extensions[wsi->count_act_ext] = ext;
  129. /* allow him to construct his context */
  130. if (ext->callback(lws_get_context(wsi), ext, wsi,
  131. LWS_EXT_CB_CONSTRUCT,
  132. (void *)&wsi->act_ext_user[
  133. wsi->count_act_ext],
  134. &opts, 0)) {
  135. lwsl_notice("ext %s failed construction\n",
  136. ext_name);
  137. ext_count--;
  138. ext++;
  139. continue;
  140. }
  141. if (ext_count > 1)
  142. *(*p)++ = ',';
  143. else
  144. LWS_CPYAPP(*p,
  145. "\x0d\x0aSec-WebSocket-Extensions: ");
  146. *p += lws_snprintf(*p, (end - *p), "%s", ext_name);
  147. /*
  148. * go through the options trying to apply the
  149. * recognized ones
  150. */
  151. lwsl_debug("ext args %s", args);
  152. while (args && *args && *args != ',') {
  153. while (*args == ' ')
  154. args++;
  155. po = opts;
  156. while (po->name) {
  157. lwsl_debug("'%s' '%s'\n", po->name, args);
  158. /* only support arg-less options... */
  159. if (po->type == EXTARG_NONE &&
  160. !strncmp(args, po->name,
  161. strlen(po->name))) {
  162. oa.option_name = NULL;
  163. oa.option_index = po - opts;
  164. oa.start = NULL;
  165. lwsl_debug("setting %s\n", po->name);
  166. if (!ext->callback(
  167. lws_get_context(wsi), ext, wsi,
  168. LWS_EXT_CB_OPTION_SET,
  169. wsi->act_ext_user[
  170. wsi->count_act_ext],
  171. &oa, (end - *p))) {
  172. *p += lws_snprintf(*p, (end - *p), "; %s", po->name);
  173. lwsl_debug("adding option %s\n", po->name);
  174. }
  175. }
  176. po++;
  177. }
  178. while (*args && *args != ',' && *args != ';')
  179. args++;
  180. }
  181. wsi->count_act_ext++;
  182. lwsl_parser("count_act_ext <- %d\n",
  183. wsi->count_act_ext);
  184. ext++;
  185. }
  186. n = 0;
  187. args = NULL;
  188. }
  189. return 0;
  190. }
  191. #endif
  192. int
  193. handshake_0405(struct lws_context *context, struct lws *wsi)
  194. {
  195. struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
  196. unsigned char hash[20];
  197. int n, accept_len;
  198. char *response;
  199. char *p;
  200. if (!lws_hdr_total_length(wsi, WSI_TOKEN_HOST) ||
  201. !lws_hdr_total_length(wsi, WSI_TOKEN_KEY)) {
  202. lwsl_parser("handshake_04 missing pieces\n");
  203. /* completed header processing, but missing some bits */
  204. goto bail;
  205. }
  206. if (lws_hdr_total_length(wsi, WSI_TOKEN_KEY) >= MAX_WEBSOCKET_04_KEY_LEN) {
  207. lwsl_warn("Client key too long %d\n", MAX_WEBSOCKET_04_KEY_LEN);
  208. goto bail;
  209. }
  210. /*
  211. * since key length is restricted above (currently 128), cannot
  212. * overflow
  213. */
  214. n = sprintf((char *)pt->serv_buf,
  215. "%s258EAFA5-E914-47DA-95CA-C5AB0DC85B11",
  216. lws_hdr_simple_ptr(wsi, WSI_TOKEN_KEY));
  217. lws_SHA1(pt->serv_buf, n, hash);
  218. accept_len = lws_b64_encode_string((char *)hash, 20,
  219. (char *)pt->serv_buf, context->pt_serv_buf_size);
  220. if (accept_len < 0) {
  221. lwsl_warn("Base64 encoded hash too long\n");
  222. goto bail;
  223. }
  224. /* allocate the per-connection user memory (if any) */
  225. if (lws_ensure_user_space(wsi))
  226. goto bail;
  227. /* create the response packet */
  228. /* make a buffer big enough for everything */
  229. response = (char *)pt->serv_buf + MAX_WEBSOCKET_04_KEY_LEN + LWS_PRE;
  230. p = response;
  231. LWS_CPYAPP(p, "HTTP/1.1 101 Switching Protocols\x0d\x0a"
  232. "Upgrade: WebSocket\x0d\x0a"
  233. "Connection: Upgrade\x0d\x0a"
  234. "Sec-WebSocket-Accept: ");
  235. strcpy(p, (char *)pt->serv_buf);
  236. p += accept_len;
  237. /* we can only return the protocol header if:
  238. * - one came in, and ... */
  239. if (lws_hdr_total_length(wsi, WSI_TOKEN_PROTOCOL) &&
  240. /* - it is not an empty string */
  241. wsi->protocol->name &&
  242. wsi->protocol->name[0]) {
  243. LWS_CPYAPP(p, "\x0d\x0aSec-WebSocket-Protocol: ");
  244. p += lws_snprintf(p, 128, "%s", wsi->protocol->name);
  245. }
  246. #ifndef LWS_NO_EXTENSIONS
  247. /*
  248. * Figure out which extensions the client has that we want to
  249. * enable on this connection, and give him back the list.
  250. *
  251. * Give him a limited write bugdet
  252. */
  253. if (lws_extension_server_handshake(wsi, &p, 192))
  254. goto bail;
  255. #endif
  256. //LWS_CPYAPP(p, "\x0d\x0a""An-unknown-header: blah");
  257. /* end of response packet */
  258. LWS_CPYAPP(p, "\x0d\x0a\x0d\x0a");
  259. if (!lws_any_extension_handled(wsi, LWS_EXT_CB_HANDSHAKE_REPLY_TX,
  260. response, p - response)) {
  261. /* okay send the handshake response accepting the connection */
  262. lwsl_parser("issuing resp pkt %d len\n", (int)(p - response));
  263. #if defined(DEBUG) && ! defined(LWS_WITH_ESP8266)
  264. fwrite(response, 1, p - response, stderr);
  265. #endif
  266. n = lws_write(wsi, (unsigned char *)response,
  267. p - response, LWS_WRITE_HTTP_HEADERS);
  268. if (n != (p - response)) {
  269. lwsl_debug("handshake_0405: ERROR writing to socket\n");
  270. goto bail;
  271. }
  272. }
  273. /* alright clean up and set ourselves into established state */
  274. wsi->state = LWSS_ESTABLISHED;
  275. wsi->lws_rx_parse_state = LWS_RXPS_NEW;
  276. {
  277. const char * uri_ptr =
  278. lws_hdr_simple_ptr(wsi, WSI_TOKEN_GET_URI);
  279. int uri_len = lws_hdr_total_length(wsi, WSI_TOKEN_GET_URI);
  280. const struct lws_http_mount *hit =
  281. lws_find_mount(wsi, uri_ptr, uri_len);
  282. if (hit && hit->cgienv &&
  283. wsi->protocol->callback(wsi, LWS_CALLBACK_HTTP_PMO,
  284. wsi->user_space, (void *)hit->cgienv, 0))
  285. return 1;
  286. }
  287. return 0;
  288. bail:
  289. /* caller will free up his parsing allocations */
  290. return -1;
  291. }