Commit graph

125 commits

Author SHA1 Message Date
Robert Lubos cd5e1cd0a5 net: lib: http_server: Fix possible NULL pointer dereference
Fix possible NULL pointer dereference in http_hpack_decode_header().

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-06-24 12:41:22 -04:00
Alberto Escolar Piedras f13196429e subsys/net/lib/http: Set feature macro as required
This file uses strnlen() but the C library
is not require to expose its prototype unless
_POSIX_C_SOURCE is defined.
So let's define it to avoid an implicit function
declaration warning.

Signed-off-by: Alberto Escolar Piedras <alberto.escolar.piedras@nordicsemi.no>
2024-06-24 13:01:38 +02:00
Henrik Maier abeae8fd08 net: lib: http_server: Fix dynamic HTTP post zero 0 length reply
Fix issue in http server that a http post
was unable to send a 0 length reply.

Signed-off-by: Henrik Maier <hwmaier@gmail.com>
2024-06-20 08:58:03 +02:00
frei tycho 382670690c net: change controlling expressions in while to Boolean
Use `do { ... } while (false)` instead of `do { ... } while (0)`.

Signed-off-by: frei tycho <tfrei@baumer.com>
2024-06-11 20:03:16 +03:00
Jukka Rissanen a124dd2596 net: http_server: Send chunked response correctly
The chunked response was not sent properly. There were extra
"\r\n" before the chunk lenght and the length of the string
to be sent was calculated incorrectly.

Fixes #72887

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-06-03 15:39:09 +02:00
Jukka Rissanen 08dc8f93fe net: http_server: Add wildcard support to resource strings
Allow user to specify resource string using wildcard characters
so that multiple URL paths can be served with just one handler.

Fixes #73367

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-05-31 09:57:22 -05:00
Jukka Rissanen 6d129b2a2d net: http_server: Add support for specifying Content-Type
Allow user to specify the Content-Type header field for the
HTTP response.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-05-14 20:22:58 -04:00
Fin Maaß 9bd0170aad net: http: client: mark as unstable
As the http client API is currently used by two applications
(websocket and hawkbit), it should be marked as unstable, according
to the docs:
https://docs.zephyrproject.org/latest/develop/api/api_lifecycle.html

Signed-off-by: Fin Maaß <f.maass@vogl-electronic.com>
2024-05-13 16:02:55 -04:00
Jukka Rissanen 3c9513e9cc net: http_server: Add websocket support
After upgrading the HTTP connection to websocket, call the
application registered callback to transfer the ownership of
the socket to the application.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-05-08 15:02:16 -05:00
Jukka Rissanen 535d4b40a8 net: lib: http_server: Upgrade from HTTP/1.1 to websocket
Allow connection to be upgraded from HTTP/1.1 to websocket.
This commit does nothing yet with the upgraded connection.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-05-08 15:02:16 -05:00
Jukka Rissanen 37956465e1 net: lib: http_server: Check HTTP/1.1 Upgrade field
Check what kind of upgrading we are doing and return error
if we receive upgrade that we do not support.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-05-08 15:02:16 -05:00
Jukka Rissanen 4b157b9099 net: lib: http_server: Initial HTTP server support
Original code developed as a GSoC 2023 project by Emna Rekik.

Code refactored in order to provide better bisectability
as the origical commits were not bisectable.

The server supports static and dynamic resources, managed by
HTTP_SERVICE/HTTP_RESOURCE macros.

Fixes #59685
Fixes #59686
Fixes #59688
Fixes #59690
Fixes #59670
Fixes #59700
Fixes #59684
Fixes #59693
Fixes #59693
Fixes #59694
Fixes #59699
Fixes #59696
Fixes #59688
Fixes #59690
Fixes #59670
Fixes #59700
Fixes #59685
Fixes #59686
Fixes #59688
Fixes #59691

Signed-off-by: Emna Rekik <emna.rekik007@gmail.com>
Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-04-30 13:25:22 +02:00
Robert Lubos 318dcb6336 net: lib: http_server: Add HPACK and Huffman code encoder/decoder
Add HTTP/2 helper libraries to encode and decode HPACK encoded headers,
according to RFC7541.

HPACK string encoding requires to support certain set of Huffman codes,
therefore implement Huffman encoder/decoder as well.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2024-04-30 13:25:22 +02:00
Georges Oates_Larsen 7fc6ef28d3 net: lib: http: Correct http_client edge-case behavior
Correct various small edge-case behaviors that have been accidentally
introduced in the http_client.

- http_client_req no longer incorrectly returns -ETIMEDOUT on NULL HTTP
  resonse. -ETIMEDOUT is now only returned when the underlying TLS
  socket times out.
- http_client_req now returns -ECONRESET upon incomplete (but non-NULL)
  HTTP response. The request callback is no longer called in this case
  (as with any other error state).
- http_wait_data has been refactored slightly to increase clarity.

Signed-off-by: Georges Oates_Larsen <georges.larsen@nordicsemi.no>
2024-04-05 13:35:22 -05:00
Jukka Rissanen 28a46c0f60 net: http: client: Return error if waiting timeout
Return error to the caller if no data was received or there
was some other error. Earlier we did not check the error
condition properly.

Signed-off-by: Jukka Rissanen <jukka.rissanen@nordicsemi.no>
2024-02-28 18:02:34 +00:00
Chris Friedt 79d4107beb net: http: define http service and resource iterable sections
Provide a means of declaring zero or more HTTP services, each
with zero or more static HTTP resources.

Static HTTP resources are those which have fixed paths[1] which
are known prior to system initialization. Some examples of
static http resources would be

* a forwarder from '/' to '/index.html'
* a REST endpoint with fixed path '/api/foo' and detail
  pointing at some implementation-specific function
* a Javascript file in string form with fixed path '/js/util.js'
* a 'construction' image with path '/res/work.png'
* a gzip-compressed 'Hello' HTML file at '/hello.html'

Without describing in any detail how static HTTP resources are
organized or served by any given HTTP server, we can describe
what static resources exist on a system in a common way that
does not require any optional facilities (e.g. filesystem) and
relies only on addressable memory.

Additionally, for the purposes of simply allowing others
to implement custom HTTP servers in a consistent way, or
benchmarking implementations, or having a consistent testsuite
to use across multiple implementations, it is helpful to have
a common method to declare HTTP services and static resources
for Zephyr.

[1] https://en.wikipedia.org/wiki/URL

Signed-off-by: Chris Friedt <cfriedt@meta.com>
2023-03-08 13:57:13 +00:00
Lucas Dietrich c7f9eafeec net: http: Make http_client.c compile with CONFIG_NET_SOCKETS_POSIX_NAMES=n
Use internal constant ZSOCK_POLLIN instead of POLLIN to
 make the http_client source file compile without error with
CONFIG_NET_SOCKETS_POSIX_NAMES disabled.

 Fixes #55423

Signed-off-by: Lucas Dietrich <ld.adecy@gmail.com>
2023-03-05 08:51:25 -05:00
Wouter Cappelle aaefd3db7e net: http: Use poll for http timeout instead of shutdown
Change the http timeout mechanism to use poll instead of shutdown.
This should fix a problem where the shutdown will be called in a
different thread context which can lead to deadlocks on certain
driver implementations like offloaded modem drivers.
Fixes #53967

Signed-off-by: Wouter Cappelle <wouter.cappelle@crodeon.com>
2023-01-24 14:50:11 +01:00
Christopher Friedt dbe2c0d59e include: net: http: rename http_x.h http/x.h
Some minor housekeeping prior to adding an http server
implementation. There are already a number of http headers
and that number will likely increase with subsequent work.
Moving them into a common directory cleans up the
`include/net` directory a bit.

Signed-off-by: Christopher Friedt <cfriedt@meta.com>
2022-10-12 09:02:21 -04:00
Krzysztof Chruscinski 041f0e5379 all: logging: Remove log_strdup function
Logging v1 has been removed and log_strdup wrapper function is no
longer needed. Removing the function and its use in the tree.

Signed-off-by: Krzysztof Chruscinski <krzysztof.chruscinski@nordicsemi.no>
2022-06-23 13:42:23 +02:00
Gerard Marull-Paretas 5113c1418d subsystems: migrate includes to <zephyr/...>
In order to bring consistency in-tree, migrate all subsystems code to
the new prefix <zephyr/...>. Note that the conversion has been scripted,
refer to zephyrproject-rtos#45388 for more details.

Signed-off-by: Gerard Marull-Paretas <gerard.marull@nordicsemi.no>
2022-05-09 12:07:35 +02:00
Sjors Hettinga 1165bd667f net: http: Allow a content_len of 4GB
Instead of maximally 99999 bytes allow a content_len of 4GB.

Signed-off-by: Sjors Hettinga <s.a.hettinga@gmail.com>
2022-03-21 10:16:21 +01:00
Nazar Kazakov f483b1bc4c everywhere: fix typos
Fix a lot of typos

Signed-off-by: Nazar Kazakov <nazar.kazakov.work@gmail.com>
2022-03-18 13:24:08 -04:00
Robert Lubos ffdc621f1b net: http_client: Fix http_client_req() return value
http_client_req() was supposed to return the number of bytes sent as a
HTTP request. The return value was not riht however due to some bugs in
helper functions:
* http_send_data() returned the current buffer position istead of the
  number of bytes actually sent. This could result in counting the same
  data into the total request size several times. A helper variable was
  added to track how many bytes were actually sent to the network.
* http_flush_data() forwarded the return value of sendall() helper
  function. That function however did not return number of bytes sent,
  but 0 or a negative error code.

Additionally, change the return type of sendall() function - according
to standard the ssize_t type is only capable of holding -1 negative
value, but the function could return the full range of negative errno
values. Use int instead.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2022-03-18 11:14:24 +01:00
Yong Cong Sin ce3e1e7e29 net: http: Calculate body_frag_len
Calculate the body_frag_length if any part of the body is found
in the http response.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2022-03-15 10:05:21 +01:00
Yong Cong Sin 49c2d8a635 net: http: Rename body_start to body_frag_start
Following #42026, the body_start pointer now points to the
start of the body fragment in the recv_buffer as long as there
is body in it, either entirely or partially.

Rename the body_start to body_frag_start to better reflect
what it represents.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2022-03-15 10:05:21 +01:00
Georges Oates_Larsen 303c42e70a net: http_client: Add official null response behavior
Adds an official behavior in response to null response from HTTP
endpoint.

Fixes #42988

Signed-off-by: Georges Oates_Larsen <georges.larsen@nordicsemi.no>
2022-03-07 09:18:50 -06:00
Marcin Niestroj ab2f616641 net: http: use shutdown(..., SHUT_RD) in receive timeout handler
So far close() was called on underlying socket when timeout has expired.
This is wrong in several ways. First of all POSIX specification of
close() does not specify what should happen on blocking recv() call and
different systems have different behaviors (e.g. Linux does not wake up
recv() caller if there was no new incoming data, while other systems
might wakeup recv() caller immediately). Another (and much more severe)
problem is that HTTP client user does not know whether underlying socket
was already closed or not after HTTP request has finished. As a result
it was not clear whether close() should be called by HTTP client user.

Use shutdown(..., SHUT_RD) in internal HTTP client implementation, so
that recv() is woken up immediately with 0 as result (which means EOF).
This will allow to gracefully handle timeouts and make it clear that it
is application responsibility to always call close() after HTTP
request (successful or not).

Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com>
2022-03-02 10:05:09 -08:00
Yong Cong Sin 731241f8d0 kernel: workq: Fix type errors in delayable work handlers
A common pattern here was to take the work item as the subfield of a
containing object. But the contained field is not a k_work, it's a
k_work_delayable.

Things were working only because the work field was first, so the
pointers had the same value. Do things right and fix things to
produce correct code if/when that field ever moves within delayable.

Signed-off-by: Yong Cong Sin <yongcong.sin@gmail.com>
2022-02-02 18:43:12 -05:00
Robert Lubos 4557d183b9 net: http_client: Set body_start pointer unconditionally
Set `body_start` pointer regardless of the body position in the recv
buffer. In result, the pointer shall indicate correctly position of the
body for each fragment, it's also explicit now that if the pointer is
not set for a fragment, there's no body in that particular fragment.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2022-01-21 11:31:49 -05:00
Torsten Rasmussen 36f5600387 kconfig: net: experimental settings now uses select EXPERIMENTAL
With the introduction of `EXPERIMENTAL` and `WARN_EXPERIMENTAL` in
Zephyr all subsys/net and drivers/ethernet/Kconfig.e1000 settings
having `[EXPERIMENTAL]` in their prompt has has been updated to include
`select EXPERIMENTAL` so that developers can enable warnings when
experimental features are enabled.

The following settings has EXPERIMENTAL removed as they are considered
mature:
- NET_OFFLOAD
- NET_PROMISCUOUS_MODE

Signed-off-by: Torsten Rasmussen <Torsten.Rasmussen@nordicsemi.no>
2021-10-25 10:46:48 +02:00
Robert Lubos be7faf7c08 net: http: Fix HTTP_DATA_FINAL notification
HTTP_DATA_FINAL was incorrectly notified in case Content Length field
was present in the HTTP respone - in such case it was set for every
response fragment, not only the last one.

Fix this by relying on `message_complete` flag instead of
`http_should_keep_alive()` function to determine whether to notify
HTTP_DATA_FINAL or not. As the HTTP parser calls the
`on_message_complete()` callback in either case (response is chunked or
not), this seems to be a more reasonable apporach to determine whether
the fragment is final or not.

Additinally, instead of calling response callback for
`on_body`/`on_message_complete` separately, call it directly from
`http_wait_data()` function, after the parsing round. This fixes the
case when headers were not reported correctly when the provided buffer
was smaller than the total headers length, resulting in corrupted data
being reported to the user.

Signed-off-by: Robert Lubos <robert.lubos@nordicsemi.no>
2021-09-17 08:12:00 -04:00
Benedikt Schmidt a8b7caa58d net: http: switch to zsock_ for http_client
Using zsock_ in http_client instead of the POSIX API versions of the
functions allows the usage of http_client in combination with
CONFIG_POSIX_API.

Signed-off-by: Benedikt Schmidt <benedikt.schmidt@embedded-solutions.at>
2021-09-09 21:16:28 -04:00
Justin Morton e3517e5080 net: http: update status if no status text is provided
Call on_status if the Reason-Phrase is not provided.
This allows for the numeric status code to be set.
Also, ensure the numeric status code is always set
in on_status, not just if the specific callback is set.

Signed-off-by: Justin Morton <justin.morton@nordicsemi.no>
2021-05-18 11:23:59 -05:00
Justin Morton 35f598ba93 net: http: add numeric http status code to response struct
Add numeric http status code to the response struct to allow for
easier processing by the caller.   Textual status already exists.

Signed-off-by: Justin Morton <justin.morton@nordicsemi.no>
2021-04-14 18:46:47 +03:00
Peter Bigot 188cb2cb7c net: Conversion of k_work API
Replace all existing deprecated API with the recommended alternative.

Signed-off-by: Peter Bigot <peter.bigot@nordicsemi.no>
Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2021-04-14 07:07:40 -04:00
Flavio Ceolin 9fd4ea91b7 coccinelle: Remove extra semicolon
coccicheck --mode=patch --cocci=semicolon.cocci

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2021-03-25 11:35:30 -05:00
Anas Nashif 5d1c535fc8 license: add missing SPDX headers
Add SPDX header to files with existing license.

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2021-02-11 08:05:16 -05:00
Jackie Ja fac4a6a4fe net: lib: http: chunked encoding body support
We should not ignore the body when the Transfer-Encoding is
chunked.

Signed-off-by: Jackie Ja <qazq.jackie@gmail.com>
2020-10-12 13:58:19 +03:00
Xavier Chapron 824f423e54 misc: Replace assert include and calls by sys/__assert.h equivalent
Replace all calls to the assert macro that comes from libc by calls to
__ASSERT_NO_MSG(). This is usefull as the former might be different
depending on the libc used and the later can be customized to reduce
flash footprint.

Signed-off-by: Xavier Chapron <xavier.chapron@stimio.fr>
2020-10-02 11:42:40 +02:00
Flavio Ceolin 0aaae4a039 guideline: Make explicit fallthrough cases
-Wimplicit-fallthrough=2 requires a fallthrough comment or a compiler
to tells gcc that this happens intentionally.

Signed-off-by: Flavio Ceolin <flavio.ceolin@intel.com>
2020-08-24 20:28:47 -04:00
Carles Cufi 244f826e3c cmake: remove _if_kconfig() functions
This set of functions seem to be there just because of historical
reasons, stemming from Kbuild. They are non-obvious and prone to errors,
so remove them in favor of the `_ifdef()` ones with an explicit
`CONFIG_` condition.

Script used:

git grep -l _if_kconfig | xargs sed -E -i
"s/_if_kconfig\(\s*(\w*)/_ifdef(CONFIG_\U\1\E \1/g"

Signed-off-by: Carles Cufi <carles.cufi@nordicsemi.no>
2020-08-01 12:35:20 +02:00
NavinSankar Velliangiri 820bfb46bc net: http: client: Add port number to HTTP Header
Add port number to HTTP Header.

Signed-off-by: NavinSankar Velliangiri <navin@linumiz.com>
2020-06-15 16:59:20 +02:00
Kumar Gala a1b77fd589 zephyr: replace zephyr integer types with C99 types
git grep -l 'u\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/u\(8\|16\|32\|64\)_t/uint\1_t/g"
	git grep -l 's\(8\|16\|32\|64\)_t' | \
		xargs sed -i "s/s\(8\|16\|32\|64\)_t/int\1_t/g"

Signed-off-by: Kumar Gala <kumar.gala@linaro.org>
2020-06-08 08:23:57 -05:00
Jukka Rissanen 65a1bebe85 net: Replace NET_WAIT_FOREVER by SYS_FOREVER_MS
As we now have SYS_FOREVER_MS, use that instead of network
specific NET_WAIT_FOREVER.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2020-05-05 16:29:23 +03:00
Arvin Farahmand 6d5f3debaa net: http_client: Fix payload issue on HTTP upload
Bug fix and improved `payload` handling in `http_client_req`.

Changes to `http_client_req` behaviour:

If the user provides `payload_len` it is used to generate the
`Content-Length` header. This is done even if `payload_cb` is used to
provide the actual data. If no `payload_len` is specified then no
`Content-Length` is generated.

If `payload_cb` is provided it is called to send the payload data.
Otherwise `payload` is used as the payload buffer and sent. If
`payload_len` is not zero, it is used as the size of `payload`.
Otherwise `payload` is assumed to be a string and `strlen` is used to
determine its size. This is to maintain current behaviour and not break
existing samples.

Fixes #24431

Signed-off-by: Arvin Farahmand <arvinf@ip-logix.com>
2020-04-21 00:05:04 +03:00
Jukka Rissanen 1b04d44247 net: http: Refactor because of timeout overhaul
Use k_timeout_t internally, no change to user API.
Clarify the documentation of the timeout parameter that it is
in milliseconds.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2020-04-09 16:07:03 +03:00
Ulf Magnusson bd6e04411e kconfig: Clean up header comments and make them consistent
Use this short header style in all Kconfig files:

    # <description>

    # <copyright>
    # <license>

    ...

Also change all <description>s from

    # Kconfig[.extension] - Foo-related options

to just

    # Foo-related options

It's clear enough that it's about Kconfig.

The <description> cleanup was done with this command, along with some
manual cleanup (big letter at the start, etc.)

    git ls-files '*Kconfig*' | \
        xargs sed -i -E '1 s/#\s*Kconfig[\w.-]*\s*-\s*/# /'

Signed-off-by: Ulf Magnusson <Ulf.Magnusson@nordicsemi.no>
2019-11-04 17:31:27 -05:00
Jukka Rissanen eb40499627 net: http: client: Initial version
Simple HTTP client API.

Signed-off-by: Jukka Rissanen <jukka.rissanen@linux.intel.com>
2019-10-04 16:38:34 +03:00
Anas Nashif f2cb20c772 docs: fix misspelling across the tree
Found a few annoying typos and figured I better run script and
fix anything it can find, here are the results...

Signed-off-by: Anas Nashif <anas.nashif@intel.com>
2019-06-19 15:34:13 -05:00