diff options
Diffstat (limited to 'src/lib/response_for_upgrade.c')
-rw-r--r-- | src/lib/response_for_upgrade.c | 90 |
1 files changed, 90 insertions, 0 deletions
diff --git a/src/lib/response_for_upgrade.c b/src/lib/response_for_upgrade.c new file mode 100644 index 00000000..90e361b6 --- /dev/null +++ b/src/lib/response_for_upgrade.c | |||
@@ -0,0 +1,90 @@ | |||
1 | /* | ||
2 | This file is part of libmicrohttpd | ||
3 | Copyright (C) 2007-2018 Daniel Pittman and Christian Grothoff | ||
4 | |||
5 | This library is free software; you can redistribute it and/or | ||
6 | modify it under the terms of the GNU Lesser General Public | ||
7 | License as published by the Free Software Foundation; either | ||
8 | version 2.1 of the License, or (at your option) any later version. | ||
9 | |||
10 | This library is distributed in the hope that it will be useful, | ||
11 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
12 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
13 | Lesser General Public License for more details. | ||
14 | |||
15 | You should have received a copy of the GNU Lesser General Public | ||
16 | License along with this library; if not, write to the Free Software | ||
17 | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA | ||
18 | */ | ||
19 | |||
20 | /** | ||
21 | * @file lib/response_for_upgrade.c | ||
22 | * @brief implementation of MHD_response_for_upgrade() | ||
23 | * @author Christian Grothoff | ||
24 | */ | ||
25 | #include "internal.h" | ||
26 | |||
27 | |||
28 | /** | ||
29 | * Create a response object that can be used for 101 UPGRADE | ||
30 | * responses, for example to implement WebSockets. After sending the | ||
31 | * response, control over the data stream is given to the callback (which | ||
32 | * can then, for example, start some bi-directional communication). | ||
33 | * If the response is queued for multiple connections, the callback | ||
34 | * will be called for each connection. The callback | ||
35 | * will ONLY be called after the response header was successfully passed | ||
36 | * to the OS; if there are communication errors before, the usual MHD | ||
37 | * connection error handling code will be performed. | ||
38 | * | ||
39 | * MHD will automatically set the correct HTTP status | ||
40 | * code (#MHD_HTTP_SWITCHING_PROTOCOLS). | ||
41 | * Setting correct HTTP headers for the upgrade must be done | ||
42 | * manually (this way, it is possible to implement most existing | ||
43 | * WebSocket versions using this API; in fact, this API might be useful | ||
44 | * for any protocol switch, not just WebSockets). Note that | ||
45 | * draft-ietf-hybi-thewebsocketprotocol-00 cannot be implemented this | ||
46 | * way as the header "HTTP/1.1 101 WebSocket Protocol Handshake" | ||
47 | * cannot be generated; instead, MHD will always produce "HTTP/1.1 101 | ||
48 | * Switching Protocols" (if the response code 101 is used). | ||
49 | * | ||
50 | * As usual, the response object can be extended with header | ||
51 | * information and then be used any number of times (as long as the | ||
52 | * header information is not connection-specific). | ||
53 | * | ||
54 | * @param upgrade_handler function to call with the "upgraded" socket | ||
55 | * @param upgrade_handler_cls closure for @a upgrade_handler | ||
56 | * @return NULL on error (i.e. invalid arguments, out of memory) | ||
57 | */ | ||
58 | struct MHD_Response * | ||
59 | MHD_response_for_upgrade (MHD_UpgradeHandler upgrade_handler, | ||
60 | void *upgrade_handler_cls) | ||
61 | { | ||
62 | struct MHD_Response *response; | ||
63 | |||
64 | mhd_assert (NULL != upgrade_handler); | ||
65 | response = MHD_calloc_ (1, | ||
66 | sizeof (struct MHD_Response)); | ||
67 | if (NULL == response) | ||
68 | return NULL; | ||
69 | if (! MHD_mutex_init_ (&response->mutex)) | ||
70 | { | ||
71 | free (response); | ||
72 | return NULL; | ||
73 | } | ||
74 | response->upgrade_handler = upgrade_handler; | ||
75 | response->upgrade_handler_cls = upgrade_handler_cls; | ||
76 | response->status_code = MHD_HTTP_SWITCHING_PROTOCOLS; | ||
77 | response->total_size = MHD_SIZE_UNKNOWN; | ||
78 | response->reference_count = 1; | ||
79 | if (MHD_NO == | ||
80 | MHD_add_response_header (response, | ||
81 | MHD_HTTP_HEADER_CONNECTION, | ||
82 | "Upgrade")) | ||
83 | { | ||
84 | MHD_destroy_response (response); | ||
85 | return NULL; | ||
86 | } | ||
87 | return response; | ||
88 | } | ||
89 | |||
90 | /* end of response_for_upgrade.c */ | ||