File: ss-server.c

package info (click to toggle)
libwebsockets 4.3.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,288 kB
  • sloc: ansic: 194,407; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (235 lines) | stat: -rw-r--r-- 5,954 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * lws-minimal-secure-streams-server
 *
 * Written in 2010-2020 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 */

#include <libwebsockets.h>
#include <assert.h>

extern int interrupted, bad, multipart;

static const char *html =
		/* normally we serve this... */
	"<head><meta content=\"text/html;charset=utf-8\" "
			"http-equiv=\"Content-Type\"><script>"
	" var ws = new WebSocket(\"wss://localhost:7681\", \"mywsprotocol\");"
	"try { ws.onopen = function() { console.log(\"open\"); }; "
		"ws.onmessage = function got_packet(msg) { "
		   "var s=\"\"; s += msg.data; "
		   "document.getElementById(\"wsd\").innerHTML = s; };"
		"} catch(exception) {"
		"alert(\"<p>Error\" + exception); }"
	"</script></head><html><body>"
	  "Hello from the web server<br>"
	  "<div id=\"wsd\"></div>"
	"</body></html>",

*multipart_html =
	/*
	 * If you use -m commandline switch we send this instead, as
	 * multipart/form-data
	 */
	"--aBoundaryString\r\n"
	"Content-Disposition: form-data; name=\"myFile\"; filename=\"xxx.txt\"\r\n"
	"Content-Type: text/plain\r\n"
	"\r\n"
	"The file contents\r\n"
	"--aBoundaryString\r\n"
	"Content-Disposition: form-data; name=\"myField\"\r\n"
	"\r\n"
	"(data)\r\n"
	"--aBoundaryString--\r\n";


typedef struct myss {
	struct lws_ss_handle 		*ss;
	void				*opaque_data;
	/* ... application specific state ... */

	lws_sorted_usec_list_t		sul;
	int				count;
	char				upgraded;

} myss_srv_t;

/*
 * This is the Secure Streams Server RX and TX for HTTP(S)
 */

static lws_ss_state_return_t
myss_srv_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
{
//	myss_srv_t *m = (myss_srv_t *)userobj;

	lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
	lwsl_hexdump_info(buf, len);

	/*
	 * If we received the whole message, for our example it means
	 * we are done.
	 */
	if (flags & LWSSS_FLAG_EOM) {
		bad = 0;
		interrupted = 1;
	}

	return 0;
}

static lws_ss_state_return_t
myss_srv_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
	int *flags)
{
	myss_srv_t *m = (myss_srv_t *)userobj;
	const char *send = html;

	if (m->upgraded)
		return LWSSSSRET_TX_DONT_SEND;

	if (multipart)
		send = multipart_html;

	*flags = LWSSS_FLAG_SOM | LWSSS_FLAG_EOM;

	lws_strncpy((char *)buf, send, *len);
	*len = strlen(send);

	return 0;
}

/*
 * This is the Secure Streams Server RX and TX for WS(S)... when we get a
 * state that the underlying connection upgraded protocol, we switch the stream
 * rx and tx handlers to here.
 */

static lws_ss_state_return_t
myss_ws_rx(void *userobj, const uint8_t *buf, size_t len, int flags)
{
//	myss_srv_t *m = (myss_srv_t *)userobj;

	lwsl_user("%s: len %d, flags: %d\n", __func__, (int)len, flags);
	lwsl_hexdump_info(buf, len);

	/*
	 * If we received the whole message, for our example it means
	 * we are done.
	 */
	if (flags & LWSSS_FLAG_EOM) {
		bad = 0;
		interrupted = 1;
	}

	return 0;
}

/* this is the callback that mediates sending the incrementing number */

static void
spam_sul_cb(struct lws_sorted_usec_list *sul)
{
	myss_srv_t *m = lws_container_of(sul, myss_srv_t, sul);

	if (!lws_ss_request_tx(m->ss))
		lws_sul_schedule(lws_ss_get_context(m->ss), 0, &m->sul, spam_sul_cb,
			 100 * LWS_US_PER_MS);
}

static lws_ss_state_return_t
myss_ws_tx(void *userobj, lws_ss_tx_ordinal_t ord, uint8_t *buf, size_t *len,
	int *flags)
{
	myss_srv_t *m = (myss_srv_t *)userobj;

	*flags = LWSSS_FLAG_SOM | LWSSS_FLAG_EOM;

	*len = (unsigned int)lws_snprintf((char *)buf, *len, "hello from ws %d", m->count++);

	lws_sul_schedule(lws_ss_get_context(m->ss), 0, &m->sul, spam_sul_cb,
			 100 * LWS_US_PER_MS);

	return 0;
}

static lws_ss_state_return_t
myss_srv_state(void *userobj, void *sh, lws_ss_constate_t state,
	   lws_ss_tx_ordinal_t ack)
{
	myss_srv_t *m = (myss_srv_t *)userobj;

	lwsl_user("%s: %p %s, ord 0x%x\n", __func__, m->ss,
		  lws_ss_state_name((int)state), (unsigned int)ack);

	switch (state) {
	case LWSSSCS_DISCONNECTED:
		lws_sul_cancel(&m->sul);
		break;
	case LWSSSCS_CREATING:
		return lws_ss_request_tx(m->ss);

	case LWSSSCS_ALL_RETRIES_FAILED:
		/* if we're out of retries, we want to close the app and FAIL */
		interrupted = 1;
		break;

	case LWSSSCS_SERVER_TXN:
		/*
		 * The underlying protocol started a transaction, let's
		 * describe how we want to complete it.  We can defer this until
		 * later, eg, after we have consumed any rx that's coming with
		 * the client's transaction initiation phase, but in this
		 * example we know what we want to do already.
		 *
		 * We do want to ack the transaction...
		 */
		lws_ss_server_ack(m->ss, 0);
		/*
		 * ... it's going to be either text/html or multipart ...
		 */
		if (multipart) {
			if (lws_ss_set_metadata(m->ss, "mime",
			   "multipart/form-data; boundary=aBoundaryString", 45))
				return LWSSSSRET_DISCONNECT_ME;
		} else
			if (lws_ss_set_metadata(m->ss, "mime", "text/html", 9))
				return LWSSSSRET_DISCONNECT_ME;
		/*
		 * ...it's going to be whatever size it is (and request tx)
		 */
		return lws_ss_request_tx_len(m->ss, (unsigned long)
				(multipart ? strlen(multipart_html) :
							 strlen(html)));

	case LWSSSCS_SERVER_UPGRADE:

		/*
		 * This is sent when the underlying protocol has experienced
		 * an upgrade, eg, http->ws... it's a one-way upgrade on this
		 * stream, change the handlers to deal with the kind of
		 * messages we send on ws
		 */

		m->upgraded = 1;
		lws_ss_change_handlers(m->ss, myss_ws_rx, myss_ws_tx, NULL);
		return lws_ss_request_tx(m->ss); /* we want to start sending numbers */

	default:
		break;
	}

	return 0;
}

const lws_ss_info_t ssi_server = {
	.handle_offset			= offsetof(myss_srv_t, ss),
	.opaque_user_data_offset	= offsetof(myss_srv_t, opaque_data),
	.streamtype			= "myserver",
	.rx				= myss_srv_rx,
	.tx				= myss_srv_tx,
	.state				= myss_srv_state,
	.user_alloc			= sizeof(myss_srv_t),
};