5 Author: Pekka Riikonen <priikone@silcnet.org>
7 Copyright (C) 2006 Pekka Riikonen
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; version 2 of the License.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
20 /****h* silchttp/SILC HTTP Server Interface
24 * Very simple HTTP server interface. This HTTP server supports basic HTTP
25 * features. All pages on the server are dynamically created by the caller
26 * of this interface. The server does not support plugins, modules, cgi-bin,
27 * server-side includes or any other special features.
31 #ifndef SILCHTTPSERVER_H
32 #define SILCHTTPSERVER_H
34 typedef struct SilcHttpServerStruct *SilcHttpServer;
35 typedef struct SilcHttpConnectionStruct *SilcHttpConnection;
37 /****f* silchttp/SilcHTTPServer/SilcHttpServerCallback
41 * typedef void (*SilcHttpServerCallback)(SilcHttpServer httpd,
42 * SilcHttpConnection conn,
50 * The HTTP request callback, that is called everytime a new HTTP request
51 * comes from a HTTP client. The `uri' is the requested URI (web page),
52 * and the `method' is the HTTP request method (GET, POST, etc.). The
53 * `data' is non-NULL only if the `method' is POST, and it includes the
56 * The requested web page must be returned to the HTTP client from this
57 * callback by calling silc_http_server_send or error is returned by
58 * calling silc_http_server_send_error.
60 * The silc_http_server_get_header may be called to find a specific
61 * HTTP header from this request. New headers may be added to the
62 * reply by calling silc_http_server_add_header.
65 typedef void (*SilcHttpServerCallback)(SilcHttpServer httpd,
66 SilcHttpConnection conn,
72 /****f* silchttp/SilcHTTPServer/silc_http_server_alloc
77 * silc_http_server_alloc(const char *ip, SilcUInt16 port,
78 * SilcUInt32 max_connections,
79 * SilcSchedule schedule,
80 * SilcHttpServerCallback callback, void *context);
84 * Allocates HTTP server and binds it to the IP address `ip' on the
85 * `port'. If `max_connections' is non-zero, that many connections
86 * are allowed to the HTTP server. The `callback' with `context' will
87 * be called everytime a new HTTP request comes to the server from
88 * a HTTP client. In that callback the caller must then reply with
89 * the requested Web page or with an error.
92 SilcHttpServer silc_http_server_alloc(const char *ip, SilcUInt16 port,
93 SilcUInt32 max_connections,
94 SilcSchedule schedule,
95 SilcHttpServerCallback callback,
98 /****f* silchttp/SilcHTTPServer/silc_http_server_free
102 * void silc_http_server_free(SilcHttpServer httpd);
106 * Close HTTP server and free all resources.
109 void silc_http_server_free(SilcHttpServer httpd);
111 /****f* silchttp/SilcHTTPServer/silc_http_server_free
115 * SilcBool silc_http_server_send(SilcHttpServer httpd,
116 * SilcHttpConnection conn,
121 * Send the HTTP data indicated by `data' buffer into the connection
122 * indicated by `conn'. Returns TRUE after the data is sent, and FALSE
123 * if error occurred. Usually the `data' would be the requested web page.
126 SilcBool silc_http_server_send(SilcHttpServer httpd,
127 SilcHttpConnection conn,
130 /****f* silchttp/SilcHTTPServer/silc_http_server_free
134 * SilcBool silc_http_server_send_error(SilcHttpServer httpd,
135 * SilcHttpConnection conn,
137 * const char *error_message);
141 * Send HTTP error back to the connection indicated by `conn'. The
142 * `error' is one of the 4xx or 5xx errors defined by the HTTP protocol.
143 * The `error_message' is the optional error message sent to the
144 * connection. Returns FALSE if the error could not be sent.
146 * Typical errors are: 400 Bad Request
152 * silc_http_server_send_error(httpd, conn, "400 Bad Request",
153 * "<body><h1>400 Bad Request!!</h1></body>");
156 SilcBool silc_http_server_send_error(SilcHttpServer httpd,
157 SilcHttpConnection conn,
159 const char *error_message);
161 /****f* silchttp/SilcHTTPServer/silc_http_server_get_header
165 * const char *silc_http_server_get_header(SilcHttpServer httpd,
166 * SilcHttpConnection conn,
167 * const char *field);
171 * Finds a header field indicated by `field' from the current HTTP
172 * request sent by the HTTP client. Returns the field value or NULL
173 * if suchs header field does not exist.
176 const char *silc_http_server_get_header(SilcHttpServer httpd,
177 SilcHttpConnection conn,
180 /****f* silchttp/SilcHTTPServer/silc_http_server_add_header
184 * SilcBool silc_http_server_add_header(SilcHttpServer httpd,
185 * SilcHttpConnection conn,
187 * const char *value);
191 * Adds a new header to the HTTP headers to be sent back to the
192 * HTTP client. This may be called to add needed headers to the
197 * silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
200 SilcBool silc_http_server_add_header(SilcHttpServer httpd,
201 SilcHttpConnection conn,
205 #endif /* SILCHTTPSERVER_H */