Pyrogenesis  trunk
mongoose.h
Go to the documentation of this file.
1 // Copyright (c) 2004-2011 Sergey Lyubka
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef MONGOOSE_HEADER_INCLUDED
22 #define MONGOOSE_HEADER_INCLUDED
23 
24 #include <stddef.h>
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif // __cplusplus
29 
30 struct mg_context; // Handle for the HTTP service itself
31 struct mg_connection; // Handle for the individual connection
32 
33 
34 // This structure contains information about the HTTP request.
36  void *user_data; // User-defined pointer passed to mg_start()
37  char *request_method; // "GET", "POST", etc
38  char *uri; // URL-decoded URI
39  char *http_version; // E.g. "1.0", "1.1"
40  char *query_string; // URL part after '?' (not including '?') or NULL
41  char *remote_user; // Authenticated user, or NULL if no auth used
42  char *log_message; // Mongoose error log message, MG_EVENT_LOG only
43  long remote_ip; // Client's IP address
44  int remote_port; // Client's port
45  int status_code; // HTTP reply status code, e.g. 200
46  int is_ssl; // 1 if SSL-ed, 0 if not
47  int num_headers; // Number of headers
48  struct mg_header {
49  char *name; // HTTP header name
50  char *value; // HTTP header value
51  } http_headers[64]; // Maximum 64 headers
52 };
53 
54 // Various events on which user-defined function is called by Mongoose.
55 enum mg_event {
56  MG_NEW_REQUEST, // New HTTP request has arrived from the client
57  MG_HTTP_ERROR, // HTTP error must be returned to the client
58  MG_EVENT_LOG, // Mongoose logs an event, request_info.log_message
59  MG_INIT_SSL // Mongoose initializes SSL. Instead of mg_connection *,
60  // SSL context is passed to the callback function.
61 };
62 
63 // Prototype for the user-defined function. Mongoose calls this function
64 // on every MG_* event.
65 //
66 // Parameters:
67 // event: which event has been triggered.
68 // conn: opaque connection handler. Could be used to read, write data to the
69 // client, etc. See functions below that have "mg_connection *" arg.
70 // request_info: Information about HTTP request.
71 //
72 // Return:
73 // If handler returns non-NULL, that means that handler has processed the
74 // request by sending appropriate HTTP reply to the client. Mongoose treats
75 // the request as served.
76 // If handler returns NULL, that means that handler has not processed
77 // the request. Handler must not send any data to the client in this case.
78 // Mongoose proceeds with request handling as if nothing happened.
79 typedef void * (*mg_callback_t)(enum mg_event event,
80  struct mg_connection *conn,
81  const struct mg_request_info *request_info);
82 
83 
84 // Start web server.
85 //
86 // Parameters:
87 // callback: user defined event handling function or NULL.
88 // options: NULL terminated list of option_name, option_value pairs that
89 // specify Mongoose configuration parameters.
90 //
91 // Side-effects: on UNIX, ignores SIGCHLD and SIGPIPE signals. If custom
92 // processing is required for these, signal handlers must be set up
93 // after calling mg_start().
94 //
95 //
96 // Example:
97 // const char *options[] = {
98 // "document_root", "/var/www",
99 // "listening_ports", "80,443s",
100 // NULL
101 // };
102 // struct mg_context *ctx = mg_start(&my_func, NULL, options);
103 //
104 // Please refer to http://code.google.com/p/mongoose/wiki/MongooseManual
105 // for the list of valid option and their possible values.
106 //
107 // Return:
108 // web server context, or NULL on error.
109 struct mg_context *mg_start(mg_callback_t callback, void *user_data,
110  const char **options);
111 
112 
113 // Stop the web server.
114 //
115 // Must be called last, when an application wants to stop the web server and
116 // release all associated resources. This function blocks until all Mongoose
117 // threads are stopped. Context pointer becomes invalid.
118 void mg_stop(struct mg_context *);
119 
120 
121 // Get the value of particular configuration parameter.
122 // The value returned is read-only. Mongoose does not allow changing
123 // configuration at run time.
124 // If given parameter name is not valid, NULL is returned. For valid
125 // names, return value is guaranteed to be non-NULL. If parameter is not
126 // set, zero-length string is returned.
127 const char *mg_get_option(const struct mg_context *ctx, const char *name);
128 
129 
130 // Return array of strings that represent valid configuration options.
131 // For each option, a short name, long name, and default value is returned.
132 // Array is NULL terminated.
133 const char **mg_get_valid_option_names(void);
134 
135 
136 // Add, edit or delete the entry in the passwords file.
137 //
138 // This function allows an application to manipulate .htpasswd files on the
139 // fly by adding, deleting and changing user records. This is one of the
140 // several ways of implementing authentication on the server side. For another,
141 // cookie-based way please refer to the examples/chat.c in the source tree.
142 //
143 // If password is not NULL, entry is added (or modified if already exists).
144 // If password is NULL, entry is deleted.
145 //
146 // Return:
147 // 1 on success, 0 on error.
148 int mg_modify_passwords_file(const char *passwords_file_name,
149  const char *domain,
150  const char *user,
151  const char *password);
152 
153 // Send data to the client.
154 int mg_write(struct mg_connection *, const void *buf, size_t len);
155 
156 
157 // Send data to the browser using printf() semantics.
158 //
159 // Works exactly like mg_write(), but allows to do message formatting.
160 // Note that mg_printf() uses internal buffer of size IO_BUF_SIZE
161 // (8 Kb by default) as temporary message storage for formatting. Do not
162 // print data that is bigger than that, otherwise it will be truncated.
163 int mg_printf(struct mg_connection *, const char *fmt, ...);
164 
165 
166 // Send contents of the entire file together with HTTP headers.
167 void mg_send_file(struct mg_connection *conn, const char *path);
168 
169 
170 // Read data from the remote end, return number of bytes read.
171 int mg_read(struct mg_connection *, void *buf, size_t len);
172 
173 
174 // Get the value of particular HTTP header.
175 //
176 // This is a helper function. It traverses request_info->http_headers array,
177 // and if the header is present in the array, returns its value. If it is
178 // not present, NULL is returned.
179 const char *mg_get_header(const struct mg_connection *, const char *name);
180 
181 
182 // Get a value of particular form variable.
183 //
184 // Parameters:
185 // data: pointer to form-uri-encoded buffer. This could be either POST data,
186 // or request_info.query_string.
187 // data_len: length of the encoded data.
188 // var_name: variable name to decode from the buffer
189 // buf: destination buffer for the decoded variable
190 // buf_len: length of the destination buffer
191 //
192 // Return:
193 // On success, length of the decoded variable.
194 // On error, -1 (variable not found, or destination buffer is too small).
195 //
196 // Destination buffer is guaranteed to be '\0' - terminated. In case of
197 // failure, dst[0] == '\0'.
198 int mg_get_var(const char *data, size_t data_len,
199  const char *var_name, char *buf, size_t buf_len);
200 
201 // Fetch value of certain cookie variable into the destination buffer.
202 //
203 // Destination buffer is guaranteed to be '\0' - terminated. In case of
204 // failure, dst[0] == '\0'. Note that RFC allows many occurrences of the same
205 // parameter. This function returns only first occurrence.
206 //
207 // Return:
208 // On success, value length.
209 // On error, 0 (either "Cookie:" header is not present at all, or the
210 // requested parameter is not found, or destination buffer is too small
211 // to hold the value).
212 int mg_get_cookie(const struct mg_connection *,
213  const char *cookie_name, char *buf, size_t buf_len);
214 
215 
216 // Return Mongoose version.
217 const char *mg_version(void);
218 
219 
220 // MD5 hash given strings.
221 // Buffer 'buf' must be 33 bytes long. Varargs is a NULL terminated list of
222 // asciiz strings. When function returns, buf will contain human-readable
223 // MD5 hash. Example:
224 // char buf[33];
225 // mg_md5(buf, "aa", "bb", NULL);
226 void mg_md5(char *buf, ...);
227 
228 
229 #ifdef __cplusplus
230 }
231 #endif // __cplusplus
232 
233 #endif // MONGOOSE_HEADER_INCLUDED
Definition: mongoose.h:56
int mg_get_cookie(const struct mg_connection *, const char *cookie_name, char *buf, size_t buf_len)
Definition: mongoose.cpp:1530
void mg_send_file(struct mg_connection *conn, const char *path)
Definition: mongoose.cpp:2611
char * request_method
Definition: mongoose.h:37
char * uri
Definition: mongoose.h:38
struct mg_request_info::mg_header http_headers[64]
void *(* mg_callback_t)(enum mg_event event, struct mg_connection *conn, const struct mg_request_info *request_info)
Definition: mongoose.h:79
Definition: mongoose.h:57
int num_headers
Definition: mongoose.h:47
char * log_message
Definition: mongoose.h:42
int mg_write(struct mg_connection *, const void *buf, size_t len)
Definition: mongoose.cpp:1442
void mg_md5(char *buf,...)
Definition: mongoose.cpp:2019
Definition: mongoose.h:59
const char * mg_get_option(const struct mg_context *ctx, const char *name)
Definition: mongoose.cpp:539
int status_code
Definition: mongoose.h:45
int mg_get_var(const char *data, size_t data_len, const char *var_name, char *buf, size_t buf_len)
Definition: mongoose.cpp:1494
Definition: mongoose.h:35
void mg_stop(struct mg_context *)
Definition: mongoose.cpp:4202
int mg_modify_passwords_file(const char *passwords_file_name, const char *domain, const char *user, const char *password)
Definition: mongoose.cpp:2253
char * value
Definition: mongoose.h:50
int remote_port
Definition: mongoose.h:44
char * http_version
Definition: mongoose.h:39
long remote_ip
Definition: mongoose.h:43
mg_event
Definition: mongoose.h:55
int mg_read(struct mg_connection *, void *buf, size_t len)
Definition: mongoose.cpp:1392
char * query_string
Definition: mongoose.h:40
struct mg_context * mg_start(mg_callback_t callback, void *user_data, const char **options)
Definition: mongoose.cpp:4216
char * name
Definition: mongoose.h:49
Definition: mongoose.cpp:501
void * user_data
Definition: mongoose.h:36
int is_ssl
Definition: mongoose.h:46
Definition: format.h:119
const char ** mg_get_valid_option_names(void)
Definition: mongoose.cpp:517
Definition: mongoose.cpp:481
const char * mg_version(void)
Definition: mongoose.cpp:610
int mg_printf(struct mg_connection *, const char *fmt,...)
Definition: mongoose.cpp:1447
char * remote_user
Definition: mongoose.h:41
Definition: mongoose.h:58
const char * mg_get_header(const struct mg_connection *, const char *name)
Definition: mongoose.cpp:762
Definition: mongoose.h:48