* Create request parameter list
*
* @v name Parameter list name (may be NULL)
+ * @v method Method name (may be NULL)
* @ret params Parameter list, or NULL on failure
*/
-struct parameters * create_parameters ( const char *name ) {
+struct parameters * create_parameters ( const char *name,
+ const char *method ) {
struct parameters *params;
size_t name_len;
+ size_t method_len;
char *name_copy;
+ char *method_copy;
/* Destroy any existing parameter list of this name */
params = find_parameters ( name );
/* Allocate parameter list */
name_len = ( name ? ( strlen ( name ) + 1 /* NUL */ ) : 0 );
- params = zalloc ( sizeof ( *params ) + name_len );
+ method_len = ( method ? ( strlen ( method ) + 1 /* NUL */ ) : 0 );
+ params = zalloc ( sizeof ( *params ) + name_len + method_len );
if ( ! params )
return NULL;
ref_init ( ¶ms->refcnt, free_parameters );
- name_copy = ( ( void * ) ( params + 1 ) );
+ name_copy = ( ( ( void * ) params ) + sizeof ( *params ) );
+ method_copy = ( ( ( void * ) name_copy ) + name_len );
/* Populate parameter list */
if ( name ) {
strcpy ( name_copy, name );
params->name = name_copy;
}
+ if ( method ) {
+ strcpy ( method_copy, method );
+ params->method = method_copy;
+ }
INIT_LIST_HEAD ( ¶ms->entries );
/* Add to list of parameter lists */
list_add_tail ( ¶ms->list, ¶meters );
DBGC ( params, "PARAMS \"%s\" created\n", params->name );
+ if ( params->method ) {
+ DBGC ( params, "PARAMS \"%s\" method is \"%s\"\n",
+ params->name, params->method );
+ }
return params;
}
struct params_options {
/** Name */
char *name;
+ /** Method */
+ char *method;
/** Delete */
int delete;
};
static struct option_descriptor params_opts[] = {
OPTION_DESC ( "name", 'n', required_argument,
struct params_options, name, parse_string ),
+ OPTION_DESC ( "method", 'm', required_argument,
+ struct params_options, method, parse_string ),
OPTION_DESC ( "delete", 'd', no_argument,
struct params_options, delete, parse_flag ),
};
return rc;
/* Create parameter list */
- params = create_parameters ( opts.name );
+ params = create_parameters ( opts.name, opts.method );
if ( ! params )
return -ENOMEM;
const char *name;
};
-extern struct http_method http_head;
-extern struct http_method http_get;
-extern struct http_method http_post;
+/** HTTP method table */
+#define HTTP_METHODS __table ( struct http_method, "http_methods" )
+
+/** Declare an HTTP method */
+#define __http_method __table_entry ( HTTP_METHODS, 01 )
+
+extern struct http_method http_head __http_method;
+extern struct http_method http_get __http_method;
+extern struct http_method http_post __http_method;
+extern struct http_method http_put __http_method;
/******************************************************************************
*
struct list_head list;
/** Name */
const char *name;
+ /** Request method */
+ const char *method;
/** Parameters */
struct list_head entries;
};
list_for_each_entry ( (param), &(params)->entries, list )
extern struct parameters * find_parameters ( const char *name );
-extern struct parameters * create_parameters ( const char *name );
+extern struct parameters * create_parameters ( const char *name,
+ const char *method );
extern struct parameter * add_parameter ( struct parameters *params,
const char *key, const char *value,
unsigned int flags );
*/
/** HTTP HEAD method */
-struct http_method http_head = {
+struct http_method http_head __http_method = {
.name = "HEAD",
};
/** HTTP GET method */
-struct http_method http_get = {
+struct http_method http_get __http_method = {
.name = "GET",
};
/** HTTP POST method */
-struct http_method http_post = {
+struct http_method http_post __http_method = {
.name = "POST",
};
+/** HTTP PUT method */
+struct http_method http_put __http_method = {
+ .name = "PUT",
+};
+
+/**
+ * Identify HTTP method
+ *
+ * @v name Method name
+ * @ret method HTTP method, or NULL if not known
+ */
+static struct http_method * http_method ( const char *name ) {
+ struct http_method *method;
+
+ /* Identify method */
+ for_each_table_entry ( method, HTTP_METHODS ) {
+ if ( strcasecmp ( name, method->name ) == 0 )
+ return method;
+ }
+
+ return NULL;
+}
+
/******************************************************************************
*
* Utility functions
data = NULL;
}
+ /* Use explicitly requested method name if applicable */
+ if ( params && params->method ) {
+ method = http_method ( params->method );
+ if ( ! method ) {
+ DBGC ( uri, "HTTP unsupported method \"%s\"\n",
+ params->method );
+ rc = -ENOTSUP;
+ goto err_method;
+ }
+ }
+
/* Construct request content */
content.type = type;
content.data = data;
goto err_open;
err_open:
+ err_method:
free ( data );
err_alloc:
return rc;
struct uri *dup;
/* Create parameter list */
- params = create_parameters ( test->name );
+ params = create_parameters ( test->name, NULL );
okx ( params != NULL, file, line );
if ( params ) {
for ( list = test->list ; list->key ; list++ ) {