mirror of https://github.com/ARMmbed/mbed-os.git
tcpclient test: cheanged the way we ask server for file content (we will now ask for full path, not relative one) so we avoid 'File moved' HTTP 301 return code.
parent
70d2f16ca3
commit
0e51149ecf
|
@ -4,13 +4,18 @@
|
|||
#include "test_env.h"
|
||||
|
||||
namespace {
|
||||
const char *HTTP_SERVER_NAME = "http://developer.mbed.org";
|
||||
// Test connection information
|
||||
const char *HTTP_SERVER_NAME = "developer.mbed.org";
|
||||
const char *HTTP_SERVER_FILE_PATH = "/media/uploads/mbed_official/hello.txt";
|
||||
const int HTTP_SERVER_PORT = 80;
|
||||
const int RECV_BUFFER_SIZE = 512;
|
||||
|
||||
// Test related data
|
||||
const char *HTTP_OK_STR = "200 OK";
|
||||
const char *HTTP_HELLO_STR = "Hello world!";
|
||||
|
||||
// Test buffers
|
||||
char buffer[RECV_BUFFER_SIZE] = {0};
|
||||
}
|
||||
|
||||
bool find_substring(const char *first, const char *last, const char *s_first, const char *s_last) {
|
||||
|
@ -19,39 +24,54 @@ bool find_substring(const char *first, const char *last, const char *s_first, co
|
|||
}
|
||||
|
||||
int main() {
|
||||
bool result = false;
|
||||
EthernetInterface eth;
|
||||
eth.init(); //Use DHCP
|
||||
eth.connect();
|
||||
printf("TCP client IP Address is %s\n", eth.getIPAddress());
|
||||
printf("TCP client IP Address is %s\r\n", eth.getIPAddress());
|
||||
|
||||
TCPSocketConnection sock;
|
||||
sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT);
|
||||
if (sock.connect(HTTP_SERVER_NAME, HTTP_SERVER_PORT) == 0) {
|
||||
printf("HTTP: Connected to %s:%d\r\n", HTTP_SERVER_NAME, HTTP_SERVER_PORT);
|
||||
|
||||
char http_cmd[] = "GET /media/uploads/mbed_official/hello.txt HTTP/1.0\n\n";
|
||||
sock.send_all(http_cmd, sizeof(http_cmd));
|
||||
// We are constructing GET command like this:
|
||||
// GET http://developer.mbed.org/media/uploads/mbed_official/hello.txt HTTP/1.0\n\n
|
||||
strcpy(buffer, "GET http://");
|
||||
strcat(buffer, HTTP_SERVER_NAME);
|
||||
strcat(buffer, HTTP_SERVER_FILE_PATH);
|
||||
strcat(buffer, " HTTP/1.0\n\n");
|
||||
// Send GET command
|
||||
sock.send_all(buffer, strlen(buffer));
|
||||
|
||||
char buffer[RECV_BUFFER_SIZE] = {0};
|
||||
bool result = true;
|
||||
while (true) {
|
||||
const int ret = sock.receive(buffer, sizeof(buffer) - 1);
|
||||
if (ret <= 0)
|
||||
break;
|
||||
buffer[ret] = '\0';
|
||||
// Server will respond with HTTP GET's success code
|
||||
bool found_200_ok = false;
|
||||
{
|
||||
const int ret = sock.receive(buffer, sizeof(buffer) - 1);
|
||||
buffer[ret] = '\0';
|
||||
// Find 200 OK HTTP status in reply
|
||||
found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
|
||||
printf("HTTP: Received %d chars from server\r\n", ret);
|
||||
printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
|
||||
printf("HTTP: Received massage:\r\n\r\n");
|
||||
printf("%s", buffer);
|
||||
}
|
||||
|
||||
// Find 200 OK HTTP status in reply
|
||||
bool found_200_ok = find_substring(buffer, buffer + ret, HTTP_OK_STR, HTTP_OK_STR + strlen(HTTP_OK_STR));
|
||||
result = result && found_200_ok;
|
||||
// Server will respond with requested file content
|
||||
bool found_hello = false;
|
||||
{
|
||||
const int ret = sock.receive(buffer, sizeof(buffer) - 1);
|
||||
buffer[ret] = '\0';
|
||||
// Find Hello World! in reply
|
||||
found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
|
||||
printf("HTTP: Received %d chars from server\r\n", ret);
|
||||
printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
|
||||
printf("HTTP: Received massage:\r\n\r\n");
|
||||
printf("%s", buffer);
|
||||
}
|
||||
|
||||
// Find Hello World! in reply
|
||||
bool found_hello = find_substring(buffer, buffer + ret, HTTP_HELLO_STR, HTTP_HELLO_STR + strlen(HTTP_HELLO_STR));
|
||||
result = result && found_hello;
|
||||
|
||||
// Print results
|
||||
printf("HTTP: Received %d chars from server\r\n", ret);
|
||||
printf("HTTP: Received 200 OK status ... %s\r\n", found_200_ok ? "[OK]" : "[FAIL]");
|
||||
printf("HTTP: Received '%s' status ... %s\r\n", HTTP_HELLO_STR, found_hello ? "[OK]" : "[FAIL]");
|
||||
printf("HTTP: Received massage:\r\n\r\n");
|
||||
printf("%s", buffer);
|
||||
if (found_200_ok && found_hello) {
|
||||
result = true;
|
||||
}
|
||||
}
|
||||
|
||||
sock.close();
|
||||
|
|
Loading…
Reference in New Issue