MCPcopy Create free account
hub / github.com/ElementsProject/lightning / http_respond

Function http_respond

connectd/websocketd.c:129–184  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

127}
128
129static void http_respond(int fd, const u8 *buf, size_t len)
130{
131 const char *hdr;
132 char *resp;
133
134 /* RFC-6455:
135
136 The client's opening handshake consists of the following
137 parts. If the server, while reading the handshake, finds
138 that the client did not send a handshake that matches the
139 description below ... the server MUST stop processing the
140 client's handshake and return an HTTP response with an
141 appropriate error code (such as 400 Bad Request).
142
143 1. An HTTP/1.1 or higher GET request, including a "Request-URI"
144 [RFC2616] that should be interpreted as a /resource name/
145 defined in Section 3 (or an absolute HTTP/HTTPS URI containing
146 the /resource name/).
147
148 2. A |Host| header field containing the server's authority.
149
150 3. An |Upgrade| header field containing the value "websocket",
151 treated as an ASCII case-insensitive value.
152
153 4. A |Connection| header field that includes the token "Upgrade",
154 treated as an ASCII case-insensitive value.
155
156 5. A |Sec-WebSocket-Key| header field with a base64-encoded (see
157 Section 4 of [RFC4648]) value that, when decoded, is 16 bytes in
158 length.
159
160 6. A |Sec-WebSocket-Version| header field, with a value of 13.
161 */
162 hdr = get_http_hdr(tmpctx, buf, len, "Upgrade");
163 if (!hdr || !strstr(hdr, "websocket"))
164 bad_http(fd, "Upgrade: websocket missing");
165 hdr = get_http_hdr(tmpctx, buf, len, "Connection");
166 if (!hdr || !strstr(hdr, "Upgrade"))
167 bad_http(fd, "Connection: Upgrade missing");
168 hdr = get_http_hdr(tmpctx, buf, len, "Sec-WebSocket-Version");
169 if (!hdr || !streq(hdr, "13"))
170 bad_http(fd, "Sec-WebSocket-Version: must be 13");
171 hdr = get_http_hdr(tmpctx, buf, len, "Sec-WebSocket-Key");
172 if (!hdr)
173 bad_http(fd, "Sec-WebSocket-Key missing");
174
175 resp = tal_fmt(tmpctx,
176 "HTTP/1.1 101 Switching Protocols\r\n"
177 "Upgrade: websocket\r\n"
178 "Connection: Upgrade\r\n"
179 "Sec-WebSocket-Accept: %s\r\n\r\n",
180 websocket_accept_str(tmpctx, hdr));
181
182 if (!write_all(fd, resp, strlen(resp)))
183 exit(0);
184}
185
186static void http_upgrade(int fd)

Callers 1

http_upgradeFunction · 0.85

Calls 3

get_http_hdrFunction · 0.85
websocket_accept_strFunction · 0.85
write_allFunction · 0.50

Tested by

no test coverage detected