Commit graph

111 commits

Author SHA1 Message Date
Andy Green
2721e3ca9d qualify server specific api with LWS_NO_SERVER
Signed-off-by: Andy Green <andy.green@linaro.org>
2015-10-21 09:23:21 +08:00
Andy Green
2627148523 keepalive fix flow now forced closed removed
Since 0d89f3cbed added recently,
http1.1 keepalive tries to actually keep alive.

Some updates are needed to keepalive flow to solve problems coming from
changes that were hidden until now by keepalive basically closing until
recently.  It's not very noticable since clients will retry as close is
the default 1.0 behaviour... anyway this lets me do

wget http://localhost:7681/test.html http://localhost:7681/test.html

using keepalive correctly on the test server.

Signed-off-by: Andy Green <andy.green@linaro.org>
2015-10-21 08:16:34 +08:00
Christian Schüldt
3a01cc896d ssl renew timeout while post keeps coming
Signed-off-by: Christian Schüldt <crilla@kth.se>
2014-10-09 16:58:24 +08:00
Andy Green
095d303b49 http2 fix build when http2 disabled
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-10-08 12:15:29 +08:00
Andy Green
024eb6c80c http2 can keep upgraded connection up
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-10-08 12:00:53 +08:00
Carl Stehle
66d466a1a4 http keepalive needs to reinit some header union state
AG moved the code to the keepalive loop location
2014-08-16 10:17:59 +08:00
Andrew Canaday
e917345e05 Reset hdr_parsing_completed for WSI_STATE_HTTP:
This fixes a bug where a client issues two GET requests on the same connection
(keep-alive). If the second request is split into two reads, the
hdr_parsing_complete flag gets us into trouble by ending the request read two
early and giving us bogus data.
2014-08-07 12:13:28 +08:00
Andrew Canaday
78228ed5cb Keep-alive: disable timeout and let TCP ka kill.
This is required for streaming HTTP connections.
Not sure if there's a more graceful way to handle this
(maybe resetting the timeout in the 'send'-side code?
2014-07-19 07:01:10 +08:00
Andrew Canaday
c7dbad66ab Make sure headers are parsed before proceeding. 2014-07-19 07:00:52 +08:00
Andrew Canaday
3bb0650e6a Don't need 'content_remain' in libwebsocket_read 2014-07-14 20:33:07 +08:00
Andrew Canaday
afe26cf4a6 HTTP Version, Keep-alive support, No-copy POST
This is a squashed commit from https://github.com/andrew-canaday/libwebsockets,
dev/http_keepalive branch (strategies changed a few times, so the commit
history is clutteread). This branch is submitted for clarity, but the other
can be used as a reference or alternative.

 * added **enum http_version** to track HTTP/1.0 vs HTTP/1.1 requests
 * added **enum http_connection_type** to track keep-alive vs close
 * replaced content_length_seen and body_index with **content_remain**
 * removed **post_buffer** (see handshake.c modifications)

 * removed post_buffer free

 * switch state to WSI_TOKEN_SKIPPING after URI is complete to store version
 * delete *spill* label (unused)

 * add vars to track HTTP version and connection type
 * HTTP version defaults to 1.0
 * connection type defaults to 'close' for 1.0, keep-alive for 1.1
 * additional checks in **cleanup:** label:
   * if HTTP version string is present and valid, set enum val appropriately
   * override connection default with the "Connection:" header, if present
 * set state to WSI_STATE_HTTP_BODY if content_length > 0
 * return 0 on HTTP requests, unless LWS_CALLBACK_HTTP indicates otherwise

 * add vars to track remaining content_length and body chunk size
 * re-arrange switch case order to facilitate creation of jump-table
 * added new labels:
   * **read_ok**: normal location reach on break from switch; just return 0
   * **http_complete**: check for keep-alive + init state, mode, hdr table
   * **http_new**: jump location for keep-alive when http_complete sees len>0
 * after libwebsocket_parse, jump to one of those labels based on state
 * POST body handling:
   * don't bother iterating over input byte-by-byte or using memcpy
   * just pass the relevant portion of the context->service_buffer to callback
2014-07-14 20:19:43 +08:00
Andrew Canaday
7c67634fec libwebsockets_read: update 'len' after lws_handshake_server invocation 2014-07-08 06:15:25 +08:00
Andy Green
d7340c141f clean=various ifdef reduction
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-04-10 14:08:10 +08:00
Andy Green
aad2eac48e refactor handshake client and server handling into client.c and server.c
Eliminate more #ifdefs

Signed-off-by: Andy Green <andy.green@linaro.org>
2014-04-03 09:03:37 +08:00
Andy Green
564056d27c nonzero return from client parser is close
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-03-17 18:40:08 +08:00
Andy Green
5ac7e7ad5a client allow user callback to close on nonzero return
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-03-15 09:32:40 +08:00
Patrick Gansterer
ce8079c09b Add explicit cast to setsockopt() calls
Implicit cast from size_t* to const char* throws an error on some compilers.
2014-02-28 08:45:19 +08:00
Patrick Gansterer
81338aa886 Use native file functions on Windows
Add a special implementation with CreateFile(), ReadFile() and CloseFile()
for serving HTTP file request to allow compilation on all Windows platforms.
2014-02-27 21:20:36 +08:00
Patrick Gansterer
3ef96e8095 Rename leave to cleanup
The name ‘leave’ can not be used on some Windows CE platforms, so rename it.
2014-02-27 21:16:00 +08:00
Craig McQueen
e049a77775 Call libwebsocket_set_timeout() before callback LWS_CALLBACK_HTTP.
This allows the LWS_CALLBACK_HTTP callback to override the timeout
with libwebsocket_set_timeout() if it wants.
2014-02-20 07:51:10 +08:00
Andy Green
176de27df6 add timeout between accept and negotiation
Signed-off-by: Andy Green <andy.green@linaro.org>
2014-02-15 14:36:02 +08:00
John Clark
9bdcf18e74 fix LWS_NO_SERVER compile
Signed-off-by: John Clark <inidev@gmail.com>
2014-02-10 07:24:29 +08:00
Andy Green
aa08492021 solve broken partial file sends
We can't force the wsi state to HTTP_BODY without considering the callback
may already have set the state to sending a file.

This fixes the bug that we can get stalled in the test app at
"choked before able to send whole file"

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-12-09 11:27:07 +08:00
Andy Green
ca15338545 interpret zero rx buffer size as default in http
Reported by pystub

https://github.com/warmcat/libwebsockets/pull/33#issuecomment-29578258

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-12-08 21:26:52 +08:00
kapejod
ce64fb0f58 Support for HTTP POST.
Rewritten by Andy Green to use chunks, add timeout, use extra states in handshake not parser.

Signed-off-by: Andy Green <andy@warmcat.com>
2013-11-24 10:36:37 +08:00
Andy Green
4e7a13314d real http status codes update attack.sh
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-11-11 07:30:33 +08:00
Andy Green
19895bcfd4 introduce LWS_CALLBACK_FILTER_HTTP_CONNECTION
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-11-09 11:59:56 +08:00
Andy Green
84fd949e05 make ah available in http callback properly
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-11-09 11:40:32 +08:00
Andy Green
96d48fdc28 change LWS_CALLBACK_FILTER_PROTOCOL_CONNECTION user param usage
Also audit the bail_nuke_ah usage as Daniel Griscom suggested.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-09-18 08:32:55 +08:00
Andy Green
7cf6cb00c2 trac 28 - differentiate between http serving and accepted http connection
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-05-19 14:04:10 +08:00
Peter Pentchev
9a4fef7ed6 Hide private library symbols
Signed-off-by: Peter Pentchev <roam@ringlet.net>
2013-03-30 09:52:21 +08:00
Andy Green
01b206e804 SOL_SNDBUF set to protocol rx size
This tells the OS to reserve a TX buffer at least the size of the biggest RX frame
expected, for both server and client connections.

In Linux, the OS reserves 2 x the requested amount.

This is aimed at reducing the partial large atomic frame send problem to the point
it's only coming at large atomic frames the OS balks at reserving the size for.

If you have a lot of data to send, it is better to split it into multiple writes,
and use the FIN / CONTINUATION websocket stuff to manage it.

See the "fraggle" test app for example code of how to do that.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-03-23 09:53:17 +08:00
Andy Green
ca0a129065 rxflow remove recursion and simplify
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-03-16 11:24:23 +08:00
Andy Green
38c570c6d2 dont leak when protocol agreement probs
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-03-09 11:52:18 +08:00
Andy Green
2af4d5b2e2 remove lws_ensure_user_space from public api change return
The function has a logical problem when the size of the requested
allocation is 0, it will return NULL which is overloaded as
failure.

Actually the whole function is evil as an api, this patch moves
it out of the public API space and fixes it to return 0 for
success or 1 for fail.  Private code does not need to to return
wsi->user_space and public code should only get that from the
callback as discussed on trac recently.

Thanks to Edwin for debugging the problem.

Reported-by: Edwin van den Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-18 16:30:10 +08:00
Andy Green
0caf9c5acb handle http union transition properly
There's no proper transition to http union state until now.

It only becomes apparant there's a problem when you try to
return -1 from the HTTP callback, during the close action
it will try to close() a nonsense, nonzero fd pointer in
the uninitialized u.http union member.

This patch takes a copy of the allocated headers struct from
the u.hdr union state, transitions to u.http clearing down u
and then calls the HTTP callback with URI args pointing to
the still-in-scope ah allocation.  After the call, the copy ah
is freed.

That makes sure we are in the correct union state while still
giving the HTTP callback access to the URI without having to
copy it around.

Reported-by: Edwin can den Oetelaar <oetelaar.automatisering@gmail.com>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-18 09:48:31 +08:00
Andy Green
e803c82044 create user alloc for http if requested
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-14 23:18:10 +08:00
Andy Green
3ee9b31054 security harden http parser a bit
Drop the connection during parsing for a few more cases that can't be legit.

Take care about trying to free rxflow_buffer only if we reached a connmode
where it exists

Change behaviour on setting unknown HTTP method to kill connection

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-12 12:55:40 +08:00
Andy Green
b5b23199f6 style cleanup
This brings the library sources into compliance with checkpatch
style except for three or four exceptions like WIN32 related stuff
and one long string constant I don't want to break into multiple
sprintf calls.

There should be no functional or compilability change from all
this (hopefully).

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 17:13:32 +08:00
Andy Green
4708a02f06 valgrind drop header allocation down http path
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:27:44 +08:00
Andy Green
16ab3185c4 replace per header mallocs with single malloc 3 level struct
This big patch replaces the malloc / realloc per header
approach used until now with a single three-level struct
that gets malloc'd during the header union phase and freed
in one go when we transition to a different union phase.

It's more expensive in that we malloc a bit more than 4Kbytes,
but it's a lot cheaper in terms of malloc, frees, heap fragmentation,
no reallocs, nothing to configure.  It also moves from arrays of
pointers (8 bytes on x86_64) to unsigned short offsets into the
data array, (2 bytes on all platforms).

The 3-level thing is all in one struct

 - array indexed by the header enum, pointing to first "fragment" index
	(ie, header type to fragment lookup, or 0 for none)

 - array of fragments indexes, enough for 2 x the number of known headers
	(fragment array... note that fragments can point to a "next"
	fragment if the same header is spread across multiple entries)

 - linear char array where the known header payload gets written
	(fragments point into null-terminated strings stored in here,
	only the known header content is stored)

http headers can legally be split over multiple headers of the same
name which should be concatenated.  This scheme does not linearly
conatenate them but uses a linked list in the fragment structs to
link them.  There are apis to get the total length and copy out a
linear, concatenated version to a buffer.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-11 11:10:57 +08:00
Andy Green
0480f6420c handshake bail3 should be bail
Reported-by: Jack Mitchell <ml@communistcode.co.uk>
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 20:10:03 +08:00
Andy Green
5449511d3e remove fixed rx buffer allow definition per protocol
A new protocol member is defined that controls the size of rx
buffer allocation per connection.  For compatibility 0 size
allocates 4096, but you should adapt your protocol definition
array in the user code to declare an appropriate value.

See the changelog for more detail.

The advantage is the rx frame buffer size is now tailored to
what is expected from the protocol, rather than being fixed
to a default of 4096.  If your protocol only sends frames of
a dozen bytes this allows you to only allocate an rx frame
buffer of the same size.

For example the per-connection allocation (excluding headers)
for the test server fell from ~4500 to < 750 bytes with this.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-08 13:16:07 +08:00
Andy Green
68a672bb44 unionize header token array
This reduces the size of struct libwebscocket from 4840 to 4552
on x86_64

There are also big benefits on malloc pool fragmentation and
allocation, the header allocations only exist between the first
peer communication and websocket connection establishment for
both server and client.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Andy Green
2b57a34677 headers deleted after websocket established
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Andy Green
3455e676d8 act on fatal parse problems
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-02-07 20:30:56 +08:00
Joakim Soderberg
4c53123677 CMake support + fixed windows build.
- Finalized CMake support (tested on windows only so far).
  - Uses a generated lws_config.h that is included in
  private-libwebsocket to pass defines, only used if CMAKE_BUILD is set.
  - Support for SSL on Windows.
  - Initial support for CyaSSL replacement of OpenSSL (This has been added
    to my older CMake-fork but haven't been tested on this version yet).
- Fixed windows build (see below for details).
- Fixed at least the 32-bit Debug build for the existing Visual Studio
  Project. (Not to keen fixing all the others when we have CMake support
  anyway (which can generate much better project files)...)
- BUGFIXES:
  - handshake.c
    - used C99 definition of handshake_0405 function
  - libwebsocket.c
    - syslog not available on windows, put in ifdefs.
    - Fixed previous known crash bug on Windows where WSAPoll in
      Ws2_32.dll would not be present, causing the poll function pointer
      being set to NULL.
    - Uninitialized variable context->listen_service_extraseen would
      result in stack overflow because of infinite recursion. Fixed by
      initializing in libwebsocket_create_context
    - SO_REUSADDR means something different on Windows compared to Unix.
    - Setting a socket to nonblocking is done differently on Windows.
      (This should probably broken out into a helper function instead)
    - lwsl_emit_syslog -> lwsl_emit_stderr on Windows.
  - private-libwebsocket.h
    - PATH_MAX is not available on Windows, define as MAX_PATH
    - Always define LWS_NO_DAEMONIZE on windows.
    - Don't define lws_latency as inline that does nothing. inline is not
      support by the Microsoft compiler, replaced with an empty define
      instead. (It's __inline in MSVC)
  - server.c
    - Fixed nonblock call on windows
  - test-ping.c
    - Don't use C99 features (Microsoft compiler does not support it).
    - Move non-win32 headers into ifdefs.
    - Skip use of sighandler on Windows.
  - test-server.c
    - ifdef syslog parts on Windows.
2013-02-06 15:49:12 +09:00
Andy Green
5fc75a9b6b server allow NULL protocol map to protocol 0
Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-30 12:26:14 +08:00
Andy Green
623a98dab0 unionize mutually exclusive wsi members
Large chunks of struct libwebsocket members actually have a mutually
exclusive lifecycle, eg, once the http headers are finished they sit
there unused until the instance is destroyed.

This makes a big improvement in memory efficiency by making four
categories of member: always needed, needed for header processing,
needed for http processing, and needed for ws processing.  The last
three are mutually exclusive and bound into a union inside the wsi.

Care needs taking now at "union transitions", although we zeroed down
the struct at init, the other union siblings have been writing the
same memory by the time later member siblings start to use it.  So
it must be cleared down appropriately when we cross from one
mutually-exclusive use to another.

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-21 11:06:34 +08:00
Andy Green
5738c0e838 remove all support for pre v13 protocols
Since v13 was defined as the released ietf version the older versions
are deprecated.  This patch strips out everything to do with the older
versions and gets rid of the option to send stuff unmasked.

The in-tree md5 implementation is then also deleted as nothing needs
it any more, 1280 loc are shed in all

Signed-off-by: Andy Green <andy.green@linaro.org>
2013-01-21 09:53:35 +08:00