HTTP server, HTTP client, socket functionality.
More...
|
#define | HTTP_HEADER_STRING(n, v) { .type = HTTP_HEADER_STRING, .name = (n), .val.s = (v), } |
| define HTTP header name with string value More...
|
|
#define | HTTP_HEADER_INT(n, v) { .type = HTTP_HEADER_INT, .name = (n), .val.i = (v), } |
| define HTTP header name with integer value More...
|
|
#define | HTTP_ERROR(x) ((x) != HTTP_SUCCESS) |
| return true if HTTP error occurred More...
|
|
#define | IN_LOOPBACK(a) ((((in_addr_t)(a)) & 0x7f000000) == 0x7f000000) |
| expression is true, if address from loopback networks More...
|
|
|
enum | http_method {
HTTP_OPTIONS,
HTTP_GET,
HTTP_HEAD,
HTTP_POST,
HTTP_PUT,
HTTP_DELETE,
HTTP_TRACE,
HTTP_CONNECT
} |
| HTTP methods, see rfc2616. More...
|
|
enum | http_status {
HTTP_SUCCESS = 0,
HTTP_CONNECTION_CLOSED,
HTTP_SOCKET_ERROR,
HTTP_LARGE_REQUEST,
HTTP_INVALID_REQUEST,
HTTP_UNKNOWN_REQUEST,
HTTP_INVALID_HEADER,
HTTP_INVALID_REPLY,
HTTP_ABORTED,
HTTP_OUT_MEMORY
} |
| HTTP status code. More...
|
|
enum | http_header_type { HTTP_HEADER_STRING,
HTTP_HEADER_INT
} |
| HTTP header values type. More...
|
|
|
struct in_addr * | gethostaddr (const char *hostname, struct in_addr *addr) |
| resolve hostname or ip address into in_addr More...
|
|
http_status_t | http_request_send (int sockfd, http_method_t method, const char *path, const char *ver, http_header_t hdr[], size_t count) |
| send HTTP request by socket More...
|
|
http_status_t | http_request_recv (int sockfd, http_request_cb_t request_cb, http_header_cb_t header_cb, void *priv) |
| read HTTP request from socket and parse it More...
|
|
http_status_t | http_reply_send (int sockfd, const char *ver, int code, const char *desc, http_header_t hdr[], size_t count) |
| send HTTP reply to socket More...
|
|
http_status_t | http_reply_recv (int sockfd, http_reply_cb_t reply_cb, http_header_cb_t header_cb, void *priv) |
| read HTTP reply from socket and parse it More...
|
|
int | iface_list (void(*iface_cb)(struct iface *, void *), void *priv) |
| list IPv4 network interfaces More...
|
|
ssize_t | recvall (int sockfd, void *buf, size_t len, int flags) |
| read buffer from socket descriptor More...
|
|
int | recvfile (int sockfd, const char *path, size_t size) |
| receive file from socket More...
|
|
ssize_t | recvline (int fd, char *buf, size_t size, const char *d, size_t dlen) |
| receive data from socket until delimiter is not reached More...
|
|
int | sendfile (int sockfd, const char *path, size_t size) |
| Send file to the socket. More...
|
|
HTTP server, HTTP client, socket functionality.
return true if HTTP error occurred
- Parameters
-
- Returns
- comparison result
#define HTTP_HEADER_INT |
( |
|
n, |
|
|
|
v |
|
) |
| { .type = HTTP_HEADER_INT, .name = (n), .val.i = (v), } |
define HTTP header name with integer value
- Parameters
-
[in] | n | HTTP header name |
[in] | v | HTTP header value |
#define HTTP_HEADER_STRING |
( |
|
n, |
|
|
|
v |
|
) |
| { .type = HTTP_HEADER_STRING, .name = (n), .val.s = (v), } |
define HTTP header name with string value
- Parameters
-
[in] | n | HTTP header name |
[in] | v | HTTP header value |
#define IN_LOOPBACK |
( |
|
a | ) |
((((in_addr_t)(a)) & 0x7f000000) == 0x7f000000) |
expression is true, if address from loopback networks
typedef int(* http_header_cb_t) (const char *name, const char *value, void *priv) |
pointer to a callback function to process header
- Parameters
-
[in] | name | HTTP header name |
[in] | value | HTTP header value |
[in] | priv | private data for callback |
- Returns
- result of the function
- Return values
-
0 | if function ran properly, other values will mean an error |
HTTP methods, see rfc2616.
typedef int(* http_reply_cb_t) (const char *ver, int code, const char *msg, void *priv) |
pointer to a callback function to process reply
- Parameters
-
[in] | ver | version of HTTP protocol |
[in] | code | HTTP reply code |
[in] | msg | HTTP message |
[in] | priv | private data for callback |
- Returns
- result of the function
- Return values
-
0 | if function ran properly, other values will mean an error |
typedef int(* http_request_cb_t) (http_method_t m, const char *path, const char *ver, void *priv) |
pointer to a callback function to process request
- Parameters
-
[in] | method | HTTP method |
[in] | path | resource path |
[in] | ver | version of HTTP protocol |
[in] | priv | private data for callback |
- Returns
- result of the function
- Return values
-
0 | if function ran properly, other values will mean an error |
HTTP header values type.
Enumerator |
---|
HTTP_HEADER_STRING |
|
HTTP_HEADER_INT |
|
HTTP methods, see rfc2616.
Enumerator |
---|
HTTP_OPTIONS |
|
HTTP_GET |
|
HTTP_HEAD |
|
HTTP_POST |
|
HTTP_PUT |
|
HTTP_DELETE |
|
HTTP_TRACE |
|
HTTP_CONNECT |
|
HTTP status code.
Enumerator |
---|
HTTP_SUCCESS |
|
HTTP_CONNECTION_CLOSED |
|
HTTP_SOCKET_ERROR |
|
HTTP_LARGE_REQUEST |
|
HTTP_INVALID_REQUEST |
|
HTTP_UNKNOWN_REQUEST |
|
HTTP_INVALID_HEADER |
|
HTTP_INVALID_REPLY |
|
HTTP_ABORTED |
|
HTTP_OUT_MEMORY |
|
struct in_addr* gethostaddr |
( |
const char * |
hostname, |
|
|
struct in_addr * |
addr |
|
) |
| |
resolve hostname or ip address into in_addr
- Parameters
-
[in] | hostname | hostname or ip address string |
[out] | addr | struct in_addr |
- Returns
- pointer to
addr
, if successful
- Return values
-
read HTTP reply from socket and parse it
- Parameters
-
[in] | sockfd | socket descriptor |
| reply_cb | reply callback |
| header_cb | header callback |
| priv | private data for callbacks |
- Returns
- HTTP status code
- Return values
-
HTTP_ABORTED | aborted by callback |
HTTP_INVALID_REPLY | invalid HTTP reply |
send HTTP reply to socket
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | ver | HTTP version |
[in] | code | HTTP code |
[in] | desc | HTTP reply description |
[in] | hdr | headers |
[in] | count | headers count |
- Returns
- HTTP status code
- Return values
-
HTTP_INVALID_REQUEST | invalid version |
HTTP_OUT_MEMORY | out of memory to reply |
read HTTP request from socket and parse it
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | request_cb | request callback |
[in] | header_cb | header callback |
[in] | priv | private data for callbacks |
- Returns
- HTTP status code
- Return values
-
HTTP_ABORTED | aborted by callback |
send HTTP request by socket
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | method | HTTP method |
[in] | path | resource path |
[in] | ver | version of HTTP protocol |
[in] | hdr | array of headers |
[in] | count | count of headers in hdr |
- Returns
- HTTP status code
- Return values
-
HTTP_UNKNOWN_REQUEST | unknown HTTP request |
HTTP_INVALID_REQUEST | can't create valid HTTP request |
HTTP_OUT_MEMORY | can't complete HTTP request |
int iface_list |
( |
void(*)(struct iface *, void *) |
iface_cb, |
|
|
void * |
priv |
|
) |
| |
list IPv4 network interfaces
- Parameters
-
[in] | iface_cb | callback handler for interface |
[in] | priv | pointer to private data for callback routine |
- Returns
- on success, zero is returned
- Return values
-
If error occurred function update errno - see errors specified for the routine getifaddrs().
ssize_t recvall |
( |
int |
sockfd, |
|
|
void * |
buf, |
|
|
size_t |
len, |
|
|
int |
flags |
|
) |
| |
read buffer from socket descriptor
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | buf | buffer for read data |
[in] | len | length of buf in bytes |
[in] | flags | please read "man recv" for flags description |
- Returns
- amount of received bytes, should be equal
len
- Return values
-
0 | connection closed |
-1 | error occurred |
int recvfile |
( |
int |
sockfd, |
|
|
const char * |
path, |
|
|
size_t |
size |
|
) |
| |
receive file from socket
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | path | filename for receive file |
[in] | size | size of file to receive |
- Returns
- on success, zero is returned
If error occurred, function can create file with name path
and it will be incomplete.
ssize_t recvline |
( |
int |
fd, |
|
|
char * |
buf, |
|
|
size_t |
size, |
|
|
const char * |
d, |
|
|
size_t |
dlen |
|
) |
| |
receive data from socket until delimiter is not reached
- Parameters
-
[in] | fd | socket descriptor |
[out] | buf | buffer for received data |
[in] | size | length of buffer buf in bytes |
[in] | d | pointer to delimiter data |
[in] | dlen | length of delimiter data in bytes |
- Returns
- amount of received bytes
- Return values
-
0 | connection closed |
-1 | error occurred |
Buffer buf
should contain data with terminal delimiter d
if not, than buffer buf
is too small to store whole string.
int sendfile |
( |
int |
sockfd, |
|
|
const char * |
path, |
|
|
size_t |
size |
|
) |
| |
Send file to the socket.
- Parameters
-
[in] | sockfd | socket descriptor |
[in] | path | name of file to send |
[in] | size | size of file to send |
- Returns
- on success, zero is returned
- Return values
-
Function updates errno, if error occurred: