From e191b51a0849ae51b782c9809a9013c960eac392 Mon Sep 17 00:00:00 2001 From: "Kevin P. Fleming" Date: Tue, 18 Mar 2008 22:32:26 +0000 Subject: [PATCH] start the process of changing HTTP request dispatching to do it based on *both* URI and method, so that POST support can move into a module; move http.c's private function prototypes into _private.h git-svn-id: https://origsvn.digium.com/svn/asterisk/trunk@109762 65c4cc65-6c06-0410-ace0-fbb531ad65f3 --- include/asterisk/_private.h | 4 +++- include/asterisk/http.h | 30 +++++++++++++++++++----------- main/http.c | 13 ++++++++----- main/manager.c | 14 +++++++------- res/res_phoneprov.c | 5 +++-- 5 files changed, 40 insertions(+), 26 deletions(-) diff --git a/include/asterisk/_private.h b/include/asterisk/_private.h index b477d0e93c..52b7e227f0 100644 --- a/include/asterisk/_private.h +++ b/include/asterisk/_private.h @@ -33,7 +33,9 @@ int ast_device_state_engine_init(void); /*!< Provided by devicestate.c */ int astobj2_init(void); /*!< Provided by astobj2.c */ int ast_file_init(void); /*!< Provided by file.c */ int ast_features_init(void); /*!< Provided by features.c */ -void ast_autoservice_init(void); /*!< Provided by autoservice.c */ +void ast_autoservice_init(void); /*!< Provided by autoservice.c */ +int ast_http_init(void); /*!< Provided by http.c */ +int ast_http_reload(void); /*!< Provided by http.c */ /*! * \brief Reload asterisk modules. diff --git a/include/asterisk/http.h b/include/asterisk/http.h index 10e75d93e9..8dd122e3fe 100644 --- a/include/asterisk/http.h +++ b/include/asterisk/http.h @@ -65,29 +65,37 @@ content is specified) \endverbatim */ -typedef struct ast_str *(*ast_http_callback)(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength); -/*! \brief Definition of a URI reachable in the embedded HTTP server */ +enum ast_http_method { + AST_HTTP_GET = 0, + AST_HTTP_POST, +}; + +typedef struct ast_str *(*ast_http_callback)(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, + struct ast_variable *params, int *status, char **title, int *contentlength); + +/*! \brief Definition of a URI handler */ struct ast_http_uri { AST_LIST_ENTRY(ast_http_uri) entry; const char *description; const char *uri; + ast_http_callback callback; unsigned int has_subtree:1; - /*! This URI mapping serves static content */ + /*! This handler serves static content */ unsigned int static_content:1; - ast_http_callback callback; + /*! This handler accepts GET requests */ + unsigned int supports_get:1; + /*! This handler accepts POST requests */ + unsigned int supports_post:1; }; -/*! \brief Link into the Asterisk HTTP server */ +/*! \brief Register a URI handler */ int ast_http_uri_link(struct ast_http_uri *urihandler); -/*! \brief Return an ast_str malloc()'d string containing an HTTP error message */ -struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text); - -/*! \brief Destroy an HTTP server */ +/*! \brief Unregister a URI handler */ void ast_http_uri_unlink(struct ast_http_uri *urihandler); -int ast_http_init(void); -int ast_http_reload(void); +/*! \brief Return an ast_str malloc()'d string containing an HTTP error message */ +struct ast_str *ast_http_error(int status, const char *title, const char *extra_header, const char *text); #endif /* _ASTERISK_SRV_H */ diff --git a/main/http.c b/main/http.c index 13dfb5e72e..c3df5af429 100644 --- a/main/http.c +++ b/main/http.c @@ -32,8 +32,6 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") -#include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */ -#include "asterisk/network.h" #include #include #include @@ -44,6 +42,8 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include #endif /* ENABLE_UPLOADS */ +#include "asterisk/paths.h" /* use ast_config_AST_DATA_DIR */ +#include "asterisk/network.h" #include "asterisk/cli.h" #include "asterisk/tcptls.h" #include "asterisk/http.h" @@ -53,6 +53,7 @@ ASTERISK_FILE_VERSION(__FILE__, "$Revision$") #include "asterisk/stringfields.h" #include "asterisk/ast_version.h" #include "asterisk/manager.h" +#include "asterisk/_private.h" #define MAX_PREFIX 80 @@ -145,7 +146,8 @@ static const char *ftype2mtype(const char *ftype, char *wkspace, int wkspacelen) return wkspace; } -static struct ast_str *static_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength) +static struct ast_str *static_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, + struct ast_variable *vars, int *status, char **title, int *contentlength) { char *path; char *ftype; @@ -215,7 +217,8 @@ out403: } -static struct ast_str *httpstatus_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength) +static struct ast_str *httpstatus_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, + struct ast_variable *vars, int *status, char **title, int *contentlength) { struct ast_str *out = ast_str_create(512); struct ast_variable *v; @@ -641,7 +644,7 @@ static struct ast_str *handle_uri(struct ast_tcptls_session_instance *ser, char if (urih) { if (urih->static_content) *static_content = 1; - out = urih->callback(ser, uri, vars, status, title, contentlength); + out = urih->callback(ser, uri, AST_HTTP_GET, vars, status, title, contentlength); AST_RWLIST_UNLOCK(&uris); } else { out = ast_http_error(404, "Not Found", NULL, diff --git a/main/manager.c b/main/manager.c index e205eb0122..b81384b204 100644 --- a/main/manager.c +++ b/main/manager.c @@ -3476,7 +3476,7 @@ static void xml_translate(struct ast_str **out, char *in, struct ast_variable *v } static struct ast_str *generic_http_callback(enum output_format format, - struct sockaddr_in *requestor, const char *uri, + struct sockaddr_in *requestor, const char *uri, enum ast_http_method method, struct ast_variable *params, int *status, char **title, int *contentlength) { @@ -3631,19 +3631,19 @@ generic_callback_out: return out; } -static struct ast_str *manager_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) +static struct ast_str *manager_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *params, int *status, char **title, int *contentlength) { - return generic_http_callback(FORMAT_HTML, &ser->requestor, uri, params, status, title, contentlength); + return generic_http_callback(FORMAT_HTML, &ser->requestor, uri, method, params, status, title, contentlength); } -static struct ast_str *mxml_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) +static struct ast_str *mxml_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *params, int *status, char **title, int *contentlength) { - return generic_http_callback(FORMAT_XML, &ser->requestor, uri, params, status, title, contentlength); + return generic_http_callback(FORMAT_XML, &ser->requestor, uri, method, params, status, title, contentlength); } -static struct ast_str *rawman_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *params, int *status, char **title, int *contentlength) +static struct ast_str *rawman_http_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, struct ast_variable *params, int *status, char **title, int *contentlength) { - return generic_http_callback(FORMAT_RAW, &ser->requestor, uri, params, status, title, contentlength); + return generic_http_callback(FORMAT_RAW, &ser->requestor, uri, method, params, status, title, contentlength); } struct ast_http_uri rawmanuri = { diff --git a/res/res_phoneprov.c b/res/res_phoneprov.c index f5d7856439..21a440f5a8 100644 --- a/res/res_phoneprov.c +++ b/res/res_phoneprov.c @@ -323,7 +323,8 @@ static int load_file(const char *filename, char **ret) } /*! \brief Callback that is executed everytime an http request is received by this module */ -static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *ser, const char *uri, struct ast_variable *vars, int *status, char **title, int *contentlength) +static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *ser, const char *uri, enum ast_http_method method, + struct ast_variable *vars, int *status, char **title, int *contentlength) { struct http_route *route; struct http_route search_route = { @@ -358,7 +359,7 @@ static struct ast_str *phoneprov_callback(struct ast_tcptls_session_instance *se } ast_strftime(buf, sizeof(buf), "%a, %d %b %Y %H:%M:%S %Z", ast_localtime(&tv, &tm, "GMT")); - fprintf(ser->f, "HTTP/1.1 200 OK\r\n" + fprintf(ser->f, "HTTP/1.1 200 OK\r\n" "Server: Asterisk/%s\r\n" "Date: %s\r\n" "Connection: close\r\n"