Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 1 | /* |
| 2 | * libwebsockets - small server side websockets and web server implementation |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 3 | * |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 4 | * Copyright (C) 2010-2013 Andy Green <andy@warmcat.com> |
Andy Green | a0da8a8 | 2010-11-08 17:12:19 +0000 | [diff] [blame] | 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 | |
Markus Elfring | 7521233 | 2013-10-26 20:23:00 +0800 | [diff] [blame] | 22 | #ifndef LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C |
| 23 | #define LIBWEBSOCKET_H_3060898B846849FF9F88F5DB59B5950C |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 24 | |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 25 | #ifdef __cplusplus |
| 26 | extern "C" { |
Paulo Roberto Urio | 4144e83 | 2012-06-04 09:04:33 +0800 | [diff] [blame] | 27 | #include <cstddef> |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 28 | #endif |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 29 | |
| 30 | #ifdef CMAKE_BUILD |
| 31 | #include "lws_config.h" |
| 32 | #endif |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 33 | |
Andreas Pakulat | 68bd4bd | 2013-10-28 15:18:04 +0100 | [diff] [blame] | 34 | #if defined(WIN32) || defined(_WIN32) |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 35 | |
| 36 | #ifndef WIN32_LEAN_AND_MEAN |
| 37 | #define WIN32_LEAN_AND_MEAN |
| 38 | #endif |
| 39 | #include <winsock2.h> |
| 40 | #include <ws2tcpip.h> |
Paulo Roberto Urio | 4144e83 | 2012-06-04 09:04:33 +0800 | [diff] [blame] | 41 | #include <stddef.h> |
pmcdona | 0a0a7eb | 2013-12-18 10:17:25 +0800 | [diff] [blame] | 42 | #include <basetsd.h> |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 43 | |
Oleg Golosovskiy | 2ef7505 | 2011-10-04 20:20:14 +0800 | [diff] [blame] | 44 | #define strcasecmp stricmp |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 45 | #define getdtablesize() 30000 |
Oleg Golosovskiy | 2ef7505 | 2011-10-04 20:20:14 +0800 | [diff] [blame] | 46 | |
Andy Green | 3661d7b | 2013-04-26 07:37:16 +0800 | [diff] [blame] | 47 | #define LWS_VISIBLE |
| 48 | |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 49 | #ifdef LWS_DLL |
| 50 | #ifdef LWS_INTERNAL |
| 51 | #define LWS_EXTERN extern __declspec(dllexport) |
| 52 | #else |
| 53 | #define LWS_EXTERN extern __declspec(dllimport) |
| 54 | #endif |
Andy Green | 3661d7b | 2013-04-26 07:37:16 +0800 | [diff] [blame] | 55 | #else |
| 56 | #define LWS_EXTERN |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 57 | #endif |
| 58 | |
Joakim Soderberg | f272cb0 | 2013-02-13 09:29:26 +0800 | [diff] [blame] | 59 | #else // NOT WIN32 |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 60 | |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 61 | #include <poll.h> |
Andy Green | dfb2304 | 2013-01-17 12:26:48 +0800 | [diff] [blame] | 62 | #include <unistd.h> |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 63 | |
| 64 | #if defined(__GNUC__) |
| 65 | #define LWS_VISIBLE __attribute__((visibility("default"))) |
| 66 | #else |
| 67 | #define LWS_VISIBLE |
| 68 | #endif |
| 69 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 70 | #endif |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 71 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 72 | #ifdef LWS_USE_LIBEV |
| 73 | #include <ev.h> |
| 74 | #endif /* LWS_USE_LIBEV */ |
| 75 | |
Peter Young | 27b3237 | 2013-01-17 10:10:10 +0800 | [diff] [blame] | 76 | #include <assert.h> |
| 77 | |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 78 | #ifndef LWS_EXTERN |
| 79 | #define LWS_EXTERN extern |
| 80 | #endif |
Andy Green | 158e804 | 2014-04-02 14:25:10 +0800 | [diff] [blame] | 81 | |
| 82 | #ifdef _WIN32 |
| 83 | #define random rand |
| 84 | #else |
| 85 | #include <sys/time.h> |
| 86 | #include <unistd.h> |
| 87 | #endif |
David Galeano | 9454e21 | 2011-09-26 12:17:20 +0100 | [diff] [blame] | 88 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 89 | #define CONTEXT_PORT_NO_LISTEN -1 |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 90 | #define MAX_MUX_RECURSION 2 |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 91 | |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 92 | enum lws_log_levels { |
| 93 | LLL_ERR = 1 << 0, |
| 94 | LLL_WARN = 1 << 1, |
Andy Green | 7c19c34 | 2013-01-19 12:18:07 +0800 | [diff] [blame] | 95 | LLL_NOTICE = 1 << 2, |
| 96 | LLL_INFO = 1 << 3, |
| 97 | LLL_DEBUG = 1 << 4, |
| 98 | LLL_PARSER = 1 << 5, |
| 99 | LLL_HEADER = 1 << 6, |
| 100 | LLL_EXT = 1 << 7, |
| 101 | LLL_CLIENT = 1 << 8, |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 102 | LLL_LATENCY = 1 << 9, |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 103 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 104 | LLL_COUNT = 10 /* set to count of valid flags */ |
Andy Green | 43db045 | 2013-01-10 19:50:35 +0800 | [diff] [blame] | 105 | }; |
| 106 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 107 | LWS_VISIBLE LWS_EXTERN void _lws_log(int filter, const char *format, ...); |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 108 | |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 109 | /* notice, warn and log are always compiled in */ |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 110 | #define lwsl_notice(...) _lws_log(LLL_NOTICE, __VA_ARGS__) |
| 111 | #define lwsl_warn(...) _lws_log(LLL_WARN, __VA_ARGS__) |
| 112 | #define lwsl_err(...) _lws_log(LLL_ERR, __VA_ARGS__) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 113 | /* |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 114 | * weaker logging can be deselected at configure time using --disable-debug |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 115 | * that gets rid of the overhead of checking while keeping _warn and _err |
| 116 | * active |
| 117 | */ |
| 118 | #ifdef _DEBUG |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 119 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 120 | #define lwsl_info(...) _lws_log(LLL_INFO, __VA_ARGS__) |
| 121 | #define lwsl_debug(...) _lws_log(LLL_DEBUG, __VA_ARGS__) |
| 122 | #define lwsl_parser(...) _lws_log(LLL_PARSER, __VA_ARGS__) |
| 123 | #define lwsl_header(...) _lws_log(LLL_HEADER, __VA_ARGS__) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 124 | #define lwsl_ext(...) _lws_log(LLL_EXT, __VA_ARGS__) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 125 | #define lwsl_client(...) _lws_log(LLL_CLIENT, __VA_ARGS__) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 126 | #define lwsl_latency(...) _lws_log(LLL_LATENCY, __VA_ARGS__) |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 127 | LWS_VISIBLE LWS_EXTERN void lwsl_hexdump(void *buf, size_t len); |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 128 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 129 | #else /* no debug */ |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 130 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 131 | #define lwsl_info(...) |
| 132 | #define lwsl_debug(...) |
| 133 | #define lwsl_parser(...) |
| 134 | #define lwsl_header(...) |
| 135 | #define lwsl_ext(...) |
| 136 | #define lwsl_client(...) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 137 | #define lwsl_latency(...) |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 138 | #define lwsl_hexdump(a, b) |
Andy Green | d636e35 | 2013-01-29 12:36:17 +0800 | [diff] [blame] | 139 | |
Andy Green | 69e4364 | 2013-01-19 11:58:07 +0800 | [diff] [blame] | 140 | #endif |
| 141 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 142 | #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0])) |
| 143 | |
Andy Green | 6ea337a | 2014-04-21 08:27:19 +0900 | [diff] [blame] | 144 | /* api change list for user code to test against */ |
| 145 | |
| 146 | #define LWS_FEATURE_SERVE_HTTP_FILE_HAS_OTHER_HEADERS_ARG |
| 147 | |
Michael Haberler | a2d3bf1 | 2014-08-11 14:36:57 +0200 | [diff] [blame^] | 148 | /* the struct libwebsocket_protocols has the id field present */ |
| 149 | #define LWS_FEATURE_PROTOCOLS_HAS_ID_FIELD |
| 150 | |
Andy Green | 6ea337a | 2014-04-21 08:27:19 +0900 | [diff] [blame] | 151 | |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 152 | enum libwebsocket_context_options { |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 153 | LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT = 2, |
Andy Green | 788c4a8 | 2012-10-22 12:29:57 +0100 | [diff] [blame] | 154 | LWS_SERVER_OPTION_SKIP_SERVER_CANONICAL_NAME = 4, |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 155 | LWS_SERVER_OPTION_ALLOW_NON_SSL_ON_SSL_PORT = 8, |
James Devine | 3f13ea2 | 2014-03-24 16:09:25 +0800 | [diff] [blame] | 156 | LWS_SERVER_OPTION_LIBEV = 16, |
| 157 | LWS_SERVER_OPTION_DISABLE_IPV6 = 32, |
Andy Green | f55b2ef | 2014-07-05 10:59:59 +0800 | [diff] [blame] | 158 | LWS_SERVER_OPTION_DISABLE_OS_CA_CERTS = 64, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 159 | }; |
Andy Green | 8014b29 | 2011-01-30 20:57:25 +0000 | [diff] [blame] | 160 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 161 | enum libwebsocket_callback_reasons { |
| 162 | LWS_CALLBACK_ESTABLISHED, |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 163 | LWS_CALLBACK_CLIENT_CONNECTION_ERROR, |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 164 | LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 165 | LWS_CALLBACK_CLIENT_ESTABLISHED, |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 166 | LWS_CALLBACK_CLOSED, |
Andy Green | 0c9563b | 2013-06-10 22:54:40 +0800 | [diff] [blame] | 167 | LWS_CALLBACK_CLOSED_HTTP, |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 168 | LWS_CALLBACK_RECEIVE, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 169 | LWS_CALLBACK_CLIENT_RECEIVE, |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 170 | LWS_CALLBACK_CLIENT_RECEIVE_PONG, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 171 | LWS_CALLBACK_CLIENT_WRITEABLE, |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 172 | LWS_CALLBACK_SERVER_WRITEABLE, |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 173 | LWS_CALLBACK_HTTP, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 174 | LWS_CALLBACK_HTTP_BODY, |
| 175 | LWS_CALLBACK_HTTP_BODY_COMPLETION, |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 176 | LWS_CALLBACK_HTTP_FILE_COMPLETION, |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 177 | LWS_CALLBACK_HTTP_WRITEABLE, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 178 | LWS_CALLBACK_FILTER_NETWORK_CONNECTION, |
Andy Green | 19895bc | 2013-11-09 11:59:56 +0800 | [diff] [blame] | 179 | LWS_CALLBACK_FILTER_HTTP_CONNECTION, |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 180 | LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 181 | LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION, |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 182 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS, |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 183 | LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS, |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 184 | LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION, |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 185 | LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 186 | LWS_CALLBACK_CONFIRM_EXTENSION_OKAY, |
Andy Green | c6517fa | 2011-03-06 13:15:29 +0000 | [diff] [blame] | 187 | LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED, |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 188 | LWS_CALLBACK_PROTOCOL_INIT, |
| 189 | LWS_CALLBACK_PROTOCOL_DESTROY, |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 190 | LWS_CALLBACK_WSI_CREATE, /* always protocol[0] */ |
| 191 | LWS_CALLBACK_WSI_DESTROY, /* always protocol[0] */ |
Andy Green | 3b3fa9e | 2013-12-25 16:34:37 +0800 | [diff] [blame] | 192 | LWS_CALLBACK_GET_THREAD_ID, |
| 193 | |
Andy Green | 3221f92 | 2011-02-12 13:14:11 +0000 | [diff] [blame] | 194 | /* external poll() management support */ |
| 195 | LWS_CALLBACK_ADD_POLL_FD, |
| 196 | LWS_CALLBACK_DEL_POLL_FD, |
Michael Haberler | b309305 | 2014-02-15 20:18:24 +0800 | [diff] [blame] | 197 | LWS_CALLBACK_CHANGE_MODE_POLL_FD, |
Andy Green | 7a13279 | 2013-12-18 09:48:26 +0800 | [diff] [blame] | 198 | LWS_CALLBACK_LOCK_POLL, |
| 199 | LWS_CALLBACK_UNLOCK_POLL, |
Andy Green | 09f0058 | 2014-02-15 16:49:41 +0800 | [diff] [blame] | 200 | |
| 201 | LWS_CALLBACK_USER = 1000, /* user code can use any including / above */ |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 202 | }; |
| 203 | |
Michael Haberler | cb73306 | 2014-02-15 20:15:55 +0800 | [diff] [blame] | 204 | // argument structure for all external poll related calls |
| 205 | // passed in via 'in' |
| 206 | struct libwebsocket_pollargs { |
| 207 | int fd; // applicable file descriptor |
| 208 | int events; // the new event mask |
| 209 | int prev_events; // the previous event mask |
| 210 | }; |
| 211 | |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 212 | #ifdef _WIN32 |
| 213 | struct libwebsocket_pollfd { |
| 214 | SOCKET fd; |
| 215 | SHORT events; |
| 216 | SHORT revents; |
| 217 | }; |
| 218 | #else |
| 219 | #define libwebsocket_pollfd pollfd |
| 220 | #endif |
Michael Haberler | cb73306 | 2014-02-15 20:15:55 +0800 | [diff] [blame] | 221 | |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 222 | enum libwebsocket_extension_callback_reasons { |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 223 | LWS_EXT_CALLBACK_SERVER_CONTEXT_CONSTRUCT, |
| 224 | LWS_EXT_CALLBACK_CLIENT_CONTEXT_CONSTRUCT, |
| 225 | LWS_EXT_CALLBACK_SERVER_CONTEXT_DESTRUCT, |
| 226 | LWS_EXT_CALLBACK_CLIENT_CONTEXT_DESTRUCT, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 227 | LWS_EXT_CALLBACK_CONSTRUCT, |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 228 | LWS_EXT_CALLBACK_CLIENT_CONSTRUCT, |
Andy Green | 68b4504 | 2011-05-25 21:41:57 +0100 | [diff] [blame] | 229 | LWS_EXT_CALLBACK_CHECK_OK_TO_REALLY_CLOSE, |
Andy Green | 0922650 | 2011-05-28 10:19:19 +0100 | [diff] [blame] | 230 | LWS_EXT_CALLBACK_CHECK_OK_TO_PROPOSE_EXTENSION, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 231 | LWS_EXT_CALLBACK_DESTROY, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 232 | LWS_EXT_CALLBACK_DESTROY_ANY_WSI_CLOSING, |
| 233 | LWS_EXT_CALLBACK_ANY_WSI_ESTABLISHED, |
Andy Green | 98a717c | 2011-03-06 13:14:15 +0000 | [diff] [blame] | 234 | LWS_EXT_CALLBACK_PACKET_RX_PREPARSE, |
Andy Green | 3b84c00 | 2011-03-06 13:14:42 +0000 | [diff] [blame] | 235 | LWS_EXT_CALLBACK_PACKET_TX_PRESEND, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 236 | LWS_EXT_CALLBACK_PACKET_TX_DO_SEND, |
| 237 | LWS_EXT_CALLBACK_HANDSHAKE_REPLY_TX, |
Andy Green | c44159f | 2011-03-07 07:08:18 +0000 | [diff] [blame] | 238 | LWS_EXT_CALLBACK_FLUSH_PENDING_TX, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 239 | LWS_EXT_CALLBACK_EXTENDED_PAYLOAD_RX, |
| 240 | LWS_EXT_CALLBACK_CAN_PROXY_CLIENT_CONNECTION, |
| 241 | LWS_EXT_CALLBACK_1HZ, |
| 242 | LWS_EXT_CALLBACK_REQUEST_ON_WRITEABLE, |
| 243 | LWS_EXT_CALLBACK_IS_WRITEABLE, |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 244 | LWS_EXT_CALLBACK_PAYLOAD_TX, |
| 245 | LWS_EXT_CALLBACK_PAYLOAD_RX, |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 246 | }; |
| 247 | |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 248 | enum libwebsocket_write_protocol { |
| 249 | LWS_WRITE_TEXT, |
| 250 | LWS_WRITE_BINARY, |
Andy Green | 5d9d94b | 2011-03-07 20:47:41 +0000 | [diff] [blame] | 251 | LWS_WRITE_CONTINUATION, |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 252 | LWS_WRITE_HTTP, |
| 253 | |
Andy Green | 5e1fa17 | 2011-02-10 09:07:05 +0000 | [diff] [blame] | 254 | /* special 04+ opcodes */ |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 255 | |
| 256 | LWS_WRITE_CLOSE, |
| 257 | LWS_WRITE_PING, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 258 | LWS_WRITE_PONG, |
| 259 | |
Andy Green | bd96d80 | 2011-01-30 08:24:31 +0000 | [diff] [blame] | 260 | /* flags */ |
| 261 | |
| 262 | LWS_WRITE_NO_FIN = 0x40, |
| 263 | /* |
| 264 | * client packet payload goes out on wire unmunged |
| 265 | * only useful for security tests since normal servers cannot |
| 266 | * decode the content if used |
| 267 | */ |
| 268 | LWS_WRITE_CLIENT_IGNORE_XOR_MASK = 0x80 |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 269 | }; |
| 270 | |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 271 | /* |
| 272 | * you need these to look at headers that have been parsed if using the |
| 273 | * LWS_CALLBACK_FILTER_CONNECTION callback. If a header from the enum |
| 274 | * list below is absent, .token = NULL and token_len = 0. Otherwise .token |
| 275 | * points to .token_len chars containing that header content. |
| 276 | */ |
| 277 | |
| 278 | struct lws_tokens { |
| 279 | char *token; |
| 280 | int token_len; |
| 281 | }; |
| 282 | |
| 283 | enum lws_token_indexes { |
| 284 | WSI_TOKEN_GET_URI, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 285 | WSI_TOKEN_POST_URI, |
Andrew Canaday | da55fb5 | 2014-07-05 10:04:19 +0800 | [diff] [blame] | 286 | WSI_TOKEN_OPTIONS_URI, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 287 | WSI_TOKEN_HOST, |
| 288 | WSI_TOKEN_CONNECTION, |
| 289 | WSI_TOKEN_KEY1, |
| 290 | WSI_TOKEN_KEY2, |
| 291 | WSI_TOKEN_PROTOCOL, |
| 292 | WSI_TOKEN_UPGRADE, |
| 293 | WSI_TOKEN_ORIGIN, |
| 294 | WSI_TOKEN_DRAFT, |
| 295 | WSI_TOKEN_CHALLENGE, |
| 296 | |
| 297 | /* new for 04 */ |
| 298 | WSI_TOKEN_KEY, |
| 299 | WSI_TOKEN_VERSION, |
| 300 | WSI_TOKEN_SWORIGIN, |
| 301 | |
| 302 | /* new for 05 */ |
| 303 | WSI_TOKEN_EXTENSIONS, |
| 304 | |
| 305 | /* client receives these */ |
| 306 | WSI_TOKEN_ACCEPT, |
| 307 | WSI_TOKEN_NONCE, |
| 308 | WSI_TOKEN_HTTP, |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 309 | |
| 310 | /* http-related */ |
| 311 | WSI_TOKEN_HTTP_ACCEPT, |
Andrew Canaday | 4220327 | 2014-06-28 20:19:57 -0400 | [diff] [blame] | 312 | WSI_TOKEN_HTTP_AC_REQUEST_HEADERS, |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 313 | WSI_TOKEN_HTTP_IF_MODIFIED_SINCE, |
Andrew Canaday | 4220327 | 2014-06-28 20:19:57 -0400 | [diff] [blame] | 314 | WSI_TOKEN_HTTP_IF_NONE_MATCH, |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 315 | WSI_TOKEN_HTTP_ACCEPT_ENCODING, |
| 316 | WSI_TOKEN_HTTP_ACCEPT_LANGUAGE, |
| 317 | WSI_TOKEN_HTTP_PRAGMA, |
| 318 | WSI_TOKEN_HTTP_CACHE_CONTROL, |
| 319 | WSI_TOKEN_HTTP_AUTHORIZATION, |
| 320 | WSI_TOKEN_HTTP_COOKIE, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 321 | WSI_TOKEN_HTTP_CONTENT_LENGTH, |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 322 | WSI_TOKEN_HTTP_CONTENT_TYPE, |
| 323 | WSI_TOKEN_HTTP_DATE, |
| 324 | WSI_TOKEN_HTTP_RANGE, |
| 325 | WSI_TOKEN_HTTP_REFERER, |
Andy Green | 1e3f7b8 | 2013-11-13 07:45:17 +0800 | [diff] [blame] | 326 | WSI_TOKEN_HTTP_URI_ARGS, |
Andy Green | cc13c6f | 2013-11-09 10:09:09 +0800 | [diff] [blame] | 327 | |
| 328 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 329 | WSI_TOKEN_MUXURL, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 330 | |
Andy Green | e77fb80 | 2013-02-11 13:04:45 +0800 | [diff] [blame] | 331 | /* use token storage to stash these */ |
| 332 | |
| 333 | _WSI_TOKEN_CLIENT_SENT_PROTOCOLS, |
| 334 | _WSI_TOKEN_CLIENT_PEER_ADDRESS, |
| 335 | _WSI_TOKEN_CLIENT_URI, |
| 336 | _WSI_TOKEN_CLIENT_HOST, |
| 337 | _WSI_TOKEN_CLIENT_ORIGIN, |
| 338 | |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 339 | /* always last real token index*/ |
| 340 | WSI_TOKEN_COUNT, |
| 341 | /* parser state additions */ |
| 342 | WSI_TOKEN_NAME_PART, |
| 343 | WSI_TOKEN_SKIPPING, |
| 344 | WSI_TOKEN_SKIPPING_SAW_CR, |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 345 | WSI_PARSING_COMPLETE, |
| 346 | WSI_INIT_TOKEN_MUXURL, |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 347 | }; |
| 348 | |
Andrew Canaday | 74b4a65 | 2014-06-29 00:25:19 -0400 | [diff] [blame] | 349 | struct lws_token_limits { |
| 350 | unsigned short token_limit[WSI_TOKEN_COUNT]; |
| 351 | }; |
| 352 | |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 353 | /* |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 354 | * From RFC 6455 |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 355 | 1000 |
| 356 | |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 357 | 1000 indicates a normal closure, meaning that the purpose for |
| 358 | which the connection was established has been fulfilled. |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 359 | |
| 360 | 1001 |
| 361 | |
| 362 | 1001 indicates that an endpoint is "going away", such as a server |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 363 | going down or a browser having navigated away from a page. |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 364 | |
| 365 | 1002 |
| 366 | |
| 367 | 1002 indicates that an endpoint is terminating the connection due |
| 368 | to a protocol error. |
| 369 | |
| 370 | 1003 |
| 371 | |
| 372 | 1003 indicates that an endpoint is terminating the connection |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 373 | because it has received a type of data it cannot accept (e.g., an |
| 374 | endpoint that understands only text data MAY send this if it |
| 375 | receives a binary message). |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 376 | |
| 377 | 1004 |
| 378 | |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 379 | Reserved. The specific meaning might be defined in the future. |
| 380 | |
| 381 | 1005 |
| 382 | |
| 383 | 1005 is a reserved value and MUST NOT be set as a status code in a |
| 384 | Close control frame by an endpoint. It is designated for use in |
| 385 | applications expecting a status code to indicate that no status |
| 386 | code was actually present. |
| 387 | |
| 388 | 1006 |
| 389 | |
| 390 | 1006 is a reserved value and MUST NOT be set as a status code in a |
| 391 | Close control frame by an endpoint. It is designated for use in |
| 392 | applications expecting a status code to indicate that the |
| 393 | connection was closed abnormally, e.g., without sending or |
| 394 | receiving a Close control frame. |
| 395 | |
| 396 | 1007 |
| 397 | |
| 398 | 1007 indicates that an endpoint is terminating the connection |
| 399 | because it has received data within a message that was not |
| 400 | consistent with the type of the message (e.g., non-UTF-8 [RFC3629] |
| 401 | data within a text message). |
| 402 | |
| 403 | 1008 |
| 404 | |
| 405 | 1008 indicates that an endpoint is terminating the connection |
| 406 | because it has received a message that violates its policy. This |
| 407 | is a generic status code that can be returned when there is no |
| 408 | other more suitable status code (e.g., 1003 or 1009) or if there |
| 409 | is a need to hide specific details about the policy. |
| 410 | |
| 411 | 1009 |
| 412 | |
| 413 | 1009 indicates that an endpoint is terminating the connection |
| 414 | because it has received a message that is too big for it to |
| 415 | process. |
| 416 | |
| 417 | 1010 |
| 418 | |
| 419 | 1010 indicates that an endpoint (client) is terminating the |
| 420 | connection because it has expected the server to negotiate one or |
| 421 | more extension, but the server didn't return them in the response |
| 422 | message of the WebSocket handshake. The list of extensions that |
| 423 | are needed SHOULD appear in the /reason/ part of the Close frame. |
| 424 | Note that this status code is not used by the server, because it |
| 425 | can fail the WebSocket handshake instead. |
| 426 | |
| 427 | 1011 |
| 428 | |
| 429 | 1011 indicates that a server is terminating the connection because |
| 430 | it encountered an unexpected condition that prevented it from |
| 431 | fulfilling the request. |
| 432 | |
| 433 | 1015 |
| 434 | |
| 435 | 1015 is a reserved value and MUST NOT be set as a status code in a |
| 436 | Close control frame by an endpoint. It is designated for use in |
| 437 | applications expecting a status code to indicate that the |
| 438 | connection was closed due to a failure to perform a TLS handshake |
| 439 | (e.g., the server certificate can't be verified). |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 440 | */ |
| 441 | |
| 442 | enum lws_close_status { |
| 443 | LWS_CLOSE_STATUS_NOSTATUS = 0, |
| 444 | LWS_CLOSE_STATUS_NORMAL = 1000, |
| 445 | LWS_CLOSE_STATUS_GOINGAWAY = 1001, |
| 446 | LWS_CLOSE_STATUS_PROTOCOL_ERR = 1002, |
| 447 | LWS_CLOSE_STATUS_UNACCEPTABLE_OPCODE = 1003, |
David Galeano | 2915a80 | 2013-01-09 15:26:08 +0800 | [diff] [blame] | 448 | LWS_CLOSE_STATUS_RESERVED = 1004, |
| 449 | LWS_CLOSE_STATUS_NO_STATUS = 1005, |
| 450 | LWS_CLOSE_STATUS_ABNORMAL_CLOSE = 1006, |
| 451 | LWS_CLOSE_STATUS_INVALID_PAYLOAD = 1007, |
| 452 | LWS_CLOSE_STATUS_POLICY_VIOLATION = 1008, |
| 453 | LWS_CLOSE_STATUS_MESSAGE_TOO_LARGE = 1009, |
| 454 | LWS_CLOSE_STATUS_EXTENSION_REQUIRED = 1010, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 455 | LWS_CLOSE_STATUS_UNEXPECTED_CONDITION = 1011, |
| 456 | LWS_CLOSE_STATUS_TLS_FAILURE = 1015, |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 457 | }; |
| 458 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 459 | enum http_status { |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 460 | HTTP_STATUS_OK = 200, |
Andrew Canaday | b57e7a0 | 2014-06-29 19:14:43 -0400 | [diff] [blame] | 461 | HTTP_STATUS_NO_CONTENT = 204, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 462 | |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 463 | HTTP_STATUS_BAD_REQUEST = 400, |
| 464 | HTTP_STATUS_UNAUTHORIZED, |
| 465 | HTTP_STATUS_PAYMENT_REQUIRED, |
| 466 | HTTP_STATUS_FORBIDDEN, |
| 467 | HTTP_STATUS_NOT_FOUND, |
| 468 | HTTP_STATUS_METHOD_NOT_ALLOWED, |
| 469 | HTTP_STATUS_NOT_ACCEPTABLE, |
| 470 | HTTP_STATUS_PROXY_AUTH_REQUIRED, |
| 471 | HTTP_STATUS_REQUEST_TIMEOUT, |
| 472 | HTTP_STATUS_CONFLICT, |
| 473 | HTTP_STATUS_GONE, |
| 474 | HTTP_STATUS_LENGTH_REQUIRED, |
| 475 | HTTP_STATUS_PRECONDITION_FAILED, |
| 476 | HTTP_STATUS_REQ_ENTITY_TOO_LARGE, |
| 477 | HTTP_STATUS_REQ_URI_TOO_LONG, |
| 478 | HTTP_STATUS_UNSUPPORTED_MEDIA_TYPE, |
| 479 | HTTP_STATUS_REQ_RANGE_NOT_SATISFIABLE, |
| 480 | HTTP_STATUS_EXPECTATION_FAILED, |
| 481 | |
| 482 | HTTP_STATUS_INTERNAL_SERVER_ERROR = 500, |
| 483 | HTTP_STATUS_NOT_IMPLEMENTED, |
| 484 | HTTP_STATUS_BAD_GATEWAY, |
| 485 | HTTP_STATUS_SERVICE_UNAVAILABLE, |
| 486 | HTTP_STATUS_GATEWAY_TIMEOUT, |
| 487 | HTTP_STATUS_HTTP_VERSION_NOT_SUPPORTED, |
| 488 | }; |
| 489 | |
Andy Green | 775c0dd | 2010-10-29 14:15:22 +0100 | [diff] [blame] | 490 | struct libwebsocket; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 491 | struct libwebsocket_context; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 492 | /* needed even with extensions disabled for create context */ |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 493 | struct libwebsocket_extension; |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 494 | |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 495 | /** |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 496 | * callback_function() - User server actions |
Andy Green | 32375b7 | 2011-02-19 08:32:53 +0000 | [diff] [blame] | 497 | * @context: Websockets context |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 498 | * @wsi: Opaque websocket instance pointer |
| 499 | * @reason: The reason for the call |
| 500 | * @user: Pointer to per-session user data allocated by library |
| 501 | * @in: Pointer used for some callback reasons |
| 502 | * @len: Length set for some callback reasons |
| 503 | * |
| 504 | * This callback is the way the user controls what is served. All the |
| 505 | * protocol detail is hidden and handled by the library. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 506 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 507 | * For each connection / session there is user data allocated that is |
| 508 | * pointed to by "user". You set the size of this user data area when |
| 509 | * the library is initialized with libwebsocket_create_server. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 510 | * |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 511 | * You get an opportunity to initialize user data when called back with |
| 512 | * LWS_CALLBACK_ESTABLISHED reason. |
| 513 | * |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 514 | * LWS_CALLBACK_ESTABLISHED: after the server completes a handshake with |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 515 | * an incoming client |
| 516 | * |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 517 | * LWS_CALLBACK_CLIENT_CONNECTION_ERROR: the request client connection has |
| 518 | * been unable to complete a handshake with the remote server |
| 519 | * |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 520 | * LWS_CALLBACK_CLIENT_FILTER_PRE_ESTABLISH: this is the last chance for the |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 521 | * client user code to examine the http headers |
| 522 | * and decide to reject the connection. If the |
| 523 | * content in the headers is interesting to the |
| 524 | * client (url, etc) it needs to copy it out at |
| 525 | * this point since it will be destroyed before |
| 526 | * the CLIENT_ESTABLISHED call |
Andy Green | 2b57a34 | 2013-02-06 15:15:25 +0900 | [diff] [blame] | 527 | * |
David Brooks | 80a4497 | 2012-04-20 12:18:47 +0800 | [diff] [blame] | 528 | * LWS_CALLBACK_CLIENT_ESTABLISHED: after your client connection completed |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 529 | * a handshake with the remote server |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 530 | * |
| 531 | * LWS_CALLBACK_CLOSED: when the websocket session ends |
| 532 | * |
Andy Green | 0c9563b | 2013-06-10 22:54:40 +0800 | [diff] [blame] | 533 | * LWS_CALLBACK_CLOSED_HTTP: when a HTTP (non-websocket) session ends |
| 534 | * |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 535 | * LWS_CALLBACK_RECEIVE: data has appeared for this server endpoint from a |
| 536 | * remote client, it can be found at *in and is |
| 537 | * len bytes long |
| 538 | * |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 539 | * LWS_CALLBACK_CLIENT_RECEIVE_PONG: if you elected to see PONG packets, |
| 540 | * they appear with this callback reason. PONG |
| 541 | * packets only exist in 04+ protocol |
| 542 | * |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 543 | * LWS_CALLBACK_CLIENT_RECEIVE: data has appeared from the server for the |
| 544 | * client connection, it can be found at *in and |
| 545 | * is len bytes long |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 546 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 547 | * LWS_CALLBACK_HTTP: an http request has come from a client that is not |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 548 | * asking to upgrade the connection to a websocket |
| 549 | * one. This is a chance to serve http content, |
| 550 | * for example, to send a script to the client |
| 551 | * which will then open the websockets connection. |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 552 | * @in points to the URI path requested and |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 553 | * libwebsockets_serve_http_file() makes it very |
| 554 | * simple to send back a file to the client. |
Andy Green | 24b588b | 2013-01-13 09:53:18 +0800 | [diff] [blame] | 555 | * Normally after sending the file you are done |
| 556 | * with the http connection, since the rest of the |
| 557 | * activity will come by websockets from the script |
| 558 | * that was delivered by http, so you will want to |
| 559 | * return 1; to close and free up the connection. |
| 560 | * That's important because it uses a slot in the |
| 561 | * total number of client connections allowed set |
| 562 | * by MAX_CLIENTS. |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 563 | * |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 564 | * LWS_CALLBACK_HTTP_BODY: the next @len bytes data from the http |
| 565 | * request body HTTP connection is now available in @in. |
| 566 | * |
| 567 | * LWS_CALLBACK_HTTP_BODY_COMPLETION: the expected amount of http request |
| 568 | * body has been delivered |
| 569 | * |
Andy Green | 54cb346 | 2013-02-14 22:23:54 +0800 | [diff] [blame] | 570 | * LWS_CALLBACK_HTTP_WRITEABLE: you can write more down the http protocol |
| 571 | * link now. |
| 572 | * |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 573 | * LWS_CALLBACK_HTTP_FILE_COMPLETION: a file requested to be send down |
| 574 | * http link has completed. |
| 575 | * |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 576 | * LWS_CALLBACK_CLIENT_WRITEABLE: |
| 577 | * LWS_CALLBACK_SERVER_WRITEABLE: If you call |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 578 | * libwebsocket_callback_on_writable() on a connection, you will |
Andy Green | 9e4c2b6 | 2011-03-07 20:47:39 +0000 | [diff] [blame] | 579 | * get one of these callbacks coming when the connection socket |
| 580 | * is able to accept another write packet without blocking. |
| 581 | * If it already was able to take another packet without blocking, |
| 582 | * you'll get this callback at the next call to the service loop |
| 583 | * function. Notice that CLIENTs get LWS_CALLBACK_CLIENT_WRITEABLE |
| 584 | * and servers get LWS_CALLBACK_SERVER_WRITEABLE. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 585 | * |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 586 | * LWS_CALLBACK_FILTER_NETWORK_CONNECTION: called when a client connects to |
| 587 | * the server at network level; the connection is accepted but then |
| 588 | * passed to this callback to decide whether to hang up immediately |
Edwin van den Oetelaar | 8c8a8e1 | 2013-02-20 20:56:59 +0800 | [diff] [blame] | 589 | * or not, based on the client IP. @in contains the connection |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 590 | * socket's descriptor. Since the client connection information is |
| 591 | * not available yet, @wsi still pointing to the main server socket. |
| 592 | * Return non-zero to terminate the connection before sending or |
| 593 | * receiving anything. Because this happens immediately after the |
| 594 | * network connection from the client, there's no websocket protocol |
| 595 | * selected yet so this callback is issued only to protocol 0. |
Andy Green | 19895bc | 2013-11-09 11:59:56 +0800 | [diff] [blame] | 596 | * |
Alexandre Erwin Ittner | d578f57 | 2014-02-06 23:15:51 -0200 | [diff] [blame] | 597 | * LWS_CALLBACK_SERVER_NEW_CLIENT_INSTANTIATED: A new client just had |
| 598 | * been connected, accepted, and instantiated into the pool. This |
| 599 | * callback allows setting any relevant property to it. Because this |
| 600 | * happens immediately after the instantiation of a new client, |
| 601 | * there's no websocket protocol selected yet so this callback is |
| 602 | * issued only to protocol 0. Only @wsi is defined, pointing to the |
| 603 | * new client, and the return value is ignored. |
| 604 | * |
Andy Green | 19895bc | 2013-11-09 11:59:56 +0800 | [diff] [blame] | 605 | * LWS_CALLBACK_FILTER_HTTP_CONNECTION: called when the request has |
| 606 | * been received and parsed from the client, but the response is |
| 607 | * not sent yet. Return non-zero to disallow the connection. |
| 608 | * @user is a pointer to the connection user space allocation, |
| 609 | * @in is the URI, eg, "/" |
| 610 | * In your handler you can use the public APIs |
| 611 | * lws_hdr_total_length() / lws_hdr_copy() to access all of the |
| 612 | * headers using the header enums lws_token_indexes from |
| 613 | * libwebsockets.h to check for and read the supported header |
| 614 | * presence and content before deciding to allow the http |
| 615 | * connection to proceed or to kill the connection. |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 616 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 617 | * LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION: called when the handshake has |
| 618 | * been received and parsed from the client, but the response is |
| 619 | * not sent yet. Return non-zero to disallow the connection. |
Andy Green | 96d48fd | 2013-09-18 08:32:55 +0800 | [diff] [blame] | 620 | * @user is a pointer to the connection user space allocation, |
| 621 | * @in is the requested protocol name |
| 622 | * In your handler you can use the public APIs |
| 623 | * lws_hdr_total_length() / lws_hdr_copy() to access all of the |
| 624 | * headers using the header enums lws_token_indexes from |
| 625 | * libwebsockets.h to check for and read the supported header |
| 626 | * presence and content before deciding to allow the handshake |
| 627 | * to proceed or to kill the connection. |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 628 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 629 | * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_CLIENT_VERIFY_CERTS: if configured for |
| 630 | * including OpenSSL support, this callback allows your user code |
| 631 | * to perform extra SSL_CTX_load_verify_locations() or similar |
Andy Green | 0894bda | 2011-02-19 09:09:11 +0000 | [diff] [blame] | 632 | * calls to direct OpenSSL where to find certificates the client |
| 633 | * can use to confirm the remote server identity. @user is the |
| 634 | * OpenSSL SSL_CTX* |
Andy Green | c85619d | 2011-02-13 08:25:26 +0000 | [diff] [blame] | 635 | * |
Andy Green | c6bf2c2 | 2011-02-20 11:10:47 +0000 | [diff] [blame] | 636 | * LWS_CALLBACK_OPENSSL_LOAD_EXTRA_SERVER_VERIFY_CERTS: if configured for |
| 637 | * including OpenSSL support, this callback allows your user code |
| 638 | * to load extra certifcates into the server which allow it to |
| 639 | * verify the validity of certificates returned by clients. @user |
| 640 | * is the server's OpenSSL SSL_CTX* |
| 641 | * |
Andy Green | 6901cb3 | 2011-02-21 08:06:47 +0000 | [diff] [blame] | 642 | * LWS_CALLBACK_OPENSSL_PERFORM_CLIENT_CERT_VERIFICATION: if the |
| 643 | * libwebsockets context was created with the option |
| 644 | * LWS_SERVER_OPTION_REQUIRE_VALID_OPENSSL_CLIENT_CERT, then this |
| 645 | * callback is generated during OpenSSL verification of the cert |
| 646 | * sent from the client. It is sent to protocol[0] callback as |
| 647 | * no protocol has been negotiated on the connection yet. |
| 648 | * Notice that the libwebsockets context and wsi are both NULL |
| 649 | * during this callback. See |
| 650 | * http://www.openssl.org/docs/ssl/SSL_CTX_set_verify.html |
| 651 | * to understand more detail about the OpenSSL callback that |
| 652 | * generates this libwebsockets callback and the meanings of the |
| 653 | * arguments passed. In this callback, @user is the x509_ctx, |
| 654 | * @in is the ssl pointer and @len is preverify_ok |
| 655 | * Notice that this callback maintains libwebsocket return |
| 656 | * conventions, return 0 to mean the cert is OK or 1 to fail it. |
| 657 | * This also means that if you don't handle this callback then |
| 658 | * the default callback action of returning 0 allows the client |
| 659 | * certificates. |
| 660 | * |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 661 | * LWS_CALLBACK_CLIENT_APPEND_HANDSHAKE_HEADER: this callback happens |
| 662 | * when a client handshake is being compiled. @user is NULL, |
| 663 | * @in is a char **, it's pointing to a char * which holds the |
| 664 | * next location in the header buffer where you can add |
| 665 | * headers, and @len is the remaining space in the header buffer, |
| 666 | * which is typically some hundreds of bytes. So, to add a canned |
| 667 | * cookie, your handler code might look similar to: |
| 668 | * |
| 669 | * char **p = (char **)in; |
| 670 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 671 | * if (len < 100) |
| 672 | * return 1; |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 673 | * |
| 674 | * *p += sprintf(*p, "Cookie: a=b\x0d\x0a"); |
| 675 | * |
| 676 | * return 0; |
| 677 | * |
| 678 | * Notice if you add anything, you just have to take care about |
| 679 | * the CRLF on the line you added. Obviously this callback is |
| 680 | * optional, if you don't handle it everything is fine. |
| 681 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 682 | * Notice the callback is coming to protocols[0] all the time, |
Andy Green | 385e7ad | 2011-03-01 21:06:02 +0000 | [diff] [blame] | 683 | * because there is no specific protocol handshook yet. |
| 684 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 685 | * LWS_CALLBACK_CONFIRM_EXTENSION_OKAY: When the server handshake code |
Andy Green | c511482 | 2011-03-06 10:29:35 +0000 | [diff] [blame] | 686 | * sees that it does support a requested extension, before |
| 687 | * accepting the extension by additing to the list sent back to |
| 688 | * the client it gives this callback just to check that it's okay |
| 689 | * to use that extension. It calls back to the requested protocol |
| 690 | * and with @in being the extension name, @len is 0 and @user is |
| 691 | * valid. Note though at this time the ESTABLISHED callback hasn't |
| 692 | * happened yet so if you initialize @user content there, @user |
| 693 | * content during this callback might not be useful for anything. |
| 694 | * Notice this callback comes to protocols[0]. |
| 695 | * |
Andy Green | c6517fa | 2011-03-06 13:15:29 +0000 | [diff] [blame] | 696 | * LWS_CALLBACK_CLIENT_CONFIRM_EXTENSION_SUPPORTED: When a client |
| 697 | * connection is being prepared to start a handshake to a server, |
| 698 | * each supported extension is checked with protocols[0] callback |
| 699 | * with this reason, giving the user code a chance to suppress the |
| 700 | * claim to support that extension by returning non-zero. If |
| 701 | * unhandled, by default 0 will be returned and the extension |
| 702 | * support included in the header to the server. Notice this |
| 703 | * callback comes to protocols[0]. |
| 704 | * |
Andy Green | a7109e6 | 2013-02-11 12:05:54 +0800 | [diff] [blame] | 705 | * LWS_CALLBACK_PROTOCOL_INIT: One-time call per protocol so it can |
| 706 | * do initial setup / allocations etc |
| 707 | * |
| 708 | * LWS_CALLBACK_PROTOCOL_DESTROY: One-time call per protocol indicating |
| 709 | * this protocol won't get used at all after this callback, the |
| 710 | * context is getting destroyed. Take the opportunity to |
| 711 | * deallocate everything that was allocated by the protocol. |
| 712 | * |
Andy Green | 76b6ea1 | 2014-02-15 19:25:50 +0800 | [diff] [blame] | 713 | * LWS_CALLBACK_WSI_CREATE: outermost (earliest) wsi create notification |
| 714 | * |
| 715 | * LWS_CALLBACK_WSI_DESTROY: outermost (latest) wsi destroy notification |
| 716 | * |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 717 | * The next five reasons are optional and only need taking care of if you |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 718 | * will be integrating libwebsockets sockets into an external polling |
| 719 | * array. |
| 720 | * |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 721 | * For these calls, @in points to a struct libwebsocket_pollargs that |
| 722 | * contains @fd, @events and @prev_events members |
| 723 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 724 | * LWS_CALLBACK_ADD_POLL_FD: libwebsocket deals with its poll() loop |
| 725 | * internally, but in the case you are integrating with another |
| 726 | * server you will need to have libwebsocket sockets share a |
| 727 | * polling array with the other server. This and the other |
| 728 | * POLL_FD related callbacks let you put your specialized |
| 729 | * poll array interface code in the callback for protocol 0, the |
| 730 | * first protocol you support, usually the HTTP protocol in the |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 731 | * serving case. |
| 732 | * This callback happens when a socket needs to be |
| 733 | * added to the polling loop: @in points to a struct |
| 734 | * libwebsocket_pollargs; the @fd member of the struct is the file |
| 735 | * descriptor, and @events contains the active events. |
| 736 | * |
| 737 | * If you are using the internal polling loop (the "service" |
| 738 | * callback), you can just ignore these callbacks. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 739 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 740 | * LWS_CALLBACK_DEL_POLL_FD: This callback happens when a socket descriptor |
Andy Green | 50097dd | 2013-02-15 22:36:30 +0800 | [diff] [blame] | 741 | * needs to be removed from an external polling array. @in is |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 742 | * again the struct libwebsocket_pollargs containing the @fd member |
| 743 | * to be removed. If you are using the internal polling |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 744 | * loop, you can just ignore it. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 745 | * |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 746 | * LWS_CALLBACK_CHANGE_MODE_POLL_FD: This callback happens when |
| 747 | * libwebsockets wants to modify the events for a connectiion. |
| 748 | * @in is the struct libwebsocket_pollargs with the @fd to change. |
| 749 | * The new event mask is in @events member and the old mask is in |
| 750 | * the @prev_events member. |
| 751 | * If you are using the internal polling loop, you can just ignore |
| 752 | * it. |
Andy Green | e7981dc | 2011-02-12 21:24:03 +0000 | [diff] [blame] | 753 | * |
Michael Haberler | 8284cf1 | 2014-02-15 20:33:46 +0800 | [diff] [blame] | 754 | * LWS_CALLBACK_LOCK_POLL: |
| 755 | * LWS_CALLBACK_UNLOCK_POLL: These allow the external poll changes driven |
| 756 | * by libwebsockets to participate in an external thread locking |
| 757 | * scheme around the changes, so the whole thing is threadsafe. |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 758 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 759 | LWS_VISIBLE LWS_EXTERN int callback(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 760 | struct libwebsocket *wsi, |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 761 | enum libwebsocket_callback_reasons reason, void *user, |
Andy Green | 8f037e4 | 2010-12-19 22:13:26 +0000 | [diff] [blame] | 762 | void *in, size_t len); |
| 763 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 764 | typedef int (callback_function)(struct libwebsocket_context *context, |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 765 | struct libwebsocket *wsi, |
| 766 | enum libwebsocket_callback_reasons reason, void *user, |
| 767 | void *in, size_t len); |
| 768 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 769 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 770 | /** |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 771 | * extension_callback_function() - Hooks to allow extensions to operate |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 772 | * @context: Websockets context |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 773 | * @ext: This extension |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 774 | * @wsi: Opaque websocket instance pointer |
| 775 | * @reason: The reason for the call |
| 776 | * @user: Pointer to per-session user data allocated by library |
| 777 | * @in: Pointer used for some callback reasons |
| 778 | * @len: Length set for some callback reasons |
| 779 | * |
| 780 | * Each extension that is active on a particular connection receives |
| 781 | * callbacks during the connection lifetime to allow the extension to |
| 782 | * operate on websocket data and manage itself. |
| 783 | * |
| 784 | * Libwebsockets takes care of allocating and freeing "user" memory for |
| 785 | * each active extension on each connection. That is what is pointed to |
| 786 | * by the @user parameter. |
| 787 | * |
| 788 | * LWS_EXT_CALLBACK_CONSTRUCT: called when the server has decided to |
| 789 | * select this extension from the list provided by the client, |
| 790 | * just before the server will send back the handshake accepting |
| 791 | * the connection with this extension active. This gives the |
| 792 | * extension a chance to initialize its connection context found |
| 793 | * in @user. |
| 794 | * |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 795 | * LWS_EXT_CALLBACK_CLIENT_CONSTRUCT: same as LWS_EXT_CALLBACK_CONSTRUCT |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 796 | * but called when client is instantiating this extension. Some |
| 797 | * extensions will work the same on client and server side and then |
| 798 | * you can just merge handlers for both CONSTRUCTS. |
| 799 | * |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 800 | * LWS_EXT_CALLBACK_DESTROY: called when the connection the extension was |
| 801 | * being used on is about to be closed and deallocated. It's the |
| 802 | * last chance for the extension to deallocate anything it has |
| 803 | * allocated in the user data (pointed to by @user) before the |
Andy Green | 2366b1c | 2011-03-06 13:15:31 +0000 | [diff] [blame] | 804 | * user data is deleted. This same callback is used whether you |
| 805 | * are in client or server instantiation context. |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 806 | * |
| 807 | * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE: when this extension was active on |
| 808 | * a connection, and a packet of data arrived at the connection, |
| 809 | * it is passed to this callback to give the extension a chance to |
| 810 | * change the data, eg, decompress it. @user is pointing to the |
| 811 | * extension's private connection context data, @in is pointing |
| 812 | * to an lws_tokens struct, it consists of a char * pointer called |
| 813 | * token, and an int called token_len. At entry, these are |
| 814 | * set to point to the received buffer and set to the content |
| 815 | * length. If the extension will grow the content, it should use |
| 816 | * a new buffer allocated in its private user context data and |
| 817 | * set the pointed-to lws_tokens members to point to its buffer. |
| 818 | * |
| 819 | * LWS_EXT_CALLBACK_PACKET_TX_PRESEND: this works the same way as |
| 820 | * LWS_EXT_CALLBACK_PACKET_RX_PREPARSE above, except it gives the |
| 821 | * extension a chance to change websocket data just before it will |
| 822 | * be sent out. Using the same lws_token pointer scheme in @in, |
| 823 | * the extension can change the buffer and the length to be |
| 824 | * transmitted how it likes. Again if it wants to grow the |
| 825 | * buffer safely, it should copy the data into its own buffer and |
| 826 | * set the lws_tokens token pointer to it. |
| 827 | */ |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 828 | LWS_VISIBLE LWS_EXTERN int extension_callback(struct libwebsocket_context *context, |
Andy Green | 46c2ea0 | 2011-03-22 09:04:01 +0000 | [diff] [blame] | 829 | struct libwebsocket_extension *ext, |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 830 | struct libwebsocket *wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 831 | enum libwebsocket_extension_callback_reasons reason, |
| 832 | void *user, void *in, size_t len); |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 833 | |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 834 | typedef int (extension_callback_function)(struct libwebsocket_context *context, |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 835 | struct libwebsocket_extension *ext, |
| 836 | struct libwebsocket *wsi, |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 837 | enum libwebsocket_extension_callback_reasons reason, |
| 838 | void *user, void *in, size_t len); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 839 | #endif |
Andy Green | 57b4e9a | 2011-03-06 13:14:46 +0000 | [diff] [blame] | 840 | |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 841 | /** |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 842 | * struct libwebsocket_protocols - List of protocols and handlers server |
| 843 | * supports. |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 844 | * @name: Protocol name that must match the one given in the client |
Andy Green | a40760a | 2014-08-07 16:52:15 +0800 | [diff] [blame] | 845 | * Javascript new WebSocket(url, 'protocol') name. |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 846 | * @callback: The service callback used for this protocol. It allows the |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 847 | * service action for an entire protocol to be encapsulated in |
| 848 | * the protocol-specific callback |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 849 | * @per_session_data_size: Each new connection using this protocol gets |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 850 | * this much memory allocated on connection establishment and |
| 851 | * freed on connection takedown. A pointer to this per-connection |
| 852 | * allocation is passed into the callback in the 'user' parameter |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 853 | * @rx_buffer_size: if you want atomic frames delivered to the callback, you |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 854 | * should set this to the size of the biggest legal frame that |
| 855 | * you support. If the frame size is exceeded, there is no |
| 856 | * error, but the buffer will spill to the user callback when |
| 857 | * full, which you can detect by using |
| 858 | * libwebsockets_remaining_packet_payload(). Notice that you |
| 859 | * just talk about frame size here, the LWS_SEND_BUFFER_PRE_PADDING |
| 860 | * and post-padding are automatically also allocated on top. |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 861 | * @no_buffer_all_partial_tx: Leave at zero if you want the library to take |
| 862 | * care of all partial tx for you. It's useful if you only have |
| 863 | * small tx packets and the chance of any truncated send is small |
| 864 | * enough any additional malloc / buffering overhead is less |
| 865 | * painful than writing the code to deal with partial sends. For |
| 866 | * protocols where you stream big blocks, set to nonzero and use |
| 867 | * the return value from libwebsocket_write() to manage how much |
| 868 | * got send yourself. |
Andy Green | eabed8d | 2014-08-11 12:11:36 +0800 | [diff] [blame] | 869 | * @id: ignored by lws, but useful to contain user information bound |
| 870 | * to the selected protocol. For example if this protocol was |
| 871 | * called "myprotocol-v2", you might set id to 2, and the user |
| 872 | * code that acts differently according to the version can do so by |
| 873 | * switch (wsi->protocol->id), user code might use some bits as |
| 874 | * capability flags based on selected protocol version, etc. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 875 | * @owning_server: the server init call fills in this opaque pointer when |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 876 | * registering this protocol with the server. |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 877 | * @protocol_index: which protocol we are starting from zero |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 878 | * |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 879 | * This structure represents one protocol supported by the server. An |
| 880 | * array of these structures is passed to libwebsocket_create_server() |
| 881 | * allows as many protocols as you like to be handled by one server. |
Andy Green | a40760a | 2014-08-07 16:52:15 +0800 | [diff] [blame] | 882 | * |
| 883 | * The first protocol given has its callback used for user callbacks when |
| 884 | * there is no agreed protocol name, that's true during HTTP part of the |
| 885 | * connection and true if the client did not send a Protocol: header. |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 886 | */ |
| 887 | |
| 888 | struct libwebsocket_protocols { |
| 889 | const char *name; |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 890 | callback_function *callback; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 891 | size_t per_session_data_size; |
Andy Green | 5449511 | 2013-02-06 21:10:16 +0900 | [diff] [blame] | 892 | size_t rx_buffer_size; |
Andy Green | 1f4267b | 2013-10-17 08:09:19 +0800 | [diff] [blame] | 893 | int no_buffer_all_partial_tx; |
Andy Green | eabed8d | 2014-08-11 12:11:36 +0800 | [diff] [blame] | 894 | unsigned int id; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 895 | |
| 896 | /* |
| 897 | * below are filled in on server init and can be left uninitialized, |
| 898 | * no need for user to use them directly either |
| 899 | */ |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 900 | |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 901 | struct libwebsocket_context *owning_server; |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 902 | int protocol_index; |
Andy Green | 4f3943a | 2010-11-12 10:44:16 +0000 | [diff] [blame] | 903 | }; |
| 904 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 905 | #ifndef LWS_NO_EXTENSIONS |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 906 | /** |
| 907 | * struct libwebsocket_extension - An extension we know how to cope with |
| 908 | * |
| 909 | * @name: Formal extension name, eg, "deflate-stream" |
| 910 | * @callback: Service callback |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 911 | * @per_session_data_size: Libwebsockets will auto-malloc this much |
| 912 | * memory for the use of the extension, a pointer |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 913 | * to it comes in the @user callback parameter |
Peter Pentchev | c74964e | 2013-02-07 16:17:13 +0200 | [diff] [blame] | 914 | * @per_context_private_data: Optional storage for this extension that |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 915 | * is per-context, so it can track stuff across |
| 916 | * all sessions, etc, if it wants |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 917 | */ |
| 918 | |
| 919 | struct libwebsocket_extension { |
| 920 | const char *name; |
David Brooks | 6c6a3d3 | 2012-04-20 12:13:50 +0800 | [diff] [blame] | 921 | extension_callback_function *callback; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 922 | size_t per_session_data_size; |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 923 | void *per_context_private_data; |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 924 | }; |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 925 | #endif |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 926 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 927 | /** |
| 928 | * struct lws_context_creation_info: parameters to create context with |
| 929 | * |
Andy Green | 67f9459 | 2014-07-31 09:44:00 +0800 | [diff] [blame] | 930 | * @port: Port to listen on... you can use CONTEXT_PORT_NO_LISTEN to |
| 931 | * suppress listening on any port, that's what you want if you are |
| 932 | * not running a websocket server at all but just using it as a |
| 933 | * client |
Andy Green | 7a506ba | 2013-02-12 10:13:02 +0800 | [diff] [blame] | 934 | * @iface: NULL to bind the listen socket to all interfaces, or the |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 935 | * interface name, eg, "eth2" |
| 936 | * @protocols: Array of structures listing supported protocols and a protocol- |
| 937 | * specific callback for each one. The list is ended with an |
| 938 | * entry that has a NULL callback pointer. |
| 939 | * It's not const because we write the owning_server member |
| 940 | * @extensions: NULL or array of libwebsocket_extension structs listing the |
| 941 | * extensions this context supports. If you configured with |
| 942 | * --without-extensions, you should give NULL here. |
Andrew Canaday | 3d2ebf3 | 2014-06-29 00:30:45 -0400 | [diff] [blame] | 943 | * @token_limits: NULL or struct lws_token_limits pointer which is initialized |
| 944 | * with a token length limit for each possible WSI_TOKEN_*** |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 945 | * @ssl_cert_filepath: If libwebsockets was compiled to use ssl, and you want |
| 946 | * to listen using SSL, set to the filepath to fetch the |
| 947 | * server cert from, otherwise NULL for unencrypted |
| 948 | * @ssl_private_key_filepath: filepath to private key if wanting SSL mode, |
| 949 | * else ignored |
| 950 | * @ssl_ca_filepath: CA certificate filepath or NULL |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 951 | * @ssl_cipher_list: List of valid ciphers to use (eg, |
| 952 | * "RC4-MD5:RC4-SHA:AES128-SHA:AES256-SHA:HIGH:!DSS:!aNULL" |
| 953 | * or you can leave it as NULL to get "DEFAULT" |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 954 | * @gid: group id to change to after setting listen socket, or -1. |
| 955 | * @uid: user id to change to after setting listen socket, or -1. |
| 956 | * @options: 0, or LWS_SERVER_OPTION_DEFEAT_CLIENT_MASK |
| 957 | * @user: optional user pointer that can be recovered via the context |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 958 | * pointer using libwebsocket_context_user |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 959 | * @ka_time: 0 for no keepalive, otherwise apply this keepalive timeout to |
| 960 | * all libwebsocket sockets, client or server |
| 961 | * @ka_probes: if ka_time was nonzero, after the timeout expires how many |
| 962 | * times to try to get a response from the peer before giving up |
| 963 | * and killing the connection |
| 964 | * @ka_interval: if ka_time was nonzero, how long to wait before each ka_probes |
| 965 | * attempt |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 966 | */ |
| 967 | |
| 968 | struct lws_context_creation_info { |
| 969 | int port; |
Joakim Soderberg | 63ff120 | 2013-02-11 17:52:23 +0100 | [diff] [blame] | 970 | const char *iface; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 971 | struct libwebsocket_protocols *protocols; |
| 972 | struct libwebsocket_extension *extensions; |
Oleh | faeac3c | 2014-07-29 23:18:41 +0800 | [diff] [blame] | 973 | struct lws_token_limits *token_limits; |
| 974 | const char *ssl_private_key_password; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 975 | const char *ssl_cert_filepath; |
| 976 | const char *ssl_private_key_filepath; |
| 977 | const char *ssl_ca_filepath; |
Andy Green | 2672fb2 | 2013-02-22 09:54:35 +0800 | [diff] [blame] | 978 | const char *ssl_cipher_list; |
Patrick Gansterer | 148b945 | 2014-02-28 02:31:23 +0100 | [diff] [blame] | 979 | const char *http_proxy_address; |
| 980 | unsigned int http_proxy_port; |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 981 | int gid; |
| 982 | int uid; |
| 983 | unsigned int options; |
| 984 | void *user; |
Andy Green | a690cd0 | 2013-02-09 12:25:31 +0800 | [diff] [blame] | 985 | int ka_time; |
| 986 | int ka_probes; |
| 987 | int ka_interval; |
| 988 | |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 989 | }; |
| 990 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 991 | LWS_VISIBLE LWS_EXTERN |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 992 | void lws_set_log_level(int level, |
| 993 | void (*log_emit_function)(int level, const char *line)); |
Andy Green | d6e0911 | 2011-03-05 16:12:15 +0000 | [diff] [blame] | 994 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 995 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | c11db20 | 2013-01-19 11:12:16 +0800 | [diff] [blame] | 996 | lwsl_emit_syslog(int level, const char *line); |
| 997 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 998 | LWS_VISIBLE LWS_EXTERN struct libwebsocket_context * |
Andy Green | 1b26527 | 2013-02-09 14:01:09 +0800 | [diff] [blame] | 999 | libwebsocket_create_context(struct lws_context_creation_info *info); |
shys | b4e800e | 2013-10-24 22:12:03 +0800 | [diff] [blame] | 1000 | |
| 1001 | LWS_VISIBLE LWS_EXTERN int |
| 1002 | libwebsocket_set_proxy(struct libwebsocket_context *context, const char *proxy); |
Andy Green | ff95d7a | 2010-10-28 22:36:01 +0100 | [diff] [blame] | 1003 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1004 | LWS_VISIBLE LWS_EXTERN void |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1005 | libwebsocket_context_destroy(struct libwebsocket_context *context); |
Andy Green | 6964bb5 | 2011-01-23 16:50:33 +0000 | [diff] [blame] | 1006 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1007 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1008 | libwebsocket_service(struct libwebsocket_context *context, int timeout_ms); |
Andy Green | e92cd17 | 2011-01-19 13:11:55 +0000 | [diff] [blame] | 1009 | |
Patrick Gansterer | 1ee57f6 | 2014-03-06 11:57:50 +0100 | [diff] [blame] | 1010 | LWS_VISIBLE LWS_EXTERN void |
| 1011 | libwebsocket_cancel_service(struct libwebsocket_context *context); |
| 1012 | |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 1013 | #ifdef LWS_USE_LIBEV |
| 1014 | LWS_VISIBLE LWS_EXTERN int |
| 1015 | libwebsocket_initloop( |
| 1016 | struct libwebsocket_context *context, struct ev_loop *loop); |
| 1017 | |
| 1018 | LWS_VISIBLE void |
Andrew Canaday | 9769f4f | 2014-03-23 13:25:07 +0800 | [diff] [blame] | 1019 | libwebsocket_sigint_cb( |
| 1020 | struct ev_loop *loop, struct ev_signal *watcher, int revents); |
| 1021 | #endif /* LWS_USE_LIBEV */ |
| 1022 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1023 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1024 | libwebsocket_service_fd(struct libwebsocket_context *context, |
Patrick Gansterer | 73882e4 | 2014-03-29 08:25:58 +0100 | [diff] [blame] | 1025 | struct libwebsocket_pollfd *pollfd); |
Andy Green | 9f99034 | 2011-02-12 11:57:45 +0000 | [diff] [blame] | 1026 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1027 | LWS_VISIBLE LWS_EXTERN void * |
Alon Levy | 0291eb3 | 2012-10-19 11:21:56 +0200 | [diff] [blame] | 1028 | libwebsocket_context_user(struct libwebsocket_context *context); |
| 1029 | |
Andy Green | 9beedc1 | 2013-09-18 08:47:15 +0800 | [diff] [blame] | 1030 | enum pending_timeout { |
| 1031 | NO_PENDING_TIMEOUT = 0, |
| 1032 | PENDING_TIMEOUT_AWAITING_PROXY_RESPONSE, |
Andy Green | 5dc62ea | 2013-09-20 20:26:12 +0800 | [diff] [blame] | 1033 | PENDING_TIMEOUT_AWAITING_CONNECT_RESPONSE, |
Andy Green | 9beedc1 | 2013-09-18 08:47:15 +0800 | [diff] [blame] | 1034 | PENDING_TIMEOUT_ESTABLISH_WITH_SERVER, |
| 1035 | PENDING_TIMEOUT_AWAITING_SERVER_RESPONSE, |
| 1036 | PENDING_TIMEOUT_AWAITING_PING, |
| 1037 | PENDING_TIMEOUT_CLOSE_ACK, |
| 1038 | PENDING_TIMEOUT_AWAITING_EXTENSION_CONNECT_RESPONSE, |
| 1039 | PENDING_TIMEOUT_SENT_CLIENT_HANDSHAKE, |
| 1040 | PENDING_TIMEOUT_SSL_ACCEPT, |
kapejod | ce64fb0 | 2013-11-19 13:38:16 +0100 | [diff] [blame] | 1041 | PENDING_TIMEOUT_HTTP_CONTENT, |
Andy Green | 0213812 | 2014-04-06 06:26:35 +0100 | [diff] [blame] | 1042 | PENDING_TIMEOUT_AWAITING_CLIENT_HS_SEND, |
Andy Green | 9beedc1 | 2013-09-18 08:47:15 +0800 | [diff] [blame] | 1043 | }; |
| 1044 | |
Patrick Gansterer | 2ff6955 | 2014-02-26 18:51:05 +0100 | [diff] [blame] | 1045 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | 9beedc1 | 2013-09-18 08:47:15 +0800 | [diff] [blame] | 1046 | libwebsocket_set_timeout(struct libwebsocket *wsi, |
| 1047 | enum pending_timeout reason, int secs); |
| 1048 | |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1049 | /* |
| 1050 | * IMPORTANT NOTICE! |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1051 | * |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 1052 | * When sending with websocket protocol (LWS_WRITE_TEXT or LWS_WRITE_BINARY) |
| 1053 | * the send buffer has to have LWS_SEND_BUFFER_PRE_PADDING bytes valid BEFORE |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1054 | * buf, and LWS_SEND_BUFFER_POST_PADDING bytes valid AFTER (buf + len). |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1055 | * |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1056 | * This allows us to add protocol info before and after the data, and send as |
| 1057 | * one packet on the network without payload copying, for maximum efficiency. |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1058 | * |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1059 | * So for example you need this kind of code to use libwebsocket_write with a |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1060 | * 128-byte payload |
| 1061 | * |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1062 | * char buf[LWS_SEND_BUFFER_PRE_PADDING + 128 + LWS_SEND_BUFFER_POST_PADDING]; |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1063 | * |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1064 | * // fill your part of the buffer... for example here it's all zeros |
| 1065 | * memset(&buf[LWS_SEND_BUFFER_PRE_PADDING], 0, 128); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1066 | * |
Alex Rhatushnyak | 9f2246e | 2013-03-09 12:01:47 +0800 | [diff] [blame] | 1067 | * libwebsocket_write(wsi, &buf[LWS_SEND_BUFFER_PRE_PADDING], 128, |
| 1068 | * LWS_WRITE_TEXT); |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1069 | * |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 1070 | * When sending LWS_WRITE_HTTP, there is no protocol addition and you can just |
| 1071 | * use the whole buffer without taking care of the above. |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1072 | */ |
| 1073 | |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1074 | /* |
| 1075 | * this is the frame nonce plus two header plus 8 length |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1076 | * there's an additional two for mux extension per mux nesting level |
Andy Green | 687b018 | 2011-02-26 11:04:01 +0000 | [diff] [blame] | 1077 | * 2 byte prepend on close will already fit because control frames cannot use |
| 1078 | * the big length style |
| 1079 | */ |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1080 | |
Andy Green | a41314f | 2011-05-23 10:00:03 +0100 | [diff] [blame] | 1081 | #define LWS_SEND_BUFFER_PRE_PADDING (4 + 10 + (2 * MAX_MUX_RECURSION)) |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1082 | #define LWS_SEND_BUFFER_POST_PADDING 4 |
Andy Green | 4ea6006 | 2010-10-30 12:15:07 +0100 | [diff] [blame] | 1083 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1084 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | e77ddd8 | 2010-11-13 10:03:47 +0000 | [diff] [blame] | 1085 | libwebsocket_write(struct libwebsocket *wsi, unsigned char *buf, size_t len, |
Andy Green | 5fd8a5e | 2010-10-31 11:57:17 +0000 | [diff] [blame] | 1086 | enum libwebsocket_write_protocol protocol); |
Andy Green | a2b0ab0 | 2010-11-11 12:28:29 +0000 | [diff] [blame] | 1087 | |
Gabriel Gritsch | 890f8e9 | 2014-02-15 16:20:25 +0800 | [diff] [blame] | 1088 | /* helper for case where buffer may be const */ |
Patrick Gansterer | cb6ee15 | 2014-03-03 12:21:20 +0100 | [diff] [blame] | 1089 | #define libwebsocket_write_http(wsi, buf, len) \ |
| 1090 | libwebsocket_write(wsi, (unsigned char *)(buf), len, LWS_WRITE_HTTP) |
Gabriel Gritsch | 890f8e9 | 2014-02-15 16:20:25 +0800 | [diff] [blame] | 1091 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1092 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1093 | libwebsockets_serve_http_file(struct libwebsocket_context *context, |
| 1094 | struct libwebsocket *wsi, const char *file, |
Andy Green | 7900256 | 2013-11-09 11:04:35 +0800 | [diff] [blame] | 1095 | const char *content_type, const char *other_headers); |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1096 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | d280b6e | 2013-01-15 13:40:23 +0800 | [diff] [blame] | 1097 | libwebsockets_serve_http_file_fragment(struct libwebsocket_context *context, |
| 1098 | struct libwebsocket *wsi); |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1099 | |
Joakim Soderberg | 7b7b7b2 | 2014-02-24 18:23:00 +0100 | [diff] [blame] | 1100 | LWS_VISIBLE LWS_EXTERN int libwebsockets_return_http_status( |
Andy Green | 4e7a133 | 2013-11-11 07:30:33 +0800 | [diff] [blame] | 1101 | struct libwebsocket_context *context, |
| 1102 | struct libwebsocket *wsi, unsigned int code, |
| 1103 | const char *html_body); |
| 1104 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1105 | LWS_VISIBLE LWS_EXTERN const struct libwebsocket_protocols * |
Andy Green | b45993c | 2010-12-18 15:13:50 +0000 | [diff] [blame] | 1106 | libwebsockets_get_protocol(struct libwebsocket *wsi); |
| 1107 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1108 | LWS_VISIBLE LWS_EXTERN int |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1109 | libwebsocket_callback_on_writable(struct libwebsocket_context *context, |
Andy Green | 62c54d2 | 2011-02-14 09:14:25 +0000 | [diff] [blame] | 1110 | struct libwebsocket *wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1111 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1112 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1113 | libwebsocket_callback_on_writable_all_protocol( |
| 1114 | const struct libwebsocket_protocols *protocol); |
| 1115 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1116 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | e39e6ef | 2014-02-15 16:36:38 +0800 | [diff] [blame] | 1117 | libwebsocket_callback_all_protocol( |
| 1118 | const struct libwebsocket_protocols *protocol, int reason); |
| 1119 | |
| 1120 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | a6cbece | 2011-01-27 20:06:03 +0000 | [diff] [blame] | 1121 | libwebsocket_get_socket_fd(struct libwebsocket *wsi); |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1122 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1123 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 82c3d54 | 2011-03-07 21:16:31 +0000 | [diff] [blame] | 1124 | libwebsocket_is_final_fragment(struct libwebsocket *wsi); |
| 1125 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1126 | LWS_VISIBLE LWS_EXTERN unsigned char |
David Galeano | e2cf992 | 2013-01-09 18:06:55 +0800 | [diff] [blame] | 1127 | libwebsocket_get_reserved_bits(struct libwebsocket *wsi); |
| 1128 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1129 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1130 | libwebsocket_rx_flow_control(struct libwebsocket *wsi, int enable); |
| 1131 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1132 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | b55451c | 2013-03-16 12:32:27 +0800 | [diff] [blame] | 1133 | libwebsocket_rx_flow_allow_all_protocol( |
| 1134 | const struct libwebsocket_protocols *protocol); |
| 1135 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1136 | LWS_VISIBLE LWS_EXTERN size_t |
Andy Green | 38e57bb | 2011-01-19 12:20:27 +0000 | [diff] [blame] | 1137 | libwebsockets_remaining_packet_payload(struct libwebsocket *wsi); |
| 1138 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1139 | LWS_VISIBLE LWS_EXTERN struct libwebsocket * |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1140 | libwebsocket_client_connect(struct libwebsocket_context *clients, |
| 1141 | const char *address, |
| 1142 | int port, |
Andy Green | 90c7cbc | 2011-01-27 06:26:52 +0000 | [diff] [blame] | 1143 | int ssl_connection, |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1144 | const char *path, |
| 1145 | const char *host, |
| 1146 | const char *origin, |
Andy Green | bfb051f | 2011-02-09 08:49:14 +0000 | [diff] [blame] | 1147 | const char *protocol, |
| 1148 | int ietf_version_or_minus_one); |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1149 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1150 | LWS_VISIBLE LWS_EXTERN struct libwebsocket * |
David Brooks | 2c60d95 | 2012-04-20 12:19:01 +0800 | [diff] [blame] | 1151 | libwebsocket_client_connect_extended(struct libwebsocket_context *clients, |
| 1152 | const char *address, |
| 1153 | int port, |
| 1154 | int ssl_connection, |
| 1155 | const char *path, |
| 1156 | const char *host, |
| 1157 | const char *origin, |
| 1158 | const char *protocol, |
| 1159 | int ietf_version_or_minus_one, |
| 1160 | void *userdata); |
| 1161 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1162 | LWS_VISIBLE LWS_EXTERN const char * |
Darin Willits | c19456f | 2011-02-14 17:52:39 +0000 | [diff] [blame] | 1163 | libwebsocket_canonical_hostname(struct libwebsocket_context *context); |
Andy Green | 2ac5a6f | 2011-01-28 10:00:18 +0000 | [diff] [blame] | 1164 | |
Andy Green | 4739e5c | 2011-01-22 12:51:57 +0000 | [diff] [blame] | 1165 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1166 | LWS_VISIBLE LWS_EXTERN void |
Andy Green | aaf0b9f | 2013-01-30 08:12:20 +0800 | [diff] [blame] | 1167 | libwebsockets_get_peer_addresses(struct libwebsocket_context *context, |
| 1168 | struct libwebsocket *wsi, int fd, char *name, int name_len, |
Andy Green | 0703409 | 2011-02-13 08:37:12 +0000 | [diff] [blame] | 1169 | char *rip, int rip_len); |
| 1170 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1171 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 8acc194 | 2011-03-07 20:47:40 +0000 | [diff] [blame] | 1172 | libwebsockets_get_random(struct libwebsocket_context *context, |
| 1173 | void *buf, int len); |
| 1174 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1175 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 279a303 | 2013-01-17 10:05:39 +0800 | [diff] [blame] | 1176 | lws_daemonize(const char *_lock_path); |
| 1177 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1178 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 043a0ba | 2011-04-25 23:54:11 +0800 | [diff] [blame] | 1179 | lws_send_pipe_choked(struct libwebsocket *wsi); |
| 1180 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1181 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 2fd3f2f | 2013-01-18 09:49:20 +0800 | [diff] [blame] | 1182 | lws_frame_is_binary(struct libwebsocket *wsi); |
| 1183 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1184 | LWS_VISIBLE LWS_EXTERN unsigned char * |
Andy Green | 2836c64 | 2011-03-07 20:47:41 +0000 | [diff] [blame] | 1185 | libwebsockets_SHA1(const unsigned char *d, size_t n, unsigned char *md); |
| 1186 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1187 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | f94e058 | 2011-05-14 15:07:56 +0200 | [diff] [blame] | 1188 | lws_b64_encode_string(const char *in, int in_len, char *out, int out_size); |
| 1189 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1190 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | f94e058 | 2011-05-14 15:07:56 +0200 | [diff] [blame] | 1191 | lws_b64_decode_string(const char *in, char *out, int out_size); |
| 1192 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1193 | LWS_VISIBLE LWS_EXTERN const char * |
Andy Green | 7b40545 | 2013-02-01 10:50:15 +0800 | [diff] [blame] | 1194 | lws_get_library_version(void); |
| 1195 | |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 1196 | /* access to headers... only valid while headers valid */ |
| 1197 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1198 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 1199 | lws_hdr_total_length(struct libwebsocket *wsi, enum lws_token_indexes h); |
| 1200 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1201 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | b5b2319 | 2013-02-11 17:13:32 +0800 | [diff] [blame] | 1202 | lws_hdr_copy(struct libwebsocket *wsi, char *dest, int len, |
| 1203 | enum lws_token_indexes h); |
Andy Green | 16ab318 | 2013-02-10 18:02:31 +0800 | [diff] [blame] | 1204 | |
Andy Green | ea0642a | 2013-01-29 06:52:00 +0800 | [diff] [blame] | 1205 | /* |
| 1206 | * Note: this is not normally needed as a user api. It's provided in case it is |
| 1207 | * useful when integrating with other app poll loop service code. |
| 1208 | */ |
| 1209 | |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1210 | LWS_VISIBLE LWS_EXTERN int |
Andy Green | ea0642a | 2013-01-29 06:52:00 +0800 | [diff] [blame] | 1211 | libwebsocket_read(struct libwebsocket_context *context, |
| 1212 | struct libwebsocket *wsi, |
| 1213 | unsigned char *buf, size_t len); |
| 1214 | |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1215 | #ifndef LWS_NO_EXTENSIONS |
Peter Pentchev | 9a4fef7 | 2013-03-30 09:52:21 +0800 | [diff] [blame] | 1216 | LWS_VISIBLE LWS_EXTERN struct libwebsocket_extension *libwebsocket_get_internal_extensions(); |
Andy Green | 3182ece | 2013-01-20 17:08:31 +0800 | [diff] [blame] | 1217 | #endif |
Andy Green | 4cd87a0 | 2011-03-06 13:15:32 +0000 | [diff] [blame] | 1218 | |
Peter Hinz | 56885f3 | 2011-03-02 22:03:47 +0000 | [diff] [blame] | 1219 | #ifdef __cplusplus |
| 1220 | } |
| 1221 | #endif |
Andy Green | a11fe94 | 2011-09-25 10:30:26 +0100 | [diff] [blame] | 1222 | |
Andy Green | ab990e4 | 2010-10-31 12:42:52 +0000 | [diff] [blame] | 1223 | #endif |