summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-11-29 17:05:31 +0100
committerAndreas Baumann <mail@andreasbaumann.cc>2014-11-29 17:05:31 +0100
commitf2eb844d53f16586b67e4d753b705838808567d9 (patch)
tree520236dc25ec6648f332a51e50ab820e37bf1f56
parent1135bfb7aa76ea4ec5cff756cc6f0f98332e9828 (diff)
downloadbiruda-f2eb844d53f16586b67e4d753b705838808567d9.tar.gz
biruda-f2eb844d53f16586b67e4d753b705838808567d9.tar.bz2
fixed POST in http_lib
changed POST request for starting workers and added proper error handling whole chain is working now
-rw-r--r--src/3rdParty/http_tiny/http_lib.c26
-rw-r--r--src/3rdParty/http_tiny/http_lib.h3
-rw-r--r--src/biruda.c17
-rw-r--r--src/webserver.c43
4 files changed, 44 insertions, 45 deletions
diff --git a/src/3rdParty/http_tiny/http_lib.c b/src/3rdParty/http_tiny/http_lib.c
index 93743a7..8bb7384 100644
--- a/src/3rdParty/http_tiny/http_lib.c
+++ b/src/3rdParty/http_tiny/http_lib.c
@@ -296,32 +296,26 @@ http_retcode http_put(filename, data, length, overwrite, type)
*
* limitations: filename is truncated to first 256 characters
* and type to 64.
+ * buf: for now we can't handle the body of the answer to a POST request
*/
-http_retcode http_post(filename, data_in, length_in, type_in, data_out, length_out, type_out)
+http_retcode http_post(filename, data, length, type)
char *filename; /* name of the ressource to create */
- char *data_in; /* pointer to the data to send */
- int length_in; /* length of the data to send */
- char *type_in; /* type of the data, if NULL default type is used */
- char **data_out; /* address of a pointer variable which will be set
- to point toward allocated memory containing read data.*/
- int *length_out;/* address of integer variable which will be set to
- length of the read data */
- char *type_out; /* allocated buffer where the read data type is returned.
- If NULL, the type is not returned */
+ char *data; /* pointer to the data to send */
+ int length; /* length of the data to send */
+ char *type; /* type of the data, if NULL default type is used */
{
char header[MAXBUF];
- if (type_in)
+ if (type)
sprintf(header,"Content-length: %d\015\012Content-type: %.64s\015\012",
- length_in,
- type_in
+ length,
+ type
);
else
- sprintf(header,"Content-length: %d\015\012",length_in
+ sprintf(header,"Content-length: %d\015\012",length
);
- return http_query("POST",filename,header,CLOSE, data_out, length_out, type_out);
+ return http_query("POST",filename,header,CLOSE, data, length, NULL);
}
-
/*
* Get data from the server
*
diff --git a/src/3rdParty/http_tiny/http_lib.h b/src/3rdParty/http_tiny/http_lib.h
index 2bc72ab..2121282 100644
--- a/src/3rdParty/http_tiny/http_lib.h
+++ b/src/3rdParty/http_tiny/http_lib.h
@@ -69,7 +69,6 @@ http_retcode http_delete(char *filename) ;
http_retcode http_head(char *filename, int *plength, char *typebuf);
-http_retcode http_post(char *filename, char *data_in, int length_in, char *type_in,
- char **data_out, int *length_out, char *type_out);
+http_retcode http_post(char *filename, char *data, int length, char *type);
#endif
diff --git a/src/biruda.c b/src/biruda.c
index 685c3ff..3c108cb 100644
--- a/src/biruda.c
+++ b/src/biruda.c
@@ -414,24 +414,13 @@ static void print_status( )
static void start_worker( const char *worker_name )
{
char url[128];
- snprintf( url, sizeof( url ), "start?op=start&type=worker&name=%s", worker_name );
- char *data = NULL;
- int len;
- http_retcode ret = http_post( url, "", 0, "Content-Type: text/plain",
- &data, &len, NULL );
+ snprintf( url, sizeof( url ), "worker?op=start&name=%s", worker_name );
+ http_retcode ret = http_post( url, "", 0, "Content-Type: text/plain" );
if( ret == 200 ) {
- if( strlen( data ) > 0 && data[len-1] == '\n' ) {
- data[len-1] = '\0';
- len--;
- }
- if( strlen( data ) > 0 && data[len-1] == '\r' ) {
- data[len-1] = '\0';
- }
- print_answer( data );
+ print_answer( "Request queued" );
} else {
print_error( "ERROR: HTTP error %d", ret );
}
- free( data );
}
static int start_interactive( bool colors )
diff --git a/src/webserver.c b/src/webserver.c
index 8093ec8..0b299ed 100644
--- a/src/webserver.c
+++ b/src/webserver.c
@@ -53,30 +53,47 @@ static int handle_request( void *cls, struct MHD_Connection *connection,
MHD_destroy_response( response );
} else if( strcmp( method, "POST" ) == 0 ) {
-
- const char *op = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "op" );
- printf( "Got POST operation '%s'\n", op );
-
- if( strcmp( op, "start" ) == 0 ) {
-
- const char *type = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "type" );
+ if( strcmp( url, "/worker" ) == 0 ) {
+
+ char biruda_msg[2048];
+ biruda_msg[0] = '\0';
- printf( "Got POST parameter for type of process '%s'\n", type );
+ const char *op = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "op" );
+
+ printf( "Got POST operation '%s'\n", op );
- if( strcmp( type, "worker" ) == 0 ) {
+ if( op != NULL && strcmp( op, "start" ) == 0 ) {
const char *name = MHD_lookup_connection_value( connection, MHD_GET_ARGUMENT_KIND, "name" );
- printf( "Got POST parameter for starting a worker with name '%s'\n", name );
+ if( name != NULL ) {
- master_start_worker( name );
+ printf( "Got POST parameter for starting a worker with name '%s'\n", name );
+
+ int res = master_start_worker( name );
+
+ if( res < 0 ) {
+ snprintf( biruda_msg, sizeof( biruda_msg ), "Queueing start request message failed\n" );
+ } else {
+ snprintf( biruda_msg, sizeof( biruda_msg ), "Queued start worker request\n" );
+ }
+
+ response = MHD_create_response_from_buffer( strlen( biruda_msg ),
+ (void *)biruda_msg, MHD_RESPMEM_MUST_COPY );
+ ret = MHD_queue_response( connection, ( res == 0 ) ? MHD_HTTP_OK : MHD_HTTP_INTERNAL_SERVER_ERROR, response );
+ MHD_destroy_response( response );
+ }
+ } else {
+ return MHD_NO;
}
+ } else {
+ return MHD_NO;
}
-
+ } else {
return MHD_NO;
}
-
+
return ret;
}