MIDAS
Loading...
Searching...
No Matches
mongoose4.h
Go to the documentation of this file.
1// Copyright (c) 2004-2013 Sergey Lyubka <valenok@gmail.com>
2// Copyright (c) 2013 Cesanta Software Limited
3// All rights reserved
4//
5// This library is dual-licensed: you can redistribute it and/or modify
6// it under the terms of the GNU General Public License version 2 as
7// published by the Free Software Foundation. For the terms of this
8// license, see <http://www.gnu.org/licenses/>.
9//
10// You are free to use this library under the terms of the GNU General
11// Public License, but WITHOUT ANY WARRANTY; without even the implied
12// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13// See the GNU General Public License for more details.
14//
15// Alternatively, you can license this library under a commercial
16// license, as set out in <http://cesanta.com/products.html>.
17
18#ifndef MONGOOSE_HEADER_INCLUDED
19#define MONGOOSE_HEADER_INCLUDED
20
21#include <stdio.h>
22#include <stddef.h>
23
27#ifdef __cplusplus
28extern "C" {
29#endif // __cplusplus
30
31struct mg_context; // Web server instance
32struct mg_connection; // HTTP request descriptor
33
34
35// This structure contains information about the HTTP request.
37 const char *request_method; // "GET", "POST", etc
38 const char *uri; // URL-decoded URI
39 const char *http_version; // E.g. "1.0", "1.1"
40 const char *query_string; // URL part after '?', not including '?', or NULL
41 const char *remote_user; // Authenticated user, or NULL if no auth used
42 long remote_ip; // Client's IP address
43 int remote_port; // Client's port
44 int is_ssl; // 1 if SSL-ed, 0 if not
45
46 int num_headers; // Number of HTTP headers
47 struct mg_header {
48 const char *name; // HTTP header name
49 const char *value; // HTTP header value
50 } http_headers[64]; // Maximum 64 headers
51};
52
53struct mg_event {
54 int type; // Event type, possible types are defined below
55#define MG_REQUEST_BEGIN 1 // event_param: NULL
56#define MG_REQUEST_END 2 // event_param: NULL
57#define MG_HTTP_ERROR 3 // event_param: int status_code
58#define MG_EVENT_LOG 4 // event_param: const char *message
59#define MG_THREAD_BEGIN 5 // event_param: NULL
60#define MG_THREAD_END 6 // event_param: NULL
61
62 void *user_data; // User data pointer passed to mg_start()
63 void *conn_data; // Connection-specific, per-thread user data.
64 void *event_param; // Event-specific parameter
65
68};
69
70typedef int (*mg_event_handler_t)(struct mg_event *event);
71
72struct mg_context *mg_start(const char **configuration_options,
73 mg_event_handler_t func, void *user_data);
74void mg_stop(struct mg_context *);
75
77int mg_websocket_read(struct mg_connection *, int *bits, char **data);
79 const char *data, size_t data_len);
80// Websocket opcodes, from http://tools.ietf.org/html/rfc6455
81enum {
88};
89
90const char *mg_get_option(const struct mg_context *ctx, const char *name);
91const char **mg_get_valid_option_names(void);
93 const char *domain,
94 const char *user,
95 const char *password);
96int mg_write(struct mg_connection *, const void *buf, int len);
97
98// Macros for enabling compiler-specific checks for printf-like arguments.
99#undef PRINTF_FORMAT_STRING
100#if defined(_MSC_VER) && _MSC_VER >= 1400
101#include <sal.h>
102#if defined(_MSC_VER) && _MSC_VER > 1400
103#define PRINTF_FORMAT_STRING(s) _Printf_format_string_ s
104#else
105#define PRINTF_FORMAT_STRING(s) __format_string s
106#endif
107#else
108#define PRINTF_FORMAT_STRING(s) s
109#endif
110
111#ifdef __GNUC__
112#define PRINTF_ARGS(x, y) __attribute__((format(printf, x, y)))
113#else
114#define PRINTF_ARGS(x, y)
115#endif
116
117// Send data to the client using printf() semantics.
118//
119// Works exactly like mg_write(), but allows to do message formatting.
121 PRINTF_FORMAT_STRING(const char *fmt), ...) PRINTF_ARGS(2, 3);
122
123
124// Send contents of the entire file together with HTTP headers.
125void mg_send_file(struct mg_connection *conn, const char *path);
126
127
128// Read data from the remote end, return number of bytes read.
129// Return:
130// 0 connection has been closed by peer. No more data could be read.
131// < 0 read error. No more data could be read from the connection.
132// > 0 number of bytes read into the buffer.
133int mg_read(struct mg_connection *, void *buf, int len);
134
135
136// Get the value of particular HTTP header.
137//
138// This is a helper function. It traverses request_info->http_headers array,
139// and if the header is present in the array, returns its value. If it is
140// not present, NULL is returned.
142
143
144// Get a value of particular form variable.
145//
146// Parameters:
147// data: pointer to form-uri-encoded buffer. This could be either POST data,
148// or request_info.query_string.
149// data_len: length of the encoded data.
150// var_name: variable name to decode from the buffer
151// dst: destination buffer for the decoded variable
152// dst_len: length of the destination buffer
153//
154// Return:
155// On success, length of the decoded variable.
156// On error:
157// -1 (variable not found).
158// -2 (destination buffer is NULL, zero length or too small to hold the
159// decoded variable).
160//
161// Destination buffer is guaranteed to be '\0' - terminated if it is not
162// NULL or zero length.
163int mg_get_var(const char *data, size_t data_len,
164 const char *var_name, char *dst, size_t dst_len);
165
166// Fetch value of certain cookie variable into the destination buffer.
167//
168// Destination buffer is guaranteed to be '\0' - terminated. In case of
169// failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
170// parameter. This function returns only first occurrence.
171//
172// Return:
173// On success, value length.
174// On error:
175// -1 (either "Cookie:" header is not present at all or the requested
176// parameter is not found).
177// -2 (destination buffer is NULL, zero length or too small to hold the
178// value).
179int mg_get_cookie(const char *cookie, const char *var_name,
180 char *buf, size_t buf_len);
181
182
183// Download data from the remote web server.
184// host: host name to connect to, e.g. "foo.com", or "10.12.40.1".
185// port: port number, e.g. 80.
186// use_ssl: wether to use SSL connection.
187// error_buffer, error_buffer_size: error message placeholder.
188// request_fmt,...: HTTP request.
189// Return:
190// On success, valid pointer to the new connection, suitable for mg_read().
191// On error, NULL. error_buffer contains error message.
192// Example:
193// char ebuf[100];
194// struct mg_connection *conn;
195// conn = mg_download("google.com", 80, 0, ebuf, sizeof(ebuf),
196// "%s", "GET / HTTP/1.0\r\nHost: google.com\r\n\r\n");
197struct mg_connection *mg_download(const char *host, int port, int use_ssl,
198 char *error_buffer, size_t error_buffer_size,
200 ...) PRINTF_ARGS(6, 7);
201
202
203// Close the connection opened by mg_download().
205
206
207// Read multipart-form-data POST buffer, save uploaded files into
208// destination directory, and return path to the saved filed.
209// This function can be called multiple times for the same connection,
210// if more then one file is uploaded.
211// Return: path to the uploaded file, or NULL if there are no more files.
213 char *path, int path_len);
214
215
216// Convenience function -- create detached thread.
217// Return: 0 on success, non-0 on error.
218typedef void * (*mg_thread_func_t)(void *);
219int mg_start_thread(mg_thread_func_t f, void *p);
220
221
222// Return builtin mime type for the given file name.
223// For unrecognized extensions, "text/plain" is returned.
225
226
227// Return Mongoose version.
228const char *mg_version(void);
229
230// URL-decode input buffer into destination buffer.
231// 0-terminate the destination buffer.
232// form-url-encoded data differs from URI encoding in a way that it
233// uses '+' as character for space, see RFC 1866 section 8.2.1
234// http://ftp.ics.uci.edu/pub/ietf/html/rfc1866.txt
235// Return: length of the decoded data, or -1 if dst buffer is too small.
236int mg_url_decode(const char *src, int src_len, char *dst,
238
239// MD5 hash given strings.
240// Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
241// ASCIIz strings. When function returns, buf will contain human-readable
242// MD5 hash. Example:
243// char buf[33];
244// mg_md5(buf, "aa", "bb", NULL);
245char *mg_md5(char buf[33], ...);
246
247
248#ifdef __cplusplus
249}
250#endif // __cplusplus
251
252#endif // MONGOOSE_HEADER_INCLUDED
int mg_modify_passwords_file(const char *passwords_file_name, const char *domain, const char *user, const char *password)
int mg_websocket_read(struct mg_connection *, int *bits, char **data)
void *(* mg_thread_func_t)(void *)
Definition mongoose4.h:218
struct mg_connection void mg_close_connection(struct mg_connection *conn)
int mg_websocket_write(struct mg_connection *conn, int opcode, const char *data, size_t data_len)
int type
Definition mongoose4.h:54
#define PRINTF_FORMAT_STRING(s)
Definition mongoose4.h:108
const char * mg_get_option(const struct mg_context *ctx, const char *name)
const char * mg_get_header(const struct mg_connection *, const char *name)
void * conn_data
Definition mongoose4.h:63
int mg_get_var(const char *data, size_t data_len, const char *var_name, char *dst, size_t dst_len)
int mg_start_thread(mg_thread_func_t f, void *p)
const char * request_method
Definition mongoose4.h:37
struct mg_request_info * request_info
Definition mongoose4.h:67
struct mg_connection * conn
Definition mongoose4.h:66
char * mg_md5(char buf[33],...)
const char * query_string
Definition mongoose4.h:40
const char * uri
Definition mongoose4.h:38
struct mg_context * mg_start(const char **configuration_options, mg_event_handler_t func, void *user_data)
int mg_url_decode(const char *src, int src_len, char *dst, int dst_len, int is_form_url_encoded)
int mg_printf(struct mg_connection *, PRINTF_FORMAT_STRING(const char *fmt),...) PRINTF_ARGS(2
#define PRINTF_ARGS(x, y)
Definition mongoose4.h:114
struct mg_connection * mg_download(const char *host, int port, int use_ssl, char *error_buffer, size_t error_buffer_size, PRINTF_FORMAT_STRING(const char *request_fmt),...) PRINTF_ARGS(6
int mg_write(struct mg_connection *, const void *buf, int len)
const char * mg_get_builtin_mime_type(const char *file_name)
const char ** mg_get_valid_option_names(void)
FILE * mg_upload(struct mg_connection *conn, const char *destination_dir, char *path, int path_len)
int void mg_send_file(struct mg_connection *conn, const char *path)
const char * http_version
Definition mongoose4.h:39
void * user_data
Definition mongoose4.h:62
int mg_get_cookie(const char *cookie, const char *var_name, char *buf, size_t buf_len)
void mg_stop(struct mg_context *)
void * event_param
Definition mongoose4.h:64
int(* mg_event_handler_t)(struct mg_event *event)
Definition mongoose4.h:70
const char * remote_user
Definition mongoose4.h:41
void mg_websocket_handshake(struct mg_connection *)
const char * mg_version(void)
struct mg_request_info::mg_header http_headers[64]
int mg_read(struct mg_connection *, void *buf, int len)
@ WEBSOCKET_OPCODE_CONNECTION_CLOSE
Definition mongoose4.h:85
@ WEBSOCKET_OPCODE_PONG
Definition mongoose4.h:87
@ WEBSOCKET_OPCODE_TEXT
Definition mongoose4.h:83
@ WEBSOCKET_OPCODE_CONTINUATION
Definition mongoose4.h:82
@ WEBSOCKET_OPCODE_BINARY
Definition mongoose4.h:84
@ WEBSOCKET_OPCODE_PING
Definition mongoose4.h:86
void * data
Definition mana.cxx:268
#define name(x)
Definition midas_macro.h:24
char file_name[256]
Definition odbhist.cxx:41
char var_name[256]
Definition odbhist.cxx:41
TH1X EXPRT * h1_book(const char *name, const char *title, int bins, double min, double max)
Definition rmidas.h:24
void * user_data