2013-01-16 12:21:29 +08:00
|
|
|
/*
|
|
|
|
* libwebsockets - small server side websockets and web server implementation
|
|
|
|
*
|
2017-09-23 12:55:21 +08:00
|
|
|
* Copyright (C) 2010-2017 Andy Green <andy@warmcat.com>
|
2013-01-16 12:21:29 +08:00
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation:
|
|
|
|
* version 2.1 of the License.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
|
|
|
* License along with this library; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
|
|
|
|
* MA 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "private-libwebsockets.h"
|
|
|
|
|
2013-02-23 10:50:10 +08:00
|
|
|
/*
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
* notice this returns number of bytes consumed, or -1
|
2013-02-23 10:50:10 +08:00
|
|
|
*/
|
2015-12-04 11:08:32 +08:00
|
|
|
int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2015-12-17 18:25:25 +08:00
|
|
|
struct lws_context *context = lws_get_context(wsi);
|
2017-05-07 10:02:03 +08:00
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
2013-12-10 21:15:00 +08:00
|
|
|
size_t real_len = len;
|
2016-03-18 23:55:59 +08:00
|
|
|
unsigned int n;
|
2018-03-19 09:33:55 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2016-03-18 23:55:59 +08:00
|
|
|
int m;
|
2018-03-19 08:28:28 +08:00
|
|
|
#endif
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2018-03-27 09:17:19 +08:00
|
|
|
// lwsl_hexdump_notice(buf, len);
|
|
|
|
|
2017-12-07 07:20:47 +08:00
|
|
|
/*
|
|
|
|
* Detect if we got called twice without going through the
|
|
|
|
* event loop to handle pending. This would be caused by either
|
|
|
|
* back-to-back writes in one WRITABLE (illegal) or calling lws_write()
|
|
|
|
* from outside the WRITABLE callback (illegal).
|
|
|
|
*/
|
|
|
|
if (wsi->could_have_pending) {
|
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
2018-04-11 13:39:42 +08:00
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, role %s: "
|
2017-12-07 07:20:47 +08:00
|
|
|
"Illegal back-to-back write of %lu detected...\n",
|
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
2018-04-11 13:39:42 +08:00
|
|
|
wsi->role_ops->name,
|
2017-12-07 07:20:47 +08:00
|
|
|
(unsigned long)len);
|
2018-04-13 06:43:11 +08:00
|
|
|
// assert(0);
|
2017-12-07 07:20:47 +08:00
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_WRITE, 1);
|
|
|
|
|
2014-04-01 14:20:44 +08:00
|
|
|
if (!len)
|
|
|
|
return 0;
|
2014-04-10 14:25:24 +08:00
|
|
|
/* just ignore sends after we cleared the truncation buffer */
|
2018-04-02 11:55:17 +08:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE && !wsi->trunc_len)
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)len;
|
2013-12-09 14:16:17 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
|
2016-07-23 14:18:25 +08:00
|
|
|
buf > (wsi->trunc_alloc + wsi->trunc_len + wsi->trunc_offset))) {
|
2017-12-07 07:20:47 +08:00
|
|
|
lwsl_hexdump_level(LLL_ERR, buf, len);
|
|
|
|
lwsl_err("** %p: vh: %s, prot: %s, Sending new %lu, pending truncated ...\n"
|
2017-10-28 07:42:44 +08:00
|
|
|
" It's illegal to do an lws_write outside of\n"
|
|
|
|
" the writable callback: fix your code\n",
|
2017-12-07 07:20:47 +08:00
|
|
|
wsi, wsi->vhost->name, wsi->protocol->name,
|
|
|
|
(unsigned long)len);
|
2013-12-09 14:16:17 +08:00
|
|
|
assert(0);
|
2016-04-14 15:07:44 +08:00
|
|
|
|
|
|
|
return -1;
|
2013-12-09 14:16:17 +08:00
|
|
|
}
|
2018-03-19 09:33:55 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
2017-10-25 08:00:23 +08:00
|
|
|
m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, (int)len);
|
2014-04-02 19:45:42 +08:00
|
|
|
if (m < 0)
|
|
|
|
return -1;
|
|
|
|
if (m) /* handled */ {
|
|
|
|
n = m;
|
|
|
|
goto handle_truncated_send;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
2018-03-19 08:17:05 +08:00
|
|
|
#endif
|
2017-10-13 10:33:02 +08:00
|
|
|
if (!wsi->http2_substream && !lws_socket_is_valid(wsi->desc.sockfd))
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
lwsl_warn("** error invalid sock but expected to send\n");
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2016-03-18 23:55:59 +08:00
|
|
|
/* limit sending */
|
2017-03-16 10:46:31 +08:00
|
|
|
if (wsi->protocol->tx_packet_size)
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)wsi->protocol->tx_packet_size;
|
2017-03-16 10:46:31 +08:00
|
|
|
else {
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)wsi->protocol->rx_buffer_size;
|
2017-03-16 10:46:31 +08:00
|
|
|
if (!n)
|
|
|
|
n = context->pt_serv_buf_size;
|
|
|
|
}
|
2016-05-15 08:59:48 +08:00
|
|
|
n += LWS_PRE + 4;
|
2016-03-18 23:55:59 +08:00
|
|
|
if (n > len)
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)len;
|
2016-03-18 15:02:27 +08:00
|
|
|
|
2015-12-06 05:52:09 +08:00
|
|
|
/* nope, send it on the socket directly */
|
2013-01-29 12:37:35 +08:00
|
|
|
lws_latency_pre(context, wsi);
|
2016-03-18 23:55:59 +08:00
|
|
|
n = lws_ssl_capable_write(wsi, buf, n);
|
|
|
|
lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2017-12-07 07:20:47 +08:00
|
|
|
/* something got written, it can have been truncated now */
|
|
|
|
wsi->could_have_pending = 1;
|
|
|
|
|
2014-04-06 06:26:35 +01:00
|
|
|
switch (n) {
|
|
|
|
case LWS_SSL_CAPABLE_ERROR:
|
2014-10-16 08:23:46 +08:00
|
|
|
/* we're going to close, let close know sends aren't possible */
|
|
|
|
wsi->socket_is_permanently_unusable = 1;
|
2014-04-06 06:26:35 +01:00
|
|
|
return -1;
|
|
|
|
case LWS_SSL_CAPABLE_MORE_SERVICE:
|
2017-12-07 07:20:47 +08:00
|
|
|
/*
|
|
|
|
* nothing got sent, not fatal. Retry the whole thing later,
|
|
|
|
* ie, implying treat it was a truncated send so it gets
|
|
|
|
* retried
|
|
|
|
*/
|
2014-04-06 06:26:35 +01:00
|
|
|
n = 0;
|
2014-04-10 14:08:10 +08:00
|
|
|
break;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
2018-03-19 09:33:55 +08:00
|
|
|
#if !defined(LWS_WITHOUT_EXTENSIONS)
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
handle_truncated_send:
|
2018-03-19 08:17:05 +08:00
|
|
|
#endif
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
/*
|
2014-04-10 14:08:10 +08:00
|
|
|
* we were already handling a truncated send?
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
*/
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len) {
|
2017-02-05 22:07:34 +08:00
|
|
|
lwsl_info("%p partial adv %d (vs %ld)\n", wsi, n, (long)real_len);
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->trunc_offset += n;
|
|
|
|
wsi->trunc_len -= n;
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_len) {
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_info("** %p partial send completed\n", wsi);
|
2014-03-23 11:41:15 +08:00
|
|
|
/* done with it, but don't free it */
|
2017-10-25 08:00:23 +08:00
|
|
|
n = (int)real_len;
|
2018-04-02 11:55:17 +08:00
|
|
|
if (lwsi_state(wsi) == LRS_FLUSHING_BEFORE_CLOSE) {
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_info("** %p signalling to close now\n", wsi);
|
2014-04-10 14:25:24 +08:00
|
|
|
return -1; /* retry closing now */
|
2014-04-10 17:06:59 +08:00
|
|
|
}
|
2014-04-10 11:23:18 +08:00
|
|
|
}
|
|
|
|
/* always callback on writeable */
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
2015-11-02 13:10:33 +08:00
|
|
|
if ((unsigned int)n == real_len)
|
2014-04-10 14:08:10 +08:00
|
|
|
/* what we just sent went out cleanly */
|
|
|
|
return n;
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/*
|
|
|
|
* Newly truncated send. Buffer the remainder (it will get
|
2017-12-07 07:20:47 +08:00
|
|
|
* first priority next time the socket is writable).
|
2014-04-10 14:08:10 +08:00
|
|
|
*/
|
2017-05-07 08:19:55 +08:00
|
|
|
lwsl_debug("%p new partial sent %d from %lu total\n", wsi, n,
|
2017-02-04 13:09:00 +01:00
|
|
|
(unsigned long)real_len);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_WRITE_PARTIALS, 1);
|
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_PARTIALS_ACCEPTED_PARTS, n);
|
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/*
|
|
|
|
* - if we still have a suitable malloc lying around, use it
|
|
|
|
* - or, if too small, reallocate it
|
|
|
|
* - or, if no buffer, create it
|
|
|
|
*/
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
|
|
|
|
lws_free(wsi->trunc_alloc);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
wsi->trunc_alloc_len = (unsigned int)(real_len - n);
|
2017-10-28 07:42:44 +08:00
|
|
|
wsi->trunc_alloc = lws_malloc(real_len - n,
|
|
|
|
"truncated send alloc");
|
2015-12-17 17:03:59 +08:00
|
|
|
if (!wsi->trunc_alloc) {
|
2017-02-04 13:09:00 +01:00
|
|
|
lwsl_err("truncated send: unable to malloc %lu\n",
|
|
|
|
(unsigned long)(real_len - n));
|
2014-04-10 14:08:10 +08:00
|
|
|
return -1;
|
|
|
|
}
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
}
|
2015-12-17 17:03:59 +08:00
|
|
|
wsi->trunc_offset = 0;
|
2017-10-25 08:00:23 +08:00
|
|
|
wsi->trunc_len = (unsigned int)(real_len - n);
|
2015-12-17 17:03:59 +08:00
|
|
|
memcpy(wsi->trunc_alloc, buf + n, real_len - n);
|
2014-04-10 14:08:10 +08:00
|
|
|
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
|
|
|
/* stash original destination for fulfilling UDP partials */
|
|
|
|
wsi->udp->sa_pending = wsi->udp->sa;
|
|
|
|
wsi->udp->salen_pending = wsi->udp->salen;
|
|
|
|
}
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
/* since something buffered, force it to get another chance to send */
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
add explicit error for partial send
This patch adds code to handle the situation that a prepared user buffer could not all be sent on the
socket at once. There are two kinds of situation to handle
1) User code handles it: The connection only has extensions active that do not rewrite the buffer.
In this case, the patch caused libwebsocket_write() to simply return the amount of user buffer that
was consumed (this is specifically the amount of user buffer used in sending what was accepted,
nothing else). So user code can just advance its buffer that much and resume sending when the socket
is writable again. This continues the frame rather than starting a new one or new fragment.
2) The connections has extensions active which actually send something quite different than what the
user buffer contains, for example a compression extension. In this case, libwebsockets will dynamically
malloc a buffer to contain a copy of the remaining unsent data, request notifiction when writeable again,
and automatically spill and free this buffer with the highest priority before passing on the writable
notification to anything else. For this situation, the call to write will return that it used the
whole user buffer, even though part is still rebuffered.
This patch should enable libwebsockets to detect the two cases and take the appropriate action.
There are also two choices for user code to deal with partial sends.
1) Leave the no_buffer_all_partial_tx member in the protocol struct at zero. The library will dyamically
buffer anything you send that did not get completely written to the socket, and automatically spill it next
time the socket is writable. You can use this method if your sent frames are relatvely small and unlikely to get
truncated anyway.
2) Set the no_buffer_all_partial_tx member in the protocol struct. User code now needs to take care of the
return value from libwebsocket_write() and deal with resending the remainder if not all of the requested amount
got sent. You should use this method if you are sending large messages and want to maximize throughput and efficiency.
Since the new member no_buffer_all_partial_tx will be zero by default, this patch will auto-rebuffer any
partial sends by default. That's good for most cases but if you attempt to send large blocks, make sure you
follow option 2) above.
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-10-17 08:09:19 +08:00
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)real_len;
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
2016-01-11 11:34:01 +08:00
|
|
|
LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
|
|
|
|
enum lws_write_protocol wp)
|
2013-01-16 12:21:29 +08:00
|
|
|
{
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2017-07-19 04:39:14 +08:00
|
|
|
if (wsi->parent_carries_io) {
|
|
|
|
struct lws_write_passthru pas;
|
|
|
|
|
|
|
|
pas.buf = buf;
|
|
|
|
pas.len = len;
|
|
|
|
pas.wp = wp;
|
|
|
|
pas.wsi = wsi;
|
|
|
|
|
|
|
|
if (wsi->parent->protocol->callback(wsi->parent,
|
|
|
|
LWS_CALLBACK_CHILD_WRITE_VIA_PARENT,
|
|
|
|
wsi->parent->user_space,
|
|
|
|
(void *)&pas, 0))
|
|
|
|
return 1;
|
|
|
|
|
2017-10-25 08:00:23 +08:00
|
|
|
return (int)len;
|
2017-07-19 04:39:14 +08:00
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_C_API_LWS_WRITE, 1);
|
|
|
|
|
2017-05-08 10:49:10 +08:00
|
|
|
if ((int)len < 0) {
|
|
|
|
lwsl_err("%s: suspicious len int %d, ulong %lu\n", __func__,
|
|
|
|
(int)len, (unsigned long)len);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(wsi->context, pt, LWSSTATS_B_WRITE, len);
|
|
|
|
|
2016-04-15 12:00:23 +08:00
|
|
|
#ifdef LWS_WITH_ACCESS_LOG
|
|
|
|
wsi->access_log.sent += len;
|
|
|
|
#endif
|
2016-04-15 14:01:29 +08:00
|
|
|
if (wsi->vhost)
|
2016-12-12 13:36:25 +08:00
|
|
|
wsi->vhost->conn_stats.tx += len;
|
2016-04-15 12:00:23 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
assert(wsi->role_ops);
|
|
|
|
if (!wsi->role_ops->write_role_protocol)
|
|
|
|
return lws_issue_raw(wsi, buf, len);
|
2013-01-16 12:21:29 +08:00
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
return wsi->role_ops->write_role_protocol(wsi, buf, len, &wp);
|
2013-01-16 12:21:29 +08:00
|
|
|
}
|
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
|
2013-01-22 07:20:08 +08:00
|
|
|
{
|
2015-12-16 18:19:08 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
2016-01-19 03:34:24 +08:00
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2016-05-19 15:28:31 +08:00
|
|
|
struct lws_process_html_args args;
|
2017-02-25 12:42:45 +08:00
|
|
|
lws_filepos_t amount, poss;
|
2017-10-13 10:33:02 +08:00
|
|
|
unsigned char *p, *pstart;
|
2016-12-12 13:36:25 +08:00
|
|
|
#if defined(LWS_WITH_RANGES)
|
|
|
|
unsigned char finished = 0;
|
|
|
|
#endif
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
int n, m;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
lwsl_debug("wsi->http2_substream %d\n", wsi->http2_substream);
|
|
|
|
|
2018-03-27 09:17:19 +08:00
|
|
|
do {
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2015-12-17 17:03:59 +08:00
|
|
|
if (wsi->trunc_len) {
|
|
|
|
if (lws_issue_raw(wsi, wsi->trunc_alloc +
|
|
|
|
wsi->trunc_offset,
|
|
|
|
wsi->trunc_len) < 0) {
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
lwsl_info("%s: closing\n", __func__);
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it;
|
2014-04-10 14:25:24 +08:00
|
|
|
}
|
2018-04-04 10:44:51 +08:00
|
|
|
break;
|
2013-12-09 14:16:17 +08:00
|
|
|
}
|
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.filepos == wsi->http.filelen)
|
2013-12-09 14:16:17 +08:00
|
|
|
goto all_sent;
|
|
|
|
|
2016-12-12 13:36:25 +08:00
|
|
|
n = 0;
|
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
pstart = pt->serv_buf + LWS_H2_FRAME_HEADER_LENGTH;
|
|
|
|
|
|
|
|
p = pstart;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_RANGES)
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.range.count_ranges && !wsi->http.range.inside) {
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2017-10-28 07:42:44 +08:00
|
|
|
lwsl_notice("%s: doing range start %llu\n", __func__,
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.range.start);
|
2016-05-19 15:28:31 +08:00
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
if ((long long)lws_vfs_file_seek_cur(wsi->http.fop_fd,
|
|
|
|
wsi->http.range.start -
|
|
|
|
wsi->http.filepos) < 0)
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.filepos = wsi->http.range.start;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.range.count_ranges > 1) {
|
2017-10-28 07:42:44 +08:00
|
|
|
n = lws_snprintf((char *)p,
|
|
|
|
context->pt_serv_buf_size -
|
|
|
|
LWS_H2_FRAME_HEADER_LENGTH,
|
2016-12-12 13:36:25 +08:00
|
|
|
"_lws\x0d\x0a"
|
|
|
|
"Content-Type: %s\x0d\x0a"
|
|
|
|
"Content-Range: bytes %llu-%llu/%llu\x0d\x0a"
|
|
|
|
"\x0d\x0a",
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.multipart_content_type,
|
|
|
|
wsi->http.range.start,
|
|
|
|
wsi->http.range.end,
|
|
|
|
wsi->http.range.extent);
|
2016-12-12 13:36:25 +08:00
|
|
|
p += n;
|
|
|
|
}
|
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.range.budget = wsi->http.range.end -
|
|
|
|
wsi->http.range.start + 1;
|
|
|
|
wsi->http.range.inside = 1;
|
2016-12-12 13:36:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
poss = context->pt_serv_buf_size - n - LWS_H2_FRAME_HEADER_LENGTH;
|
2017-03-16 10:46:31 +08:00
|
|
|
|
2018-01-14 20:09:41 +08:00
|
|
|
if (wsi->http.tx_content_length)
|
|
|
|
if (poss > wsi->http.tx_content_remain)
|
|
|
|
poss = wsi->http.tx_content_remain;
|
2017-11-05 16:31:14 +08:00
|
|
|
|
2017-03-16 10:46:31 +08:00
|
|
|
/*
|
2018-04-11 13:39:42 +08:00
|
|
|
* if there is a hint about how much we will do well to send at
|
|
|
|
* one time, restrict ourselves to only trying to send that.
|
2017-03-16 10:46:31 +08:00
|
|
|
*/
|
2017-10-13 10:33:02 +08:00
|
|
|
if (wsi->protocol->tx_packet_size &&
|
|
|
|
poss > wsi->protocol->tx_packet_size)
|
2017-03-16 10:46:31 +08:00
|
|
|
poss = wsi->protocol->tx_packet_size;
|
|
|
|
|
2018-04-11 13:39:42 +08:00
|
|
|
if (wsi->role_ops->tx_credit) {
|
|
|
|
lws_filepos_t txc = wsi->role_ops->tx_credit(wsi);
|
|
|
|
|
|
|
|
if (!txc) {
|
|
|
|
lwsl_info("%s: came here with no tx credit\n",
|
|
|
|
__func__);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (txc < poss)
|
|
|
|
poss = txc;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* consumption of the actual payload amount sent will be
|
|
|
|
* handled when the role data frame is sent
|
|
|
|
*/
|
2017-10-13 10:33:02 +08:00
|
|
|
}
|
|
|
|
|
2016-12-12 13:36:25 +08:00
|
|
|
#if defined(LWS_WITH_RANGES)
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.range.count_ranges) {
|
|
|
|
if (wsi->http.range.count_ranges > 1)
|
2016-12-12 13:36:25 +08:00
|
|
|
poss -= 7; /* allow for final boundary */
|
2017-12-01 11:09:32 +08:00
|
|
|
if (poss > wsi->http.range.budget)
|
|
|
|
poss = wsi->http.range.budget;
|
2016-12-12 13:36:25 +08:00
|
|
|
}
|
|
|
|
#endif
|
2016-05-19 15:28:31 +08:00
|
|
|
if (wsi->sending_chunked) {
|
|
|
|
/* we need to drop the chunk size in here */
|
|
|
|
p += 10;
|
|
|
|
/* allow for the chunk to grow by 128 in translation */
|
|
|
|
poss -= 10 + 128;
|
|
|
|
}
|
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
if (lws_vfs_file_read(wsi->http.fop_fd, &amount, p, poss) < 0)
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it; /* caller will close */
|
lws_plat_fd implement platform default handlers
This is a rewrite of the patch from Soapyman here
https://github.com/warmcat/libwebsockets/pull/363
The main changes compared to Soapyman's original patch are
- There's no new stuff in the info struct user code does any overrides
it may want to do explicitly after lws_context_create returns
- User overrides for file ops can call through (subclass) to the original
platform implementation using lws_get_fops_plat()
- A typedef is provided for plat-specific fd type
- Public helpers are provided to allow user code to be platform-independent
about file access, using the lws platform file operations underneath:
static inline lws_filefd_type
lws_plat_file_open(struct lws_plat_file_ops *fops, const char *filename,
unsigned long *filelen, int flags)
static inline int
lws_plat_file_close(struct lws_plat_file_ops *fops, lws_filefd_type fd)
static inline unsigned long
lws_plat_file_seek_cur(struct lws_plat_file_ops *fops, lws_filefd_type fd,
long offset_from_cur_pos)
static inline int
lws_plat_file_read(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
static inline int
lws_plat_file_write(struct lws_plat_file_ops *fops, lws_filefd_type fd,
unsigned long *amount, unsigned char *buf, unsigned long len)
There's example documentation and implementation in the test server.
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-12-10 07:58:58 +08:00
|
|
|
|
2016-12-12 13:36:25 +08:00
|
|
|
if (wsi->sending_chunked)
|
|
|
|
n = (int)amount;
|
|
|
|
else
|
2017-10-25 08:00:23 +08:00
|
|
|
n = lws_ptr_diff(p, pstart) + (int)amount;
|
2017-10-13 10:33:02 +08:00
|
|
|
|
|
|
|
lwsl_debug("%s: sending %d\n", __func__, n);
|
|
|
|
|
2014-02-27 03:21:50 +01:00
|
|
|
if (n) {
|
2015-12-06 05:52:09 +08:00
|
|
|
lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
|
2016-02-15 20:36:02 +08:00
|
|
|
context->timeout_secs);
|
2016-05-19 15:28:31 +08:00
|
|
|
|
2018-01-14 20:09:41 +08:00
|
|
|
if (wsi->interpreting) {
|
2016-05-19 15:28:31 +08:00
|
|
|
args.p = (char *)p;
|
|
|
|
args.len = n;
|
2017-06-09 20:20:42 +08:00
|
|
|
args.max_len = (unsigned int)poss + 128;
|
2017-12-01 11:09:32 +08:00
|
|
|
args.final = wsi->http.filepos + n ==
|
|
|
|
wsi->http.filelen;
|
2018-01-14 20:09:41 +08:00
|
|
|
args.chunked = wsi->sending_chunked;
|
2016-05-19 15:28:31 +08:00
|
|
|
if (user_callback_handle_rxflow(
|
2017-10-28 07:42:44 +08:00
|
|
|
wsi->vhost->protocols[
|
|
|
|
(int)wsi->protocol_interpret_idx].callback,
|
|
|
|
wsi, LWS_CALLBACK_PROCESS_HTML,
|
2016-05-19 15:28:31 +08:00
|
|
|
wsi->user_space, &args, 0) < 0)
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it;
|
2016-05-19 15:28:31 +08:00
|
|
|
n = args.len;
|
|
|
|
p = (unsigned char *)args.p;
|
2016-12-12 13:36:25 +08:00
|
|
|
} else
|
2017-10-13 10:33:02 +08:00
|
|
|
p = pstart;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_RANGES)
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.range.send_ctr + 1 ==
|
|
|
|
wsi->http.range.count_ranges && // last range
|
|
|
|
wsi->http.range.count_ranges > 1 && // was 2+ ranges (ie, multipart)
|
|
|
|
wsi->http.range.budget - amount == 0) {// final part
|
2017-10-13 10:33:02 +08:00
|
|
|
n += lws_snprintf((char *)pstart + n, 6,
|
2016-12-12 13:36:25 +08:00
|
|
|
"_lws\x0d\x0a"); // append trailing boundary
|
|
|
|
lwsl_debug("added trailing boundary\n");
|
2016-05-19 15:28:31 +08:00
|
|
|
}
|
2016-12-12 13:36:25 +08:00
|
|
|
#endif
|
2016-05-19 15:28:31 +08:00
|
|
|
m = lws_write(wsi, p, n,
|
2018-01-14 20:09:41 +08:00
|
|
|
wsi->http.filepos + amount == wsi->http.filelen ?
|
2016-05-19 15:28:31 +08:00
|
|
|
LWS_WRITE_HTTP_FINAL :
|
|
|
|
LWS_WRITE_HTTP
|
|
|
|
);
|
2013-02-23 10:50:10 +08:00
|
|
|
if (m < 0)
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it;
|
2013-02-23 10:50:10 +08:00
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.filepos += amount;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
|
|
|
#if defined(LWS_WITH_RANGES)
|
2017-12-01 11:09:32 +08:00
|
|
|
if (wsi->http.range.count_ranges >= 1) {
|
|
|
|
wsi->http.range.budget -= amount;
|
|
|
|
if (wsi->http.range.budget == 0) {
|
2016-12-12 13:36:25 +08:00
|
|
|
lwsl_notice("range budget exhausted\n");
|
2017-12-01 11:09:32 +08:00
|
|
|
wsi->http.range.inside = 0;
|
|
|
|
wsi->http.range.send_ctr++;
|
2016-12-12 13:36:25 +08:00
|
|
|
|
2017-12-01 11:09:32 +08:00
|
|
|
if (lws_ranges_next(&wsi->http.range) < 1) {
|
2016-12-12 13:36:25 +08:00
|
|
|
finished = 1;
|
|
|
|
goto all_sent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2016-05-19 15:28:31 +08:00
|
|
|
if (m != n) {
|
2013-02-23 10:50:10 +08:00
|
|
|
/* adjust for what was not sent */
|
2017-12-01 11:09:32 +08:00
|
|
|
if (lws_vfs_file_seek_cur(wsi->http.fop_fd,
|
2015-12-10 12:50:10 +08:00
|
|
|
m - n) ==
|
2017-10-20 17:45:02 +08:00
|
|
|
(lws_fileofs_t)-1)
|
2017-03-03 12:38:10 +08:00
|
|
|
goto file_had_it;
|
2016-05-19 15:28:31 +08:00
|
|
|
}
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2017-10-13 10:33:02 +08:00
|
|
|
|
2013-12-09 14:16:17 +08:00
|
|
|
all_sent:
|
2017-12-01 11:09:32 +08:00
|
|
|
if ((!wsi->trunc_len && wsi->http.filepos >= wsi->http.filelen)
|
2016-12-12 13:36:25 +08:00
|
|
|
#if defined(LWS_WITH_RANGES)
|
|
|
|
|| finished)
|
|
|
|
#else
|
|
|
|
)
|
|
|
|
#endif
|
|
|
|
{
|
2018-04-02 11:55:17 +08:00
|
|
|
lwsi_set_state(wsi, LRS_ESTABLISHED);
|
2015-10-27 07:07:14 +08:00
|
|
|
/* we might be in keepalive, so close it off here */
|
2017-12-01 11:09:32 +08:00
|
|
|
lws_vfs_file_close(&wsi->http.fop_fd);
|
2016-07-23 14:18:25 +08:00
|
|
|
|
2016-08-22 07:07:10 +08:00
|
|
|
lwsl_debug("file completed\n");
|
2015-10-27 07:07:14 +08:00
|
|
|
|
2017-10-13 10:33:02 +08:00
|
|
|
if (wsi->protocol->callback &&
|
|
|
|
user_callback_handle_rxflow(wsi->protocol->callback,
|
|
|
|
wsi, LWS_CALLBACK_HTTP_FILE_COMPLETION,
|
|
|
|
wsi->user_space, NULL,
|
|
|
|
0) < 0) {
|
|
|
|
/*
|
|
|
|
* For http/1.x, the choices from
|
|
|
|
* transaction_completed are either
|
|
|
|
* 0 to use the connection for pipelined
|
|
|
|
* or nonzero to hang it up.
|
|
|
|
*
|
|
|
|
* However for http/2. while we are
|
|
|
|
* still interested in hanging up the
|
|
|
|
* nwsi if there was a network-level
|
|
|
|
* fatal error, simply completing the
|
|
|
|
* transaction is a matter of the stream
|
|
|
|
* state, not the root connection at the
|
|
|
|
* network level
|
|
|
|
*/
|
|
|
|
if (wsi->http2_substream)
|
|
|
|
return 1;
|
|
|
|
else
|
|
|
|
return -1;
|
|
|
|
}
|
2016-05-19 15:28:31 +08:00
|
|
|
|
2013-06-29 10:24:16 +08:00
|
|
|
return 1; /* >0 indicates completed */
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2018-03-27 09:17:19 +08:00
|
|
|
} while (0); // while (!lws_send_pipe_choked(wsi))
|
2013-01-22 07:20:08 +08:00
|
|
|
|
2015-12-16 18:19:08 +08:00
|
|
|
lws_callback_on_writable(wsi);
|
2013-01-22 07:20:08 +08:00
|
|
|
|
2013-06-29 10:24:16 +08:00
|
|
|
return 0; /* indicates further processing must be done */
|
2017-03-03 12:38:10 +08:00
|
|
|
|
|
|
|
file_had_it:
|
2017-12-01 11:09:32 +08:00
|
|
|
lws_vfs_file_close(&wsi->http.fop_fd);
|
2017-03-03 12:38:10 +08:00
|
|
|
|
|
|
|
return -1;
|
2013-01-22 07:20:08 +08:00
|
|
|
}
|
2014-04-06 06:26:35 +01:00
|
|
|
|
|
|
|
LWS_VISIBLE int
|
2015-12-15 21:15:58 +08:00
|
|
|
lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
2014-04-06 06:26:35 +01:00
|
|
|
{
|
2017-05-07 10:02:03 +08:00
|
|
|
struct lws_context *context = wsi->context;
|
|
|
|
struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
|
2018-04-12 15:56:38 +08:00
|
|
|
int n = 0;
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_C_API_READ, 1);
|
|
|
|
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
wsi->udp->salen = sizeof(wsi->udp->sa);
|
|
|
|
n = recvfrom(wsi->desc.sockfd, (char *)buf, len, 0,
|
|
|
|
&wsi->udp->sa, &wsi->udp->salen);
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
} else
|
|
|
|
n = recv(wsi->desc.sockfd, (char *)buf, len, 0);
|
|
|
|
|
2016-05-05 09:06:09 +08:00
|
|
|
if (n >= 0) {
|
2016-04-15 14:01:29 +08:00
|
|
|
if (wsi->vhost)
|
2016-12-12 13:36:25 +08:00
|
|
|
wsi->vhost->conn_stats.rx += n;
|
2017-05-07 10:02:03 +08:00
|
|
|
lws_stats_atomic_bump(context, pt, LWSSTATS_B_READ, n);
|
2016-07-15 13:41:38 +08:00
|
|
|
lws_restart_ws_ping_pong_timer(wsi);
|
2018-03-24 08:07:00 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return n;
|
2016-04-15 14:01:29 +08:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2015-10-15 21:21:06 +08:00
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR)
|
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2016-05-05 09:06:09 +08:00
|
|
|
lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
|
2014-04-06 06:26:35 +01:00
|
|
|
{
|
2015-11-08 12:10:26 +08:00
|
|
|
int n = 0;
|
2015-11-02 20:34:12 +08:00
|
|
|
|
2018-03-24 08:07:00 +08:00
|
|
|
if (lws_wsi_is_udp(wsi)) {
|
2018-04-12 15:56:38 +08:00
|
|
|
#if !defined(LWS_WITH_ESP32)
|
2018-03-24 08:07:00 +08:00
|
|
|
if (wsi->trunc_len)
|
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa_pending, wsi->udp->salen_pending);
|
|
|
|
else
|
|
|
|
n = sendto(wsi->desc.sockfd, buf, len, 0, &wsi->udp->sa, wsi->udp->salen);
|
2018-04-12 15:56:38 +08:00
|
|
|
#endif
|
2018-03-24 08:07:00 +08:00
|
|
|
} else
|
|
|
|
n = send(wsi->desc.sockfd, (char *)buf, len, MSG_NOSIGNAL);
|
2016-01-27 08:50:31 +08:00
|
|
|
// lwsl_info("%s: sent len %d result %d", __func__, len, n);
|
2014-04-10 14:08:10 +08:00
|
|
|
if (n >= 0)
|
|
|
|
return n;
|
|
|
|
|
|
|
|
if (LWS_ERRNO == LWS_EAGAIN ||
|
|
|
|
LWS_ERRNO == LWS_EWOULDBLOCK ||
|
|
|
|
LWS_ERRNO == LWS_EINTR) {
|
2017-02-18 17:26:40 +08:00
|
|
|
if (LWS_ERRNO == LWS_EWOULDBLOCK) {
|
2014-04-10 14:08:10 +08:00
|
|
|
lws_set_blocking_send(wsi);
|
2017-02-18 17:26:40 +08:00
|
|
|
}
|
2014-04-06 06:26:35 +01:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_MORE_SERVICE;
|
|
|
|
}
|
2015-12-14 08:52:03 +08:00
|
|
|
|
2017-09-23 12:55:21 +08:00
|
|
|
lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n",
|
|
|
|
len, wsi->desc.sockfd, n, LWS_ERRNO);
|
2018-03-11 11:26:06 +08:00
|
|
|
|
2014-04-10 14:08:10 +08:00
|
|
|
return LWS_SSL_CAPABLE_ERROR;
|
2014-04-06 06:26:35 +01:00
|
|
|
}
|
2018-04-11 13:39:42 +08:00
|
|
|
|
2015-08-19 16:23:33 +02:00
|
|
|
LWS_VISIBLE int
|
2015-12-04 11:08:32 +08:00
|
|
|
lws_ssl_pending_no_ssl(struct lws *wsi)
|
2015-08-19 16:23:33 +02:00
|
|
|
{
|
2015-11-02 13:10:33 +08:00
|
|
|
(void)wsi;
|
2017-05-07 08:19:55 +08:00
|
|
|
#if defined(LWS_WITH_ESP32)
|
|
|
|
return 100;
|
|
|
|
#else
|
2017-05-03 21:28:26 +08:00
|
|
|
return 0;
|
2017-05-07 08:19:55 +08:00
|
|
|
#endif
|
2015-08-19 16:23:33 +02:00
|
|
|
}
|