Main Page | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals

http.h

Go to the documentation of this file.
00001 /* ==================================================================== 
00002  * The Kannel Software License, Version 1.0 
00003  * 
00004  * Copyright (c) 2001-2008 Kannel Group  
00005  * Copyright (c) 1998-2001 WapIT Ltd.   
00006  * All rights reserved. 
00007  * 
00008  * Redistribution and use in source and binary forms, with or without 
00009  * modification, are permitted provided that the following conditions 
00010  * are met: 
00011  * 
00012  * 1. Redistributions of source code must retain the above copyright 
00013  *    notice, this list of conditions and the following disclaimer. 
00014  * 
00015  * 2. Redistributions in binary form must reproduce the above copyright 
00016  *    notice, this list of conditions and the following disclaimer in 
00017  *    the documentation and/or other materials provided with the 
00018  *    distribution. 
00019  * 
00020  * 3. The end-user documentation included with the redistribution, 
00021  *    if any, must include the following acknowledgment: 
00022  *       "This product includes software developed by the 
00023  *        Kannel Group (http://www.kannel.org/)." 
00024  *    Alternately, this acknowledgment may appear in the software itself, 
00025  *    if and wherever such third-party acknowledgments normally appear. 
00026  * 
00027  * 4. The names "Kannel" and "Kannel Group" must not be used to 
00028  *    endorse or promote products derived from this software without 
00029  *    prior written permission. For written permission, please  
00030  *    contact org@kannel.org. 
00031  * 
00032  * 5. Products derived from this software may not be called "Kannel", 
00033  *    nor may "Kannel" appear in their name, without prior written 
00034  *    permission of the Kannel Group. 
00035  * 
00036  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED 
00037  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
00038  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
00039  * DISCLAIMED.  IN NO EVENT SHALL THE KANNEL GROUP OR ITS CONTRIBUTORS 
00040  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,  
00041  * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  
00042  * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR  
00043  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,  
00044  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE  
00045  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,  
00046  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
00047  * ==================================================================== 
00048  * 
00049  * This software consists of voluntary contributions made by many 
00050  * individuals on behalf of the Kannel Group.  For more information on  
00051  * the Kannel Group, please see <http://www.kannel.org/>. 
00052  * 
00053  * Portions of this software are based upon software originally written at  
00054  * WapIT Ltd., Helsinki, Finland for the Kannel project.  
00055  */ 
00056 
00057 /*
00058  * http.h - HTTP protocol implementation
00059  *
00060  * This header file defines the interface to the HTTP implementation
00061  * in Kannel.
00062  * 
00063  * We implement both the client and the server side of the protocol.
00064  * We don't implement HTTP completely - only those parts that Kannel needs.
00065  * You may or may not be able to use this code for other projects. It has
00066  * not been a goal, but it might be possible, though you do need other
00067  * parts of Kannel's gwlib as well.
00068  * 
00069  * Initialization
00070  * ==============
00071  *
00072  * The library MUST be initialized by a call to http_init. Failure to
00073  * initialize means the library WILL NOT work. Note that the library
00074  * can't initialize itself implicitly, because it cannot reliably
00075  * create a mutex to protect the initialization. Therefore, it is the
00076  * caller's responsibility to call http_init exactly once (no more, no
00077  * less) at the beginning of the process, before any other thread makes
00078  * any calls to the library.
00079  * 
00080  * Client functionality
00081  * ====================
00082  * 
00083  * The library will invisibly keep the connections to HTTP servers open,
00084  * so that it is possible to make several HTTP requests over a single
00085  * TCP connection. This makes it much more efficient in high-load situations.
00086  * On the other hand, if one request takes long, the library will still
00087  * use several connections to the same server anyway.
00088  * 
00089  * The library user can specify an HTTP proxy to be used. There can be only
00090  * one proxy at a time, but it is possible to specify a list of hosts for
00091  * which the proxy is not used. The proxy can be changed at run time.
00092  * 
00093  * Server functionality
00094  * ====================
00095  * 
00096  * The library allows the implementation of an HTTP server by having
00097  * functions to specify which ports should be open, and receiving requests
00098  * from those ports.
00099  * 
00100  * Header manipulation
00101  * ===================
00102  * 
00103  * The library additionally has some functions for manipulating lists of
00104  * headers. These take a `List' (see gwlib/list.h) of Octstr's. The list
00105  * represents a list of headers in an HTTP request or reply. The functions
00106  * manipulate the list by adding and removing headers by name. It is a
00107  * very bad idea to manipulate the list without using the header
00108  * manipulation functions, however.
00109  *
00110  * Basic Authentication
00111  * ====================
00112  *
00113  * Basic Authentication is the standard way for a client to authenticate
00114  * itself to a server. It is done by adding an "Authorization" header
00115  * to the request. The interface in this header therefore doesn't mention
00116  * it, but the client and the server can do it by checking the headers
00117  * using the generic functions provided.
00118  *
00119  * Acknowledgements
00120  * ================
00121  *
00122  * Design: Lars Wirzenius, Richard Braakman
00123  * Implementation: Lars Wirzenius
00124  */
00125 
00126 
00127 #ifndef HTTP_H
00128 #define HTTP_H
00129 
00130 #include "gwlib/list.h"
00131 #include "gwlib/octstr.h"
00132 
00133 
00134 /*
00135  * Well-known return values from HTTP servers. This is not a complete
00136  * list, but it includes the values that Kannel needs to handle
00137  * specially.
00138  */
00139 
00140 enum {
00141     HTTP_OK                        = 200,
00142     HTTP_CREATED                   = 201,
00143     HTTP_ACCEPTED                  = 202,
00144     HTTP_NO_CONTENT                = 204,
00145     HTTP_RESET_CONTENT             = 205,
00146     HTTP_MOVED_PERMANENTLY         = 301,
00147     HTTP_FOUND                     = 302,
00148     HTTP_SEE_OTHER                 = 303,
00149     HTTP_NOT_MODIFIED              = 304,
00150     HTTP_TEMPORARY_REDIRECT        = 307,
00151     HTTP_BAD_REQUEST               = 400,
00152     HTTP_UNAUTHORIZED              = 401,
00153     HTTP_FORBIDDEN                 = 403,
00154     HTTP_NOT_FOUND                 = 404,
00155     HTTP_BAD_METHOD                = 405,
00156     HTTP_NOT_ACCEPTABLE            = 406,
00157     HTTP_REQUEST_ENTITY_TOO_LARGE  = 413,
00158     HTTP_UNSUPPORTED_MEDIA_TYPE    = 415,
00159     HTTP_INTERNAL_SERVER_ERROR     = 500,
00160     HTTP_NOT_IMPLEMENTED           = 501,
00161     HTTP_BAD_GATEWAY               = 502,
00162     HTTP_SERVICE_UNAVAILABLE       = 503
00163 };
00164 
00165 /*
00166  * Groupings of the status codes listed above.
00167  * See the http_status_class() function.
00168  */
00169 
00170 enum {
00171     HTTP_STATUS_PROVISIONAL = 100,
00172     HTTP_STATUS_SUCCESSFUL = 200,
00173     HTTP_STATUS_REDIRECTION = 300,
00174     HTTP_STATUS_CLIENT_ERROR = 400,
00175     HTTP_STATUS_SERVER_ERROR = 500,
00176     HTTP_STATUS_UNKNOWN = 0
00177 };
00178 
00179 
00180 /*
00181  * Methods supported by this HTTP library.  Currently not public but
00182  * probably should be.
00183  */
00184 enum {
00185     HTTP_METHOD_GET = 1,
00186     HTTP_METHOD_POST = 2,
00187     HTTP_METHOD_HEAD = 3
00188 };
00189 
00190 /*
00191  * A structure describing a CGI-BIN argument/variable.
00192  */
00193 typedef struct {
00194     Octstr *name;
00195     Octstr *value;
00196 } HTTPCGIVar;
00197 
00198 
00199 /*
00200  * Initialization function. This MUST be called before any other function
00201  * declared in this header file.
00202  */
00203 void http_init(void);
00204 
00205 
00206 /*
00207  * Shutdown function. This MUST be called when no other function
00208  * declared in this header file will be called anymore.
00209  */
00210 void http_shutdown(void);
00211 
00212 
00213 /***********************************************************************
00214  * HTTP URL parsing.
00215  */
00216 
00217 /*
00218  * A structure describing a full URL with it's components.
00219  */
00220 typedef struct {
00221     Octstr *url;
00222     Octstr *scheme;
00223     Octstr *host;
00224     unsigned long port;
00225     Octstr *user;
00226     Octstr *pass;
00227     Octstr *path;
00228     Octstr *query;
00229     Octstr *fragment;
00230 } HTTPURLParse;
00231 
00232 /* 
00233  * Create an URL parsing structure.
00234  */
00235 HTTPURLParse *http_urlparse_create(void);
00236 
00237 /* 
00238  * Destroy an URL parsing structure. 
00239  */
00240 void http_urlparse_destroy(HTTPURLParse *p);
00241 
00242 /* 
00243  * Parse the given URL and return a parsed struct containing all
00244  * parsed components. If parsing failed, returns NULL.
00245  */
00246 HTTPURLParse *parse_url(Octstr *url);
00247 
00248 /* 
00249  * Dump the parsed struct to debug log level. 
00250  */
00251 void parse_dump(HTTPURLParse *p);
00252 
00253 
00254 /***********************************************************************
00255  * HTTP proxy interface.
00256  */
00257 
00258 
00259 /*
00260  * Functions for controlling proxy use. http_use_proxy sets the proxy to
00261  * use; if another proxy was already in use, it is closed and forgotten
00262  * about as soon as all existing requests via it have been served.
00263  *
00264  * http_close_proxy closes the current proxy connection, after any
00265  * pending requests have been served.
00266  */
00267 void http_use_proxy(Octstr *hostname, int port, int ssl, List *exceptions,
00268                     Octstr *username, Octstr *password, Octstr *exceptions_regex);
00269 void http_close_proxy(void);
00270 
00271 
00272 /***********************************************************************
00273  * HTTP client interface.
00274  */
00275 
00276 /*
00277  * Define interface from which all http requestes will be served
00278  */
00279 void http_set_interface(const Octstr *our_host);
00280 
00285 void http_set_client_timeout(long timeout);
00286 
00287 /*
00288  * Functions for doing a GET request. The difference is that _real follows
00289  * redirections, plain http_get does not. Return value is the status
00290  * code of the request as a numeric value, or -1 if a response from the
00291  * server was not received. If return value is not -1, reply_headers and
00292  * reply_body are set and MUST be destroyed by caller.
00293  *
00294  * XXX these are going away in the future
00295  */
00296 int http_get_real(int method, Octstr *url, List *request_headers, 
00297                   Octstr **final_url, List **reply_headers, 
00298                   Octstr **reply_body);
00299 
00300 /*
00301  * An identification for a caller of HTTP. This is used with
00302  * http_start_request, and http_receive_result to route results to the right
00303  * callers.
00304  *
00305  * Implementation note: We use a List as the type so that we can use
00306  * that list for communicating the results. This makes it unnecessary
00307  * to map the caller identifier to a List internally in the HTTP module.
00308  */
00309 typedef List HTTPCaller;
00310 
00311 
00312 /*
00313  * Create an HTTP caller identifier.
00314  */
00315 HTTPCaller *http_caller_create(void);
00316 
00317 
00318 /*
00319  * Destroy an HTTP caller identifier. Those that aren't destroyed
00320  * explicitly are destroyed by http_shutdown.
00321  */
00322 void http_caller_destroy(HTTPCaller *caller);
00323 
00324 
00325 /*
00326  * Signal to a caller (presumably waiting in http_receive_result) that
00327  * we're entering shutdown phase. This will make http_receive_result
00328  * no longer block if the queue is empty.
00329  */
00330 void http_caller_signal_shutdown(HTTPCaller *caller);
00331 
00332 
00333 /*
00334  * Start an HTTP request. It will be completed in the background, and
00335  * the result will eventually be received by http_receive_result.
00336  * http_receive_result will return the id parameter passed to this function,
00337  * and the caller can use this to keep track of which request and which
00338  * response belong together. If id is NULL, it is changed to a non-null
00339  * value (NULL replies from http_receive_result are reserved for cases
00340  * when it doesn't return a reply).
00341  *
00342  * If `body' is NULL, it is a GET request, otherwise as POST request.
00343  * If `follow' is true, HTTP redirections are followed, otherwise not.
00344  *
00345  * 'certkeyfile' defines a filename where openssl looks for a PEM-encoded
00346  * certificate and a private key, if openssl is compiled in and an https 
00347  * URL is used. It can be NULL, in which case none is used and thus there 
00348  * is no ssl authentication, unless you have set a global one with
00349  * use_global_certkey_file() from conn.c.
00350  */
00351 void http_start_request(HTTPCaller *caller, int method, Octstr *url, 
00352                         List *headers, Octstr *body, int follow, void *id, 
00353                         Octstr *certkeyfile); 
00354 
00355 
00356 /*
00357  * Get the result of a GET or a POST request. Returns either the id pointer
00358  * (the one passed to http_start request if non-NULL) or NULL if
00359  * http_caller_signal_shutdown has been called and there are no queued results.
00360  */
00361 void *http_receive_result_real(HTTPCaller *caller, int *status, Octstr **final_url,
00362                          List **headers, Octstr **body, int blocking);
00363 
00364 /* old compatibility mode, always blocking */
00365 #define http_receive_result(caller, status, final_url, headers, body) \
00366     http_receive_result_real(caller, status, final_url, headers, body, 1)
00367 
00368 /***********************************************************************
00369  * HTTP server interface.
00370  */
00371 
00372 
00373 /*
00374  * Data structure representing an HTTP client that has connected to
00375  * the server we implement. It is used to route responses correctly.
00376  */
00377 typedef struct HTTPClient HTTPClient;
00378 
00379 
00380 /*
00381  * Open an HTTP server at a given port. Return -1 for errors (invalid
00382  * port number, etc), 0 for OK. This will also start a background thread
00383  * to listen for connections to that port and read the requests from them.
00384  * Second boolean variable indicates if the HTTP server should be started 
00385  * for SSL-enabled connections.
00386  */
00387 int http_open_port(int port, int ssl);
00388 
00389 
00390 /*
00391  * Same as above, but bind to a specific interface.
00392  */
00393 int http_open_port_if(int port, int ssl, Octstr *interface);
00394 
00395 
00396 /*
00397  * Accept a request from a client to the specified open port. Return NULL
00398  * if the port is closed, otherwise a pointer to a client descriptor.
00399  * Return the IP number (as a string) and other related information about
00400  * the request via arguments if function return value is non-NULL. The
00401  * caller is responsible for destroying the values returned via arguments,
00402  * the caller descriptor is destroyed by http_send_reply.
00403  *
00404  * The requests are actually read by a background thread handled by the
00405  * HTTP implementation, so it is not necessary by the HTTP user to have
00406  * many threads to be fast. The HTTP user should use a single thread,
00407  * unless requests can block.
00408  */
00409 HTTPClient *http_accept_request(int port, Octstr **client_ip, 
00410                                 Octstr **url, List **headers, Octstr **body,
00411                 List **cgivars);
00412 
00413 
00414 /*
00415  * Send a reply to a previously accepted request. The caller is responsible
00416  * for destroying the headers and body after the call to http_send_reply
00417  * finishes. This allows using them in several replies in an efficient way.
00418  */
00419 void http_send_reply(HTTPClient *client, int status, List *headers, 
00420                      Octstr *body);
00421 
00422 
00423 /*
00424  * Don't send a reply to a previously accepted request, but only close
00425  * the connection to the client. This can be used to reject requests from
00426  * clients that are not authorized to access us.
00427  */
00428 void http_close_client(HTTPClient *client);
00429 
00430 
00431 /*
00432  * Close a currently open port and stop corresponding background threads.
00433  */
00434 void http_close_port(int port);
00435 
00436 
00437 /*
00438  * Close all currently open ports and stop background threads.
00439  */
00440 void http_close_all_ports(void);
00441 
00442 
00443 /*
00444  * Destroy a list of HTTPCGIVar objects.
00445  */
00446 void http_destroy_cgiargs(List *args);
00447 
00448 
00449 /*
00450  * Return reference to CGI argument 'name', or NULL if not matching.
00451  */
00452 Octstr *http_cgi_variable(List *list, char *name);
00453 
00454 
00455 /***********************************************************************
00456  * HTTP header interface.
00457  */
00458 
00459 
00460 /*
00461  * Functions for manipulating a list of headers. You can use a list of
00462  * headers returned by one of the functions above, or create an empty
00463  * list with http_create_empty_headers. Use http_destroy_headers to
00464  * destroy a list of headers (not just the list, but the headers
00465  * themselves). You can also use http_parse_header_string to create a list:
00466  * it takes a textual representation of headers as an Octstr and returns
00467  * the corresponding List. http_generate_header_string goes the other
00468  * way.
00469  *
00470  * Once you have a list of headers, you can use http_header_add and the
00471  * other functions to manipulate it.
00472  */
00473 List *http_create_empty_headers(void);
00474 void http_destroy_headers(List *headers);
00475 void http_header_add(List *headers, char *name, char *contents);
00476 void http_header_get(List *headers, long i, Octstr **name, Octstr **value);
00477 List *http_header_duplicate(List *headers);
00478 void http_header_pack(List *headers);
00479 void http_append_headers(List *to, List *from);
00480 Octstr *http_header_value(List *headers, Octstr *header);
00481 
00482 
00483 /*
00484  * Append all headers from new_headers to old_headers.  Headers from
00485  * new_headers _replace_ the ones in old_headers if they have the same
00486  * name.  For example, if you have:
00487  * old_headers
00488  *    Accept: text/html
00489  *    Accept: text/plain
00490  *    Accept: image/jpeg
00491  *    Accept-Language: en
00492  * new_headers
00493  *    Accept: text/html
00494  *    Accept: text/plain
00495  * then after the operation, old_headers will have
00496  *    Accept-Language: en
00497  *    Accept: text/html
00498  *    Accept: text/plain
00499  */
00500 void http_header_combine(List *old_headers, List *new_headers);
00501 
00502 /*
00503  * Return the length of the quoted-string (a HTTP field element)
00504  * starting at position pos in the header.  Return -1 if there
00505  * is no quoted-string at that position.
00506  */
00507 long http_header_quoted_string_len(Octstr *header, long pos);
00508 
00509 
00510 /*
00511  * Take the value part of a header that has a format that allows
00512  * multiple comma-separated elements, and split it into a list of
00513  * those elements.  Note that the function may have surprising
00514  * results for values of headers that are not in this format.
00515  */
00516 List *http_header_split_value(Octstr *value);
00517 
00518 
00519 /*
00520  * The same as http_header_split_value, except that it splits 
00521  * headers containing 'credentials' or 'challenge' lists, which
00522  * have a slightly different format.  It also normalizes the list
00523  * elements, so that parameters are introduced with ';'.
00524  */
00525 List *http_header_split_auth_value(Octstr *value);
00526 
00527 
00528 /*
00529  * Remove all headers with name 'name' from the list.  Return the
00530  * number of headers removed.
00531  */
00532 long http_header_remove_all(List *headers, char *name);
00533 
00534 
00535 /*
00536  * Remove the hop-by-hop headers from a header list.  These are the
00537  * headers that describe a specific connection, not anything about
00538  * the content.  RFC2616 section 13.5.1 defines these.
00539  */
00540 void http_remove_hop_headers(List *headers);
00541 
00542 
00543 /*
00544  * Update the headers to reflect that a transformation has been
00545  * applied to the entity body.
00546  */
00547 void http_header_mark_transformation(List *headers, Octstr *new_body, 
00548                                      Octstr *new_type);
00549 
00550 
00551 /*
00552  * Find the first header called `name' in `headers'. Returns its contents
00553  * as a new Octet string, which the caller must free. Return NULL for
00554  * not found.
00555  */
00556 Octstr *http_header_find_first_real(List *headers, char *name, 
00557                                     const char *file, long line, const char *func);
00558 #define http_header_find_first(headers, name) \
00559     gw_claim_area(http_header_find_first_real((headers), (name), __FILE__, __LINE__, __func__))
00560 List *http_header_find_all(List *headers, char *name);
00561 
00562 
00563 /*
00564  * Find the Content-Type header and returns the type and charset.
00565  */
00566 void http_header_get_content_type(List *headers, Octstr **type, 
00567     Octstr **charset);
00568 
00569 
00570 /*
00571  * Check if a specific mime-type can be handled by a client. This is
00572  * indicated via 'Accept' headers. Returns 1 if the mime-type is acceptable,
00573  * otherwise 0.
00574  */
00575 int http_type_accepted(List *headers, char *type);
00576 
00577 
00578 /*
00579  * Dump the contents of a header list with debug.
00580  */
00581 void http_header_dump(List *headers);
00582 
00583 /* 
00584  * Ditto with cgi variables. Do not panic, when an empty are found from the 
00585  * list.
00586  */
00587 void http_cgivar_dump(List *cgiargs);
00588 
00589 /*
00590  * As above function except that dump appended to Octstr.
00591  */
00592 void http_cgivar_dump_into(List *cgiargs, Octstr *os);
00593 
00594 /*
00595  * Check if the passed charset is in the 'Accept-Charset' header list
00596  * alues of the client. Returns 1 if the charset is acceptable, otherwise 0.
00597  */
00598 int http_charset_accepted(List *headers, char *charset);
00599 
00600 
00601 /*
00602  * Add Basic Authentication headers headers.
00603  */
00604 void http_add_basic_auth(List *headers, Octstr *username, Octstr *password);
00605 
00606 
00607 /* 
00608  * Many HTTP field elements can take parameters in a standardized
00609  * form: parameters appear after the main value, each is introduced
00610  * by a semicolon (;), and consists of a key=value pair or just
00611  * a key, where the key is a token and the value is either a token
00612  * or a quoted-string.
00613  * The main value itself is a series of tokens, separators, and
00614  * quoted-strings.
00615  *
00616  * This function will take such a field element, and look for the 
00617  * value of a specific key, which is then returned. If the key
00618  * is not found within the header value NULL is returned.
00619  * 
00620  * BEWARE: value is *only* the header value, not the whole header with
00621  * field name.
00622  * 
00623  * Example:
00624  *    * assume to have "Content-Type: application/xml; charset=UTF-8" 
00625  *    * within List *headers 
00626  *   value = http_header_value(headers, octstr_imm("Content-Type"))
00627  *   val = http_get_header_parameter(value, octstr_imm("charset"));
00628  * will return "UTF-8" to lvalue.
00629  */
00630 Octstr *http_get_header_parameter(Octstr *value, Octstr *parameter);
00631 
00632 
00633 /*
00634  * Return the general class of a status code.  For example, all
00635  * 2xx codes are HTTP_STATUS_SUCCESSFUL.  See the list at the top
00636  * of this file.
00637  */
00638 int http_status_class(int code);
00639 
00640 
00641 /*
00642  * Return the HTTP_METHOD_xxx enum code for a Octstr containing 
00643  * the HTTP method name.
00644  */
00645 int http_name2method(Octstr *method);
00646 
00647 
00648 /*
00649  * Return the char containing the HTTP method name.
00650  */
00651 char *http_method2name(int method);
00652 
00653 #endif
See file LICENSE for details about the license agreement for using, modifying, copying or deriving work from this software.