This repository has been archived by the owner on Feb 6, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhttp.h
73 lines (58 loc) · 2.5 KB
/
http.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* libhttp - A HTTP (RFC 2616) library for C.
*/
#ifndef http_h
#define http_h
#include <stddef.h>
typedef struct http_stream_s http_stream;
typedef struct http_session_s http_session;
typedef struct http_message_s http_message;
typedef int (*http_stream_cb)(http_stream* stream, void* context);
enum http_error_codes {
HTTP_ENOTIMPL /* Not yet implemented. */
};
enum http_message_type {
HTTP_MESSAGE_REQUEST,
HTTP_MESSAGE_RESPONSE
};
/* Streams provide an interface for writing data to the socket
* bound to a session. They are responsible for handling any
* encoding/decoding of the data. Data will be written or read from
* buffers supplied and returning number of bytes actually transferred.
* Note: these methods may or may not block depending on the
* underlying socket's mode.
*/
size_t http_stream_read(char* buffer, size_t size);
size_t http_stream_write(const char* buffer, size_t size);
/* A session represents a connection between a client and the server.
* Create a socket connected to the server or accept incoming client
* from listening socket. Initialize a new HTTP session using this socket.
* Closing the session also closes the underlying socket.
*/
http_session* http_session_init(int sockfd);
void http_session_close(http_session* session);
/* Parsing of messages arriving from a session.
* Clients will parse responses while servers parse requests.
*/
http_message* http_session_parse_request(http_session* session);
http_message* http_session_parse_response(http_session* session);
/* Compose a new message for sending with a session.
* Clients will compose requests while servers compose responses.
*/
http_message* http_request(const char* method, const char* uri);
http_message* http_response(int status_code, const char* reason_phrase);
/* Append a new header to the message if a field with
* this name does not yet exist. If it already exists then
* the new value will be appended follow RFC2616 4.2 guidelines.
*/
void http_message_append_header(http_message* msg, const char* name, const char* value);
/* Type of message: request or response. */
int http_message_get_type(http_message* msg);
/* Set a stream callback for reading or writing the message body.
* A parsed (incoming) message will read while a composed (outgoing)
* message will be writing.
*/
void http_message_set_body_cb(http_message* msg, http_stream_cb cb, void* context);
/* Send a fully composed message with a session. */
int http_session_send_message(http_session* session, const http_message* msg);
#endif