Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: tests/blocking/socket/HttpGetTest.cpp
- Revision:
- 0:836c9a6383e0
- Child:
- 1:5137ec8f4c45
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/tests/blocking/socket/HttpGetTest.cpp Mon Aug 11 11:31:32 2014 +0000 @@ -0,0 +1,133 @@ +/* + * Copyright 2014, ACKme Networks + * All Rights Reserved. + * + * This is UNPUBLISHED PROPRIETARY SOURCE CODE of ACKme Networks; + * the contents of this file may not be disclosed to third parties, copied + * or duplicated in any form, in whole or in part, without the prior + * written permission of ACKme Networks. + */ + + +#include "tests/Tests.h" +#include "Wiconnect.h" + + + +static WiconnectResult addHeadersPrompt(Wiconnect *wiconnect, Socket &socket); +static WiconnectResult readResponse(Socket &socket); + + + +/*************************************************************************************************/ +WiconnectResult socketHttpGetCommand(int argc, char **argv) +{ + WiconnectResult result; + Wiconnect *wiconnect = Wiconnect::getInstance(); + Socket socket(sizeof(testBuffer), testBuffer); + const char *url; + bool addHeaders = false; + + if(argc < 1) + { + LOG_ERROR("must specify url"); + return WICONNECT_BAD_ARG; + } + url = argv[0]; + + if(argc > 1) + { + addHeaders = true; + } + + + if(!WICONNECT_FAILED(result, wiconnect->httpGet(socket, url, addHeaders))) + { + uint32_t status; + if(addHeaders) + { + if(WICONNECT_FAILED(result, addHeadersPrompt(wiconnect, socket))) + { + goto exit; + } + else if(WICONNECT_FAILED(result, wiconnect->httpGetStatus(socket, &status))) + { + goto exit; + } + LOG_INFO("HTTP Status: %d", status); + } + + result = readResponse(socket); + } + +exit: + return result; +} + + + + +/*************************************************************************************************/ +static WiconnectResult addHeadersPrompt(Wiconnect *wiconnect, Socket &socket) +{ + WiconnectResult result; + + char buffer[128]; + for(;;) + { + LOG_INFO("Enter header 'key,value\\n' (or 'done\\n' to issue request):"); + char *ptr = buffer; + + for(;;) + { + int c = consoleSerial.getc(); + consoleSerial.putc(c); + if(c == '\r') + continue; + if(c == '\n') + { + *ptr = 0; + break; + } + *ptr++ = (char)c; + } + + if(strcmp(buffer, "done") == 0) + { + return WICONNECT_SUCCESS; + } + + char *value = strchr(buffer, ','); + if(value == NULL) + { + LOG_ERROR("Mal-formed key,value pair. Must be: <key>,<value>"); + continue; + } + + *value++ = 0; + + if(WICONNECT_FAILED(result, wiconnect->httpAddHeader(socket, buffer, value))) + { + LOG_ERROR("Failed to add header key, value"); + break; + } + } + + return result; +} + +/*************************************************************************************************/ +static WiconnectResult readResponse(Socket &socket) +{ + uint8_t c; + + LOG_INFO("Response data:"); + while(socket.getc(&c) == WICONNECT_SUCCESS) // NOTE: getc() is extremely inefficient here, + // is only used to test its functionality + { + logWrite(&c, 1); + } + + return WICONNECT_SUCCESS; +} +