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 * SilcSchedule schedule,
79 * SilcHttpServerCallback callback, void *context);
83 * Allocates HTTP server and binds it to the IP address `ip' on the
84 * `port'. The `callback' with `context' will be called everytime a new
85 * HTTP request comes to the server from a HTTP client. In that callback
86 * the caller must then reply with the requested Web page or with error.
89 SilcHttpServer silc_http_server_alloc(const char *ip, SilcUInt16 port,
90 SilcSchedule schedule,
91 SilcHttpServerCallback callback,
94 /****f* silchttp/SilcHTTPServer/silc_http_server_free
98 * void silc_http_server_free(SilcHttpServer httpd);
102 * Close HTTP server and free all resources.
105 void silc_http_server_free(SilcHttpServer httpd);
107 /****f* silchttp/SilcHTTPServer/silc_http_server_free
111 * SilcBool silc_http_server_send(SilcHttpServer httpd,
112 * SilcHttpConnection conn,
117 * Send the HTTP data indicated by `data' buffer into the connection
118 * indicated by `conn'. Returns TRUE after the data is sent, and FALSE
119 * if error occurred. Usually the `data' would be the requested web page.
122 SilcBool silc_http_server_send(SilcHttpServer httpd,
123 SilcHttpConnection conn,
126 /****f* silchttp/SilcHTTPServer/silc_http_server_free
130 * SilcBool silc_http_server_send_error(SilcHttpServer httpd,
131 * SilcHttpConnection conn,
133 * const char *error_message);
137 * Send HTTP error back to the connection indicated by `conn'. The
138 * `error' is one of the 4xx or 5xx errors defined by the HTTP protocol.
139 * The `error_message' is the optional error message sent to the
140 * connection. Returns FALSE if the error could not be sent.
142 * Typical errors are: 400 Bad Request
148 * silc_http_server_send_error(httpd, conn, "400 Bad Request",
149 * "<body><h1>400 Bad Request!!</h1></body>");
152 SilcBool silc_http_server_send_error(SilcHttpServer httpd,
153 SilcHttpConnection conn,
155 const char *error_message);
157 /****f* silchttp/SilcHTTPServer/silc_http_server_get_header
161 * const char *silc_http_server_get_header(SilcHttpServer httpd,
162 * SilcHttpConnection conn,
163 * const char *field);
167 * Finds a header field indicated by `field' from the current HTTP
168 * request sent by the HTTP client. Returns the field value or NULL
169 * if suchs header field does not exist.
172 const char *silc_http_server_get_header(SilcHttpServer httpd,
173 SilcHttpConnection conn,
176 /****f* silchttp/SilcHTTPServer/silc_http_server_add_header
180 * SilcBool silc_http_server_add_header(SilcHttpServer httpd,
181 * SilcHttpConnection conn,
183 * const char *value);
187 * Adds a new header to the HTTP headers to be sent back to the
188 * HTTP client. This may be called to add needed headers to the
193 * silc_http_server_add_header(httpd, conn, "Content-Type", "image/jpeg");
196 SilcBool silc_http_server_add_header(SilcHttpServer httpd,
197 SilcHttpConnection conn,
201 #endif /* SILCHTTPSERVER_H */