File: protocol_client_loopback_test.c

package info (click to toggle)
libwebsockets 4.3.5-3
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 31,404 kB
  • sloc: ansic: 194,409; javascript: 1,550; sh: 1,387; cpp: 505; java: 461; perl: 405; xml: 118; makefile: 76; awk: 5
file content (193 lines) | stat: -rw-r--r-- 4,878 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
/*
 * ws protocol handler plugin for "client_loopback_test"
 *
 * Written in 2010-2019 by Andy Green <andy@warmcat.com>
 *
 * This file is made available under the Creative Commons CC0 1.0
 * Universal Public Domain Dedication.
 *
 * The person who associated a work with this deed has dedicated
 * the work to the public domain by waiving all of his or her rights
 * to the work worldwide under copyright law, including all related
 * and neighboring rights, to the extent allowed by law. You can copy,
 * modify, distribute and perform the work, even for commercial purposes,
 * all without asking permission.
 *
 * These test plugins are intended to be adapted for use in your code, which
 * may be proprietary.  So unlike the library itself, they are licensed
 * Public Domain.
 */

#if !defined(LWS_DLL)
#define LWS_DLL
#endif
#if !defined(LWS_INTERNAL)
#define LWS_INTERNAL
#endif
#include <libwebsockets.h>
#include <string.h>

struct per_session_data__client_loopback_test {
	struct lws *wsi;
};

/*
 * This is a bit fiddly...
 *
 * 0) If you want the wss:// test to work, make sure the vhost is marked with
 *    enable-client-ssl if using lwsws, or call lws_init_vhost_client_ssl() on
 *    the vhost if you're doing it by hand.
 *
 * 1) enable the protocol on a vhost
 *
 *      "ws-protocols": [{
 *     "client-loopback-test": {
 *      "status": "ok"
 *     },  ...
 *
 *     the vhost should listen on 80 (ws://) or 443 (wss://)
 *
 * 2) mount the http part of the test one level down on the same vhost, eg
 *   {
 *      "mountpoint": "/c",
 *      "origin": "callback://client-loopback-test"
 *   }
 *
 * 3) Use a browser to visit the mountpoint with a URI attached for looping
 *    back, eg, if testing on localhost
 *
 *    http://localhost/c/ws://localhost
 *    https://localhost/c/wss://localhost
 *
 * 4) The HTTP part of this test protocol will try to do the requested
 *    ws client connection, to the same test protocol on the same
 *    server.
 */

static int
callback_client_loopback_test(struct lws *wsi, enum lws_callback_reasons reason,
			void *user, void *in, size_t len)
{
	struct lws_client_connect_info i;
	struct per_session_data__client_loopback_test *pss =
			(struct per_session_data__client_loopback_test *)user;
	const char *p = (const char *)in;
	char buf[100];
	int n;

	switch (reason) {

	/* HTTP part */

	case LWS_CALLBACK_HTTP:
		if (len < 10)
			return -1;

		p++;
		while (*p && *p != '/')
			p++;
		if (!*p) {
			lws_return_http_status(wsi, 400, "Arg needs to be in format ws://xxx or wss://xxx");
			return -1;
		}
		p++;

		memset(&i, 0, sizeof(i));
		i.context = lws_get_context(wsi);

		// stacked /// get resolved to /

		if (strncmp(p, "ws:/", 4) == 0) {
			i.ssl_connection = 0;
			i.port = 80;
			p += 4;
		} else
			if (strncmp(p, "wss:/", 5) == 0) {
				i.port = 443;
				i.ssl_connection = 1;
				p += 5;
			} else {
				sprintf(buf, "Arg %s is not in format ws://xxx or wss://xxx\n", p);
				lws_return_http_status(wsi, 400, buf);
				return -1;
			}

		i.address = p;
		i.path = "";
		i.host = p;
		i.origin = p;
		i.ietf_version_or_minus_one = -1;
		i.protocol = "client-loopback-test";

		pss->wsi = lws_client_connect_via_info(&i);
		if (!pss->wsi)
			lws_return_http_status(wsi, 401, "client-loopback-test: connect failed\n");
		else {
			lwsl_notice("client connection to %s:%d with ssl: %d started\n",
				    i.address, i.port, i.ssl_connection);
			lws_return_http_status(wsi, 200, "OK");
		}

		/* either way, close the triggering http link */

		return -1;

	case LWS_CALLBACK_CLOSED_HTTP:
		lwsl_notice("Http part closed\n");
		break;

	/* server part */

	case LWS_CALLBACK_ESTABLISHED:
		lwsl_notice("server part: LWS_CALLBACK_ESTABLISHED\n");
		strcpy(buf + LWS_PRE, "Made it");
		n = lws_write(wsi, (unsigned char *)buf + LWS_PRE,
			      7, LWS_WRITE_TEXT);
		if (n < 7)
			return -1;
		break;

	/* client part */

	case LWS_CALLBACK_CLIENT_ESTABLISHED:
		lwsl_notice("Client connection established\n");
		break;

	case LWS_CALLBACK_CLIENT_RECEIVE:
		lws_strncpy(buf, in, sizeof(buf));
		lwsl_notice("Client connection received %ld from server '%s'\n",
			    (long)len, buf);

		/* OK we are done with the client connection */
		return -1;

	default:
		break;
	}

	return 0;
}

LWS_VISIBLE const struct lws_protocols client_loopback_test_protocols[] = {
	{
		"client-loopback-test",
		callback_client_loopback_test,
		sizeof(struct per_session_data__client_loopback_test),
		1024, /* rx buf size must be >= permessage-deflate rx size */
		0, NULL, 0
	},
};

LWS_VISIBLE const lws_plugin_protocol_t client_loopback_test = {
	.hdr = {
		"client loopback test",
		"lws_protocol_plugin",
		LWS_BUILD_HASH,
		LWS_PLUGIN_API_MAGIC
	},

	.protocols = client_loopback_test_protocols,
	.count_protocols = LWS_ARRAY_SIZE(client_loopback_test_protocols),
	.extensions = NULL,
	.count_extensions = 0,
};