Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

samples: sockets: http_get: More configurability and logging #5981

Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 19 additions & 6 deletions samples/net/sockets/http_get/src/http_get.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,25 @@

#endif

/* HTTP server to connect to */
#define HTTP_HOST "google.com"
/* Port to connect to, as string */
#define HTTP_PORT "80"
/* HTTP path to request */
#define HTTP_PATH "/"


#define SSTRLEN(s) (sizeof(s) - 1)
#define CHECK(r) { if (r == -1) { printf("Error: " #r "\n"); } }

#define REQUEST "GET / HTTP/1.0\r\n\r\n"
#define REQUEST "GET " HTTP_PATH " HTTP/1.0\r\n\r\n"

static char response[1024];

void dump_addrinfo(const struct addrinfo *ai)
{
printf("addrinfo @%p: fam=%d, socktype=%d, proto=%d, "
"addr_fam=%d, addr_port=%x\n",
printf("addrinfo @%p: ai_family=%d, ai_socktype=%d, ai_protocol=%d, "
"sa_family=%d, sin_port=%x\n",
ai, ai->ai_family, ai->ai_socktype, ai->ai_protocol,
ai->ai_addr->sa_family,
((struct sockaddr_in *)ai->ai_addr)->sin_port);
Expand All @@ -44,9 +52,12 @@ int main(void)
struct addrinfo *res;
int st, sock;

printf("Preparing HTTP GET request for http://" HTTP_HOST
":" HTTP_PORT HTTP_PATH "\n");

hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
st = getaddrinfo("google.com", "80", &hints, &res);
st = getaddrinfo(HTTP_HOST, HTTP_PORT, &hints, &res);
printf("getaddrinfo status: %d\n", st);

if (st != 0) {
Expand All @@ -68,7 +79,7 @@ int main(void)
CHECK(connect(sock, res->ai_addr, res->ai_addrlen));
send(sock, REQUEST, SSTRLEN(REQUEST), 0);

printf("Response:\n");
printf("Response:\n\n");

while (1) {
int len = recv(sock, response, sizeof(response) - 1, 0);
Expand All @@ -83,8 +94,10 @@ int main(void)
}

response[len] = 0;
printf("%s", response);
printf("%s\n", response);
}

close(sock);

return 0;
}