diff --git a/kitsune/commands.c b/kitsune/commands.c index b28d8bdb..2f59b501 100644 --- a/kitsune/commands.c +++ b/kitsune/commands.c @@ -1984,6 +1984,15 @@ int cmd_confidence(int argc, char *argv[]); int cmd_pwr_speaker(int argc, char * argv[]); +int cmd_http_put(int argc, char *argv[]) { + char output[128] = {0}; + hlo_stream_t * post = hlo_http_put(argv[2], argv[1] ); + hlo_stream_transfer_all( INTO_STREAM, post, argv[3], strlen(argv[3]), 2); + hlo_stream_transfer_all( FROM_STREAM, post, output, sizeof(output), 2); + + LOGF("%s\n", output); + return 0; +} int cmd_button(int argc, char *argv[]) { LOGF("%d\n", check_button()); return 0; @@ -2003,6 +2012,7 @@ int cmd_flipped(int argc, char * argv[]){ // ============================================================================== tCmdLineEntry g_sCmdTable[] = { // { "cpu", Cmd_cpu, "Show CPU utilization" }, + { "hp", cmd_http_put, " " }, { "b", cmd_button, " " }, { "v", cmd_vol, " " }, { "getv", cmd_get_vol, " " }, diff --git a/kitsune/hlo_http.c b/kitsune/hlo_http.c index 459aad89..0b77368a 100644 --- a/kitsune/hlo_http.c +++ b/kitsune/hlo_http.c @@ -469,7 +469,8 @@ typedef struct{ }hlo_http_context_t; typedef enum{ GET, - POST + POST, + PUT }http_method; //==================================================================== //common functions @@ -558,6 +559,14 @@ static int _generate_header(char * output, size_t output_size, http_method metho "Transfer-Encoding: chunked\r\n\r\n", endpoint, host, content_type, hex_device_id, KIT_VER, get_top_version()); break; + case PUT: + usnprintf(output, output_size, + "PUT %s HTTP/1.1\r\n" + "Host: %s\r\n" + "Content-type: %s\r\n" + "Transfer-Encoding: chunked\r\n\r\n", + endpoint, host, content_type); + break; } return ustrlen(output); } @@ -572,6 +581,7 @@ static int _get_content(void * ctx, void * buf, size_t size){ if( !session->response_active ){ return HLO_STREAM_EOF; } + memset(session->scratch, 0, SCRATCH_SIZE); int ndata = hlo_stream_read(session->sockstream, session->scratch, bytes_to_process); if( ndata > 0 ){ while(session->response_active && ndata) { @@ -587,8 +597,8 @@ static int _get_content(void * ctx, void * buf, size_t size){ int content_size = (session->content_itr - (char*)buf); session->content_itr = NULL; /* need to clean up here to prevent cached buffer tampering */ if( http_iserror(&session->rt) ) { - DISP("Has error\r\n"); - return HLO_STREAM_ERROR; + LOGE("Has error\r\n"); + return content_size; } else if(content_size == 0 && !session->response_active){ LOGI("GET EOF %d bytes\r\n", session->len); return HLO_STREAM_EOF; @@ -791,7 +801,7 @@ static int _get_post_response(void * ctx, void * buf, size_t size){ } return _get_content(ctx,buf,size); } -hlo_stream_t * hlo_http_post_opt(hlo_stream_t * sock, const char * host, const char * endpoint, const char * content_type_str){ +hlo_stream_t * hlo_http_upload_opt(hlo_stream_t * sock, const char * host, const char * endpoint, const char * content_type_str, http_method method){ hlo_stream_vftbl_t functions = (hlo_stream_vftbl_t){ .write = _post_content_with_header, .read = _get_post_response, @@ -802,7 +812,7 @@ hlo_stream_t * hlo_http_post_opt(hlo_stream_t * sock, const char * host, const c return NULL; }else{ hlo_http_context_t * session = (hlo_http_context_t*)ret->ctx; - int len = _generate_header(session->scratch, sizeof(session->scratch), POST, host, endpoint, content_type_str); + int len = _generate_header(session->scratch, sizeof(session->scratch), method, host, endpoint, content_type_str); if( len > 0 ){ DISP("caching header\r\n"); session->header_cache = pvPortMalloc(len + 1); @@ -815,21 +825,34 @@ hlo_stream_t * hlo_http_post_opt(hlo_stream_t * sock, const char * host, const c } return ret; } -//==================================================================== -//User Friendly post -// -hlo_stream_t * hlo_http_post(const char * url, const char * content_type){ + +hlo_stream_t * hlo_http_post_opt(hlo_stream_t * sock, const char * host, const char * endpoint, const char * content_type_str ){ + return hlo_http_upload_opt(sock, host, endpoint, content_type_str, POST); +} + +hlo_stream_t * hlo_http_upload(const char * url, const char * content_type, http_method method){ url_desc_t desc; if(0 == parse_url(&desc,url)){ const char * type = content_type ? content_type:"application/octet-stream"; - return hlo_http_post_opt( + return hlo_http_upload_opt( hlo_sock_stream(desc.host, (desc.protocol == HTTP)?0:1), desc.host, desc.path, - type + type, + method ); }else{ LOGE("Malformed URL %s\r\n", url); } return NULL; } +//==================================================================== +//User Friendly post / put +// +hlo_stream_t * hlo_http_post(const char * url, const char * content_type){ + return hlo_http_upload(url, content_type, POST); +} +hlo_stream_t * hlo_http_put(const char * url, const char * content_type){ + return hlo_http_upload(url, content_type, PUT); +} + diff --git a/kitsune/hlo_http.h b/kitsune/hlo_http.h index 8b02b874..5dd1254c 100644 --- a/kitsune/hlo_http.h +++ b/kitsune/hlo_http.h @@ -33,3 +33,4 @@ hlo_stream_t * hlo_http_get(const char * url); * half duplex, open -> write all -> read response until eof -> write all ... */ hlo_stream_t * hlo_http_post(const char * url, const char * content_type); +hlo_stream_t * hlo_http_put(const char * url, const char * content_type); diff --git a/kitsune/tinyhttp b/kitsune/tinyhttp index 9abdeffb..a524562a 160000 --- a/kitsune/tinyhttp +++ b/kitsune/tinyhttp @@ -1 +1 @@ -Subproject commit 9abdeffbd5b65ac74ee0c6300b35d0876b40a25a +Subproject commit a524562a5318457d63ec21488c4ccc54202c0436