1d4afb5ceSopenharmony_ci/*
2d4afb5ceSopenharmony_ci * libwebsockets - small server side websockets and web server implementation
3d4afb5ceSopenharmony_ci *
4d4afb5ceSopenharmony_ci * Copyright (C) 2010 - 2019 Andy Green <andy@warmcat.com>
5d4afb5ceSopenharmony_ci *
6d4afb5ceSopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a copy
7d4afb5ceSopenharmony_ci * of this software and associated documentation files (the "Software"), to
8d4afb5ceSopenharmony_ci * deal in the Software without restriction, including without limitation the
9d4afb5ceSopenharmony_ci * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10d4afb5ceSopenharmony_ci * sell copies of the Software, and to permit persons to whom the Software is
11d4afb5ceSopenharmony_ci * furnished to do so, subject to the following conditions:
12d4afb5ceSopenharmony_ci *
13d4afb5ceSopenharmony_ci * The above copyright notice and this permission notice shall be included in
14d4afb5ceSopenharmony_ci * all copies or substantial portions of the Software.
15d4afb5ceSopenharmony_ci *
16d4afb5ceSopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17d4afb5ceSopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18d4afb5ceSopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19d4afb5ceSopenharmony_ci * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20d4afb5ceSopenharmony_ci * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21d4afb5ceSopenharmony_ci * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
22d4afb5ceSopenharmony_ci * IN THE SOFTWARE.
23d4afb5ceSopenharmony_ci */
24d4afb5ceSopenharmony_ci
25d4afb5ceSopenharmony_ci#include <private-lib-core.h>
26d4afb5ceSopenharmony_ci
27d4afb5ceSopenharmony_cistatic int
28d4afb5ceSopenharmony_cirops_handle_POLLIN_raw_file(struct lws_context_per_thread *pt, struct lws *wsi,
29d4afb5ceSopenharmony_ci			    struct lws_pollfd *pollfd)
30d4afb5ceSopenharmony_ci{
31d4afb5ceSopenharmony_ci	int n;
32d4afb5ceSopenharmony_ci
33d4afb5ceSopenharmony_ci	if (pollfd->revents & LWS_POLLOUT) {
34d4afb5ceSopenharmony_ci		if (lws_change_pollfd(wsi, LWS_POLLOUT, 0)) {
35d4afb5ceSopenharmony_ci			lwsl_wsi_info(wsi, "failed at set pollfd");
36d4afb5ceSopenharmony_ci			return LWS_HPI_RET_WSI_ALREADY_DIED;
37d4afb5ceSopenharmony_ci		}
38d4afb5ceSopenharmony_ci		n = lws_callback_as_writeable(wsi);
39d4afb5ceSopenharmony_ci		if (n)
40d4afb5ceSopenharmony_ci			return LWS_HPI_RET_PLEASE_CLOSE_ME;
41d4afb5ceSopenharmony_ci	}
42d4afb5ceSopenharmony_ci
43d4afb5ceSopenharmony_ci	if (pollfd->revents & LWS_POLLIN) {
44d4afb5ceSopenharmony_ci		if (user_callback_handle_rxflow(wsi->a.protocol->callback,
45d4afb5ceSopenharmony_ci						wsi, LWS_CALLBACK_RAW_RX_FILE,
46d4afb5ceSopenharmony_ci						wsi->user_space, NULL, 0)) {
47d4afb5ceSopenharmony_ci			lwsl_wsi_debug(wsi, "raw rx callback closed it");
48d4afb5ceSopenharmony_ci			return LWS_HPI_RET_PLEASE_CLOSE_ME;
49d4afb5ceSopenharmony_ci		}
50d4afb5ceSopenharmony_ci	}
51d4afb5ceSopenharmony_ci
52d4afb5ceSopenharmony_ci	if (pollfd->revents & LWS_POLLHUP)
53d4afb5ceSopenharmony_ci		if (!(pollfd->revents & LWS_POLLIN))
54d4afb5ceSopenharmony_ci			return LWS_HPI_RET_PLEASE_CLOSE_ME;
55d4afb5ceSopenharmony_ci
56d4afb5ceSopenharmony_ci	return LWS_HPI_RET_HANDLED;
57d4afb5ceSopenharmony_ci}
58d4afb5ceSopenharmony_ci
59d4afb5ceSopenharmony_cistatic int
60d4afb5ceSopenharmony_cirops_adoption_bind_raw_file(struct lws *wsi, int type, const char *vh_prot_name)
61d4afb5ceSopenharmony_ci{
62d4afb5ceSopenharmony_ci	/* no socket or http: it can only be a raw file */
63d4afb5ceSopenharmony_ci	if ((type & LWS_ADOPT_HTTP) || (type & LWS_ADOPT_SOCKET) ||
64d4afb5ceSopenharmony_ci	    (type & _LWS_ADOPT_FINISH))
65d4afb5ceSopenharmony_ci		return 0; /* no match */
66d4afb5ceSopenharmony_ci
67d4afb5ceSopenharmony_ci	lws_role_transition(wsi, 0, LRS_ESTABLISHED, &role_ops_raw_file);
68d4afb5ceSopenharmony_ci
69d4afb5ceSopenharmony_ci	if (!vh_prot_name) {
70d4afb5ceSopenharmony_ci		if (wsi->a.vhost->default_protocol_index >=
71d4afb5ceSopenharmony_ci		    wsi->a.vhost->count_protocols)
72d4afb5ceSopenharmony_ci			return 0;
73d4afb5ceSopenharmony_ci
74d4afb5ceSopenharmony_ci		wsi->a.protocol = &wsi->a.vhost->protocols[
75d4afb5ceSopenharmony_ci					wsi->a.vhost->default_protocol_index];
76d4afb5ceSopenharmony_ci	}
77d4afb5ceSopenharmony_ci
78d4afb5ceSopenharmony_ci	return 1; /* bound */
79d4afb5ceSopenharmony_ci}
80d4afb5ceSopenharmony_ci
81d4afb5ceSopenharmony_cistatic const lws_rops_t rops_table_raw_file[] = {
82d4afb5ceSopenharmony_ci	/*  1 */ { .handle_POLLIN	= rops_handle_POLLIN_raw_file },
83d4afb5ceSopenharmony_ci	/*  2 */ { .adoption_bind	= rops_adoption_bind_raw_file },
84d4afb5ceSopenharmony_ci};
85d4afb5ceSopenharmony_ci
86d4afb5ceSopenharmony_ciconst struct lws_role_ops role_ops_raw_file = {
87d4afb5ceSopenharmony_ci	/* role name */			"raw-file",
88d4afb5ceSopenharmony_ci	/* alpn id */			NULL,
89d4afb5ceSopenharmony_ci
90d4afb5ceSopenharmony_ci	/* rops_table */		rops_table_raw_file,
91d4afb5ceSopenharmony_ci	/* rops_idx */			{
92d4afb5ceSopenharmony_ci	  /* LWS_ROPS_check_upgrades */
93d4afb5ceSopenharmony_ci	  /* LWS_ROPS_pt_init_destroy */		0x00,
94d4afb5ceSopenharmony_ci	  /* LWS_ROPS_init_vhost */
95d4afb5ceSopenharmony_ci	  /* LWS_ROPS_destroy_vhost */			0x00,
96d4afb5ceSopenharmony_ci	  /* LWS_ROPS_service_flag_pending */
97d4afb5ceSopenharmony_ci	  /* LWS_ROPS_handle_POLLIN */			0x01,
98d4afb5ceSopenharmony_ci	  /* LWS_ROPS_handle_POLLOUT */
99d4afb5ceSopenharmony_ci	  /* LWS_ROPS_perform_user_POLLOUT */		0x00,
100d4afb5ceSopenharmony_ci	  /* LWS_ROPS_callback_on_writable */
101d4afb5ceSopenharmony_ci	  /* LWS_ROPS_tx_credit */			0x00,
102d4afb5ceSopenharmony_ci	  /* LWS_ROPS_write_role_protocol */
103d4afb5ceSopenharmony_ci	  /* LWS_ROPS_encapsulation_parent */		0x00,
104d4afb5ceSopenharmony_ci	  /* LWS_ROPS_alpn_negotiated */
105d4afb5ceSopenharmony_ci	  /* LWS_ROPS_close_via_role_protocol */	0x00,
106d4afb5ceSopenharmony_ci	  /* LWS_ROPS_close_role */
107d4afb5ceSopenharmony_ci	  /* LWS_ROPS_close_kill_connection */		0x00,
108d4afb5ceSopenharmony_ci	  /* LWS_ROPS_destroy_role */
109d4afb5ceSopenharmony_ci	  /* LWS_ROPS_adoption_bind */			0x02,
110d4afb5ceSopenharmony_ci	  /* LWS_ROPS_client_bind */
111d4afb5ceSopenharmony_ci	  /* LWS_ROPS_issue_keepalive */		0x00,
112d4afb5ceSopenharmony_ci					},
113d4afb5ceSopenharmony_ci
114d4afb5ceSopenharmony_ci	/* adoption_cb clnt, srv */	{ LWS_CALLBACK_RAW_ADOPT_FILE,
115d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_ADOPT_FILE },
116d4afb5ceSopenharmony_ci	/* rx_cb clnt, srv */		{ LWS_CALLBACK_RAW_RX_FILE,
117d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_RX_FILE },
118d4afb5ceSopenharmony_ci	/* writeable cb clnt, srv */	{ LWS_CALLBACK_RAW_WRITEABLE_FILE,
119d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_WRITEABLE_FILE},
120d4afb5ceSopenharmony_ci	/* close cb clnt, srv */	{ LWS_CALLBACK_RAW_CLOSE_FILE,
121d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_CLOSE_FILE},
122d4afb5ceSopenharmony_ci	/* protocol_bind cb c, srv */	{ LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL,
123d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_FILE_BIND_PROTOCOL },
124d4afb5ceSopenharmony_ci	/* protocol_unbind cb c, srv */	{ LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL,
125d4afb5ceSopenharmony_ci					  LWS_CALLBACK_RAW_FILE_DROP_PROTOCOL },
126d4afb5ceSopenharmony_ci	/* file_handle */		1,
127d4afb5ceSopenharmony_ci};
128