esp8266 initial support
[platform/upstream/libwebsockets.git] / lib / output.c
1 /*
2  * libwebsockets - small server side websockets and web server implementation
3  *
4  * Copyright (C) 2010-2015 Andy Green <andy@warmcat.com>
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
22 #include "private-libwebsockets.h"
23
24 static int
25 lws_0405_frame_mask_generate(struct lws *wsi)
26 {
27 #if 0
28         wsi->u.ws.mask[0] = 0;
29         wsi->u.ws.mask[1] = 0;
30         wsi->u.ws.mask[2] = 0;
31         wsi->u.ws.mask[3] = 0;
32 #else
33         int n;
34         /* fetch the per-frame nonce */
35
36         n = lws_get_random(lws_get_context(wsi), wsi->u.ws.mask, 4);
37         if (n != 4) {
38                 lwsl_parser("Unable to read from random device %s %d\n",
39                             SYSTEM_RANDOM_FILEPATH, n);
40                 return 1;
41         }
42 #endif
43         /* start masking from first byte of masking key buffer */
44         wsi->u.ws.mask_idx = 0;
45
46         return 0;
47 }
48
49 #ifdef _DEBUG
50
51 LWS_VISIBLE void lwsl_hexdump(void *vbuf, size_t len)
52 {
53         unsigned char *buf = (unsigned char *)vbuf;
54         unsigned int n, m, start;
55         char line[80];
56         char *p;
57
58         lwsl_parser("\n");
59
60         for (n = 0; n < len;) {
61                 start = n;
62                 p = line;
63
64                 p += sprintf(p, "%04X: ", start);
65
66                 for (m = 0; m < 16 && n < len; m++)
67                         p += sprintf(p, "%02X ", buf[n++]);
68                 while (m++ < 16)
69                         p += sprintf(p, "   ");
70
71                 p += sprintf(p, "   ");
72
73                 for (m = 0; m < 16 && (start + m) < len; m++) {
74                         if (buf[start + m] >= ' ' && buf[start + m] < 127)
75                                 *p++ = buf[start + m];
76                         else
77                                 *p++ = '.';
78                 }
79                 while (m++ < 16)
80                         *p++ = ' ';
81
82                 *p++ = '\n';
83                 *p = '\0';
84                 lwsl_debug("%s", line);
85         }
86         lwsl_debug("\n");
87 }
88
89 #endif
90
91 /*
92  * notice this returns number of bytes consumed, or -1
93  */
94
95 int lws_issue_raw(struct lws *wsi, unsigned char *buf, size_t len)
96 {
97         struct lws_context *context = lws_get_context(wsi);
98         size_t real_len = len;
99         unsigned int n;
100         int m;
101
102         if (!len)
103                 return 0;
104         /* just ignore sends after we cleared the truncation buffer */
105         if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE &&
106             !wsi->trunc_len)
107                 return len;
108
109         if (wsi->trunc_len && (buf < wsi->trunc_alloc ||
110             buf > (wsi->trunc_alloc + wsi->trunc_len + wsi->trunc_offset))) {
111                 char dump[20];
112                 strncpy(dump, (char *)buf, sizeof(dump) - 1);
113                 dump[sizeof(dump) - 1] = '\0';
114 #if defined(LWS_WITH_ESP8266)
115                 lwsl_err("****** %p: Sending new %d (%s), pending truncated ...\n",
116                          wsi, len, dump);
117 #else
118                 lwsl_err("****** %p: Sending new %d (%s), pending truncated ...\n"
119                          "       It's illegal to do an lws_write outside of\n"
120                          "       the writable callback: fix your code",
121                          wsi, len, dump);
122 #endif
123                 assert(0);
124
125                 return -1;
126         }
127
128         m = lws_ext_cb_active(wsi, LWS_EXT_CB_PACKET_TX_DO_SEND, &buf, len);
129         if (m < 0)
130                 return -1;
131         if (m) /* handled */ {
132                 n = m;
133                 goto handle_truncated_send;
134         }
135
136         if (!lws_socket_is_valid(wsi->sock))
137                 lwsl_warn("** error invalid sock but expected to send\n");
138
139         /* limit sending */
140         n = wsi->protocol->rx_buffer_size;
141         if (!n)
142                 n = context->pt_serv_buf_size;
143         n += LWS_PRE + 4;
144         if (n > len)
145                 n = len;
146 #if defined(LWS_WITH_ESP8266)   
147         if (wsi->pending_send_completion) {
148                 n = 0;
149                 goto handle_truncated_send;
150         }
151 #endif
152
153         /* nope, send it on the socket directly */
154         lws_latency_pre(context, wsi);
155         n = lws_ssl_capable_write(wsi, buf, n);
156         lws_latency(context, wsi, "send lws_issue_raw", n, n == len);
157
158         switch (n) {
159         case LWS_SSL_CAPABLE_ERROR:
160                 /* we're going to close, let close know sends aren't possible */
161                 wsi->socket_is_permanently_unusable = 1;
162                 return -1;
163         case LWS_SSL_CAPABLE_MORE_SERVICE:
164                 /* nothing got sent, not fatal, retry the whole thing later */
165                 n = 0;
166                 break;
167         }
168
169 handle_truncated_send:
170         /*
171          * we were already handling a truncated send?
172          */
173         if (wsi->trunc_len) {
174                 lwsl_info("%p partial adv %d (vs %d)\n", wsi, n, real_len);
175                 wsi->trunc_offset += n;
176                 wsi->trunc_len -= n;
177
178                 if (!wsi->trunc_len) {
179                         lwsl_info("***** %x partial send completed\n", wsi);
180                         /* done with it, but don't free it */
181                         n = real_len;
182                         if (wsi->state == LWSS_FLUSHING_STORED_SEND_BEFORE_CLOSE) {
183                                 lwsl_info("***** %x signalling to close now\n", wsi);
184                                 return -1; /* retry closing now */
185                         }
186                 }
187                 /* always callback on writeable */
188                 lws_callback_on_writable(wsi);
189
190                 return n;
191         }
192
193         if ((unsigned int)n == real_len)
194                 /* what we just sent went out cleanly */
195                 return n;
196
197         /*
198          * Newly truncated send.  Buffer the remainder (it will get
199          * first priority next time the socket is writable)
200          */
201         lwsl_notice("%p new partial sent %d from %d total\n", wsi, n, real_len);
202
203         /*
204          *  - if we still have a suitable malloc lying around, use it
205          *  - or, if too small, reallocate it
206          *  - or, if no buffer, create it
207          */
208         if (!wsi->trunc_alloc || real_len - n > wsi->trunc_alloc_len) {
209                 lws_free(wsi->trunc_alloc);
210
211                 wsi->trunc_alloc_len = real_len - n;
212                 wsi->trunc_alloc = lws_malloc(real_len - n);
213                 if (!wsi->trunc_alloc) {
214                         lwsl_err("truncated send: unable to malloc %d\n",
215                                  real_len - n);
216                         return -1;
217                 }
218         }
219         wsi->trunc_offset = 0;
220         wsi->trunc_len = real_len - n;
221         memcpy(wsi->trunc_alloc, buf + n, real_len - n);
222
223         /* since something buffered, force it to get another chance to send */
224         lws_callback_on_writable(wsi);
225
226         return real_len;
227 }
228
229 LWS_VISIBLE int lws_write(struct lws *wsi, unsigned char *buf, size_t len,
230                           enum lws_write_protocol wp)
231 {
232         struct lws_context_per_thread *pt = &wsi->context->pt[(int)wsi->tsi];
233         int masked7 = (wsi->mode == LWSCM_WS_CLIENT);
234         unsigned char is_masked_bit = 0;
235         unsigned char *dropmask = NULL;
236         struct lws_tokens eff_buf;
237         int pre = 0, n;
238         size_t orig_len = len;
239
240 #ifdef LWS_WITH_ACCESS_LOG
241         wsi->access_log.sent += len;
242 #endif
243         if (wsi->vhost)
244                 wsi->vhost->tx += len;
245
246         if (wsi->state == LWSS_ESTABLISHED && wsi->u.ws.tx_draining_ext) {
247                 /* remove us from the list */
248                 struct lws **w = &pt->tx_draining_ext_list;
249                 lwsl_debug("%s: TX EXT DRAINING: Remove from list\n", __func__);
250                 wsi->u.ws.tx_draining_ext = 0;
251                 /* remove us from context draining ext list */
252                 while (*w) {
253                         if (*w == wsi) {
254                                 *w = wsi->u.ws.tx_draining_ext_list;
255                                 break;
256                         }
257                         w = &((*w)->u.ws.tx_draining_ext_list);
258                 }
259                 wsi->u.ws.tx_draining_ext_list = NULL;
260                 wp = (wsi->u.ws.tx_draining_stashed_wp & 0xc0) |
261                                 LWS_WRITE_CONTINUATION;
262
263                 lwsl_ext("FORCED draining wp to 0x%02X\n", wp);
264         }
265
266         lws_restart_ws_ping_pong_timer(wsi);
267
268         if (wp == LWS_WRITE_HTTP ||
269             wp == LWS_WRITE_HTTP_FINAL ||
270             wp == LWS_WRITE_HTTP_HEADERS)
271                 goto send_raw;
272
273         /* if not in a state to send stuff, then just send nothing */
274
275         if (wsi->state != LWSS_ESTABLISHED &&
276             ((wsi->state != LWSS_RETURNED_CLOSE_ALREADY &&
277               wsi->state != LWSS_AWAITING_CLOSE_ACK) ||
278                             wp != LWS_WRITE_CLOSE))
279                 return 0;
280
281         /* if we are continuing a frame that already had its header done */
282
283         if (wsi->u.ws.inside_frame) {
284                 lwsl_debug("INSIDE FRAME\n");
285                 goto do_more_inside_frame;
286         }
287
288         wsi->u.ws.clean_buffer = 1;
289
290         /*
291          * give a chance to the extensions to modify payload
292          * the extension may decide to produce unlimited payload erratically
293          * (eg, compression extension), so we require only that if he produces
294          * something, it will be a complete fragment of the length known at
295          * the time (just the fragment length known), and if he has
296          * more we will come back next time he is writeable and allow him to
297          * produce more fragments until he's drained.
298          *
299          * This allows what is sent each time it is writeable to be limited to
300          * a size that can be sent without partial sends or blocking, allows
301          * interleaving of control frames and other connection service.
302          */
303         eff_buf.token = (char *)buf;
304         eff_buf.token_len = len;
305
306         switch ((int)wp) {
307         case LWS_WRITE_PING:
308         case LWS_WRITE_PONG:
309         case LWS_WRITE_CLOSE:
310                 break;
311         default:
312                 n = lws_ext_cb_active(wsi, LWS_EXT_CB_PAYLOAD_TX, &eff_buf, wp);
313                 if (n < 0)
314                         return -1;
315
316                 if (n && eff_buf.token_len) {
317                         /* extension requires further draining */
318                         wsi->u.ws.tx_draining_ext = 1;
319                         wsi->u.ws.tx_draining_ext_list = pt->tx_draining_ext_list;
320                         pt->tx_draining_ext_list = wsi;
321                         /* we must come back to do more */
322                         lws_callback_on_writable(wsi);
323                         /*
324                          * keep a copy of the write type for the overall
325                          * action that has provoked generation of these
326                          * fragments, so the last guy can use its FIN state.
327                          */
328                         wsi->u.ws.tx_draining_stashed_wp = wp;
329                         /* this is definitely not actually the last fragment
330                          * because the extension asserted he has more coming
331                          * So make sure this intermediate one doesn't go out
332                          * with a FIN.
333                          */
334                         wp |= LWS_WRITE_NO_FIN;
335                 }
336
337                 if (eff_buf.token_len && wsi->u.ws.stashed_write_pending) {
338                         wsi->u.ws.stashed_write_pending = 0;
339                         wp = (wp &0xc0) | (int)wsi->u.ws.stashed_write_type;
340                 }
341         }
342
343         /*
344          * an extension did something we need to keep... for example, if
345          * compression extension, it has already updated its state according
346          * to this being issued
347          */
348         if ((char *)buf != eff_buf.token) {
349                 /*
350                  * ext might eat it, but no have anything to issue yet
351                  * in that case we have to follow his lead, but stash and
352                  * replace the write type that was lost here the first time.
353                  */
354                 if (len && !eff_buf.token_len) {
355                         if (!wsi->u.ws.stashed_write_pending)
356                                 wsi->u.ws.stashed_write_type = (char)wp & 0x3f;
357                         wsi->u.ws.stashed_write_pending = 1;
358                         return len;
359                 }
360                 /*
361                  * extension recreated it:
362                  * need to buffer this if not all sent
363                  */
364                 wsi->u.ws.clean_buffer = 0;
365         }
366
367         buf = (unsigned char *)eff_buf.token;
368         len = eff_buf.token_len;
369
370         switch (wsi->ietf_spec_revision) {
371         case 13:
372                 if (masked7) {
373                         pre += 4;
374                         dropmask = &buf[0 - pre];
375                         is_masked_bit = 0x80;
376                 }
377
378                 switch (wp & 0xf) {
379                 case LWS_WRITE_TEXT:
380                         n = LWSWSOPC_TEXT_FRAME;
381                         break;
382                 case LWS_WRITE_BINARY:
383                         n = LWSWSOPC_BINARY_FRAME;
384                         break;
385                 case LWS_WRITE_CONTINUATION:
386                         n = LWSWSOPC_CONTINUATION;
387                         break;
388
389                 case LWS_WRITE_CLOSE:
390                         n = LWSWSOPC_CLOSE;
391                         break;
392                 case LWS_WRITE_PING:
393                         n = LWSWSOPC_PING;
394                         break;
395                 case LWS_WRITE_PONG:
396                         n = LWSWSOPC_PONG;
397                         break;
398                 default:
399                         lwsl_warn("lws_write: unknown write opc / wp\n");
400                         return -1;
401                 }
402
403                 if (!(wp & LWS_WRITE_NO_FIN))
404                         n |= 1 << 7;
405
406                 if (len < 126) {
407                         pre += 2;
408                         buf[-pre] = n;
409                         buf[-pre + 1] = (unsigned char)(len | is_masked_bit);
410                 } else {
411                         if (len < 65536) {
412                                 pre += 4;
413                                 buf[-pre] = n;
414                                 buf[-pre + 1] = 126 | is_masked_bit;
415                                 buf[-pre + 2] = (unsigned char)(len >> 8);
416                                 buf[-pre + 3] = (unsigned char)len;
417                         } else {
418                                 pre += 10;
419                                 buf[-pre] = n;
420                                 buf[-pre + 1] = 127 | is_masked_bit;
421 #if defined __LP64__
422                                         buf[-pre + 2] = (len >> 56) & 0x7f;
423                                         buf[-pre + 3] = len >> 48;
424                                         buf[-pre + 4] = len >> 40;
425                                         buf[-pre + 5] = len >> 32;
426 #else
427                                         buf[-pre + 2] = 0;
428                                         buf[-pre + 3] = 0;
429                                         buf[-pre + 4] = 0;
430                                         buf[-pre + 5] = 0;
431 #endif
432                                 buf[-pre + 6] = (unsigned char)(len >> 24);
433                                 buf[-pre + 7] = (unsigned char)(len >> 16);
434                                 buf[-pre + 8] = (unsigned char)(len >> 8);
435                                 buf[-pre + 9] = (unsigned char)len;
436                         }
437                 }
438                 break;
439         }
440
441 do_more_inside_frame:
442
443         /*
444          * Deal with masking if we are in client -> server direction and
445          * the wp demands it
446          */
447
448         if (masked7) {
449                 if (!wsi->u.ws.inside_frame)
450                         if (lws_0405_frame_mask_generate(wsi)) {
451                                 lwsl_err("frame mask generation failed\n");
452                                 return -1;
453                         }
454
455                 /*
456                  * in v7, just mask the payload
457                  */
458                 if (dropmask) { /* never set if already inside frame */
459                         for (n = 4; n < (int)len + 4; n++)
460                                 dropmask[n] = dropmask[n] ^ wsi->u.ws.mask[
461                                         (wsi->u.ws.mask_idx++) & 3];
462
463                         /* copy the frame nonce into place */
464                         memcpy(dropmask, wsi->u.ws.mask, 4);
465                 }
466         }
467
468 send_raw:
469         switch ((int)wp) {
470         case LWS_WRITE_CLOSE:
471 /*              lwsl_hexdump(&buf[-pre], len); */
472         case LWS_WRITE_HTTP:
473         case LWS_WRITE_HTTP_FINAL:
474         case LWS_WRITE_HTTP_HEADERS:
475         case LWS_WRITE_PONG:
476         case LWS_WRITE_PING:
477 #ifdef LWS_USE_HTTP2
478                 if (wsi->mode == LWSCM_HTTP2_SERVING) {
479                         unsigned char flags = 0;
480
481                         n = LWS_HTTP2_FRAME_TYPE_DATA;
482                         if (wp == LWS_WRITE_HTTP_HEADERS) {
483                                 n = LWS_HTTP2_FRAME_TYPE_HEADERS;
484                                 flags = LWS_HTTP2_FLAG_END_HEADERS;
485                                 if (wsi->u.http2.send_END_STREAM)
486                                         flags |= LWS_HTTP2_FLAG_END_STREAM;
487                         }
488
489                         if ((wp == LWS_WRITE_HTTP ||
490                              wp == LWS_WRITE_HTTP_FINAL) &&
491                             wsi->u.http.content_length) {
492                                 wsi->u.http.content_remain -= len;
493                                 lwsl_info("%s: content_remain = %lu\n", __func__,
494                                           wsi->u.http.content_remain);
495                                 if (!wsi->u.http.content_remain) {
496                                         lwsl_info("%s: selecting final write mode\n", __func__);
497                                         wp = LWS_WRITE_HTTP_FINAL;
498                                 }
499                         }
500
501                         if (wp == LWS_WRITE_HTTP_FINAL && wsi->u.http2.END_STREAM) {
502                                 lwsl_info("%s: setting END_STREAM\n", __func__);
503                                 flags |= LWS_HTTP2_FLAG_END_STREAM;
504                         }
505
506                         return lws_http2_frame_write(wsi, n, flags,
507                                         wsi->u.http2.my_stream_id, len, buf);
508                 }
509 #endif
510                 return lws_issue_raw(wsi, (unsigned char *)buf - pre, len + pre);
511         default:
512                 break;
513         }
514
515         /*
516          * give any active extensions a chance to munge the buffer
517          * before send.  We pass in a pointer to an lws_tokens struct
518          * prepared with the default buffer and content length that's in
519          * there.  Rather than rewrite the default buffer, extensions
520          * that expect to grow the buffer can adapt .token to
521          * point to their own per-connection buffer in the extension
522          * user allocation.  By default with no extensions or no
523          * extension callback handling, just the normal input buffer is
524          * used then so it is efficient.
525          *
526          * callback returns 1 in case it wants to spill more buffers
527          *
528          * This takes care of holding the buffer if send is incomplete, ie,
529          * if wsi->u.ws.clean_buffer is 0 (meaning an extension meddled with
530          * the buffer).  If wsi->u.ws.clean_buffer is 1, it will instead
531          * return to the user code how much OF THE USER BUFFER was consumed.
532          */
533
534         n = lws_issue_raw_ext_access(wsi, buf - pre, len + pre);
535         wsi->u.ws.inside_frame = 1;
536         if (n <= 0)
537                 return n;
538
539         if (n == (int)len + pre) {
540                 /* everything in the buffer was handled (or rebuffered...) */
541                 wsi->u.ws.inside_frame = 0;
542                 return orig_len;
543         }
544
545         /*
546          * it is how many bytes of user buffer got sent... may be < orig_len
547          * in which case callback when writable has already been arranged
548          * and user code can call lws_write() again with the rest
549          * later.
550          */
551
552         return n - pre;
553 }
554
555 LWS_VISIBLE int lws_serve_http_file_fragment(struct lws *wsi)
556 {
557         struct lws_context *context = wsi->context;
558         struct lws_context_per_thread *pt = &context->pt[(int)wsi->tsi];
559         struct lws_process_html_args args;
560         unsigned long amount, poss;
561         unsigned char *p = pt->serv_buf;
562         int n, m;
563         
564         // lwsl_notice("%s (trunc len %d)\n", __func__, wsi->trunc_len);
565
566         while (wsi->http2_substream || !lws_send_pipe_choked(wsi)) {
567                 if (wsi->trunc_len) {
568                         if (lws_issue_raw(wsi, wsi->trunc_alloc +
569                                           wsi->trunc_offset,
570                                           wsi->trunc_len) < 0) {
571                                 lwsl_info("%s: closing\n", __func__);
572                                 return -1;
573                         }
574                         continue;
575                 }
576
577                 if (wsi->u.http.filepos == wsi->u.http.filelen)
578                         goto all_sent;
579
580                 poss = context->pt_serv_buf_size;
581
582                 if (wsi->sending_chunked) {
583                         /* we need to drop the chunk size in here */
584                         p += 10;
585                         /* allow for the chunk to grow by 128 in translation */
586                         poss -= 10 + 128;
587                 }
588
589                 if (lws_plat_file_read(wsi, wsi->u.http.fd, &amount, p, poss) < 0)
590                         return -1; /* caller will close */
591                 
592                 //lwsl_notice("amount %ld\n", amount);
593
594                 n = (int)amount;
595                 if (n) {
596                         lws_set_timeout(wsi, PENDING_TIMEOUT_HTTP_CONTENT,
597                                         context->timeout_secs);
598
599                         if (wsi->sending_chunked) {
600                                 args.p = (char *)p;
601                                 args.len = n;
602                                 args.max_len = poss + 128;
603                                 args.final = wsi->u.http.filepos + n ==
604                                              wsi->u.http.filelen;
605                                 if (user_callback_handle_rxflow(
606                                      wsi->vhost->protocols[(int)wsi->protocol_interpret_idx].callback, wsi,
607                                      LWS_CALLBACK_PROCESS_HTML,
608                                      wsi->user_space, &args, 0) < 0)
609                                         return -1;
610                                 n = args.len;
611                                 p = (unsigned char *)args.p;
612                         }
613
614                         m = lws_write(wsi, p, n,
615                                       wsi->u.http.filepos == wsi->u.http.filelen ?
616                                         LWS_WRITE_HTTP_FINAL :
617                                         LWS_WRITE_HTTP
618                                 );
619                         if (m < 0)
620                                 return -1;
621
622                         wsi->u.http.filepos += amount;
623                         if (m != n) {
624                                 /* adjust for what was not sent */
625                                 if (lws_plat_file_seek_cur(wsi, wsi->u.http.fd,
626                                                            m - n) ==
627                                                              (unsigned long)-1)
628                                         return -1;
629                         }
630                 }
631 all_sent:
632                 if (!wsi->trunc_len &&
633                     wsi->u.http.filepos == wsi->u.http.filelen) {
634                         wsi->state = LWSS_HTTP;
635                         /* we might be in keepalive, so close it off here */
636                         lws_plat_file_close(wsi, wsi->u.http.fd);
637                         wsi->u.http.fd = LWS_INVALID_FILE;
638                         
639                         lwsl_notice("file completed\n");
640
641                         if (wsi->protocol->callback)
642                                 /* ignore callback returned value */
643                                 if (user_callback_handle_rxflow(
644                                      wsi->protocol->callback, wsi,
645                                      LWS_CALLBACK_HTTP_FILE_COMPLETION,
646                                      wsi->user_space, NULL, 0) < 0)
647                                         return -1;
648
649                         return 1;  /* >0 indicates completed */
650                 }
651         }
652
653         lws_callback_on_writable(wsi);
654
655         return 0; /* indicates further processing must be done */
656 }
657
658 #if LWS_POSIX
659 LWS_VISIBLE int
660 lws_ssl_capable_read_no_ssl(struct lws *wsi, unsigned char *buf, int len)
661 {
662         int n;
663
664         n = recv(wsi->sock, (char *)buf, len, 0);
665         if (n >= 0) {
666                 if (wsi->vhost)
667                         wsi->vhost->rx += n;
668                 lws_restart_ws_ping_pong_timer(wsi);
669                 return n;
670         }
671 #if LWS_POSIX
672         if (LWS_ERRNO == LWS_EAGAIN ||
673             LWS_ERRNO == LWS_EWOULDBLOCK ||
674             LWS_ERRNO == LWS_EINTR)
675                 return LWS_SSL_CAPABLE_MORE_SERVICE;
676 #endif
677         lwsl_notice("error on reading from skt : %d\n", LWS_ERRNO);
678         return LWS_SSL_CAPABLE_ERROR;
679 }
680
681 LWS_VISIBLE int
682 lws_ssl_capable_write_no_ssl(struct lws *wsi, unsigned char *buf, int len)
683 {
684         int n = 0;
685
686 #if LWS_POSIX
687         n = send(wsi->sock, (char *)buf, len, MSG_NOSIGNAL);
688 //      lwsl_info("%s: sent len %d result %d", __func__, len, n);
689         if (n >= 0)
690                 return n;
691
692         if (LWS_ERRNO == LWS_EAGAIN ||
693             LWS_ERRNO == LWS_EWOULDBLOCK ||
694             LWS_ERRNO == LWS_EINTR) {
695                 if (LWS_ERRNO == LWS_EWOULDBLOCK)
696                         lws_set_blocking_send(wsi);
697
698                 return LWS_SSL_CAPABLE_MORE_SERVICE;
699         }
700 #else
701         (void)n;
702         (void)wsi;
703         (void)buf;
704         (void)len;
705         // !!!
706 #endif
707
708         lwsl_debug("ERROR writing len %d to skt fd %d err %d / errno %d\n", len, wsi->sock, n, LWS_ERRNO);
709         return LWS_SSL_CAPABLE_ERROR;
710 }
711 #endif
712 LWS_VISIBLE int
713 lws_ssl_pending_no_ssl(struct lws *wsi)
714 {
715         (void)wsi;
716         return 0;
717 }