503 - Service temporarily unavailable - c

I need simple HTTP client on Raspberry PI zero with Raspbian. I used few example codes, but when i send about 7 requests then i can download just page with this error:
503 Service temporarily unavailable
There is no available fastcgi process to fullfill your request.
One of used codes:
#include <arpa/inet.h>
#include <assert.h>
#include <errno.h>
#include <netinet/in.h>
#include <signal.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <netdb.h>
#include <unistd.h>
#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB 200
ssize_t process_http(int sockfd, char *host, char *page)
{
ssize_t n;
snprintf(sendline, MAXSUB,
"GET %s\r\n"
"Host: %s\r\n"
"Connection: close\n"
"\n", page, host);
write(sockfd, sendline, strlen(sendline));
while ((n = read(sockfd, recvline, MAXLINE)) > 0)
{
recvline[n] = '\0';
}
printf("%s", recvline);
return n;
}
and in main is this:
int sockfd;
struct sockaddr_in servaddr;
char **pptr;
char *hname = "plankter.cz";
char *page = "http://plankter.cz/iot/list.json";
char str[50];
struct hostent *hptr;
if ((hptr = gethostbyname(hname)) == NULL) {
fprintf(stderr, " gethostbyname error for host: %s: %s",
hname, hstrerror(h_errno));
exit(1);
}
printf("hostname: %s\n", hptr->h_name);
if (hptr->h_addrtype == AF_INET
&& (pptr = hptr->h_addr_list) != NULL) {
printf("address: %s\n",
inet_ntop(hptr->h_addrtype, *pptr, str,
sizeof(str)));
} else {
fprintf(stderr, "Error call inet_ntop \n");
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
bzero(&servaddr, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
inet_pton(AF_INET, str, &servaddr.sin_addr);
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
process_http(sockfd, hname, page);
close(sockfd);
In this example i download json, but when i read php or txt, i have same problem. I tried some another example codes using sockets, example with happyhttp library and all give me same result after 7 requests. I think it doesn't close connection. I need to send http request and recieve data, and i need to do it few times in minute.
Thanks for all ideas.

You provide a full URI to the GET field:
char *page = "http://plankter.cz/iot/list.json";
...
snprintf(sendline, MAXSUB,
"GET %s\r\n"
"Host: %s\r\n"
"Connection: close\n"
"\n", page, host);
You should not include protocol and host name.
Try this instead:
char *page = "/iot/list.json";

Related

C Socket does not connect and returns exit value of 255

I have a piece of C code that should connect to www.google.com and make a HTTP GET request, but when I run it, it stays on "Connecting.." for about 30 seconds before returning "Connection Failed" and an exit return value of 255. What am I doing wrong?
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PORT 8000
struct hostent *hostinfo;
int main(void) {
int sock = 0, valread;
struct sockaddr_in serv_addr;
char *hostname = "www.google.com";
char *request = "GET / HTTP/1.1\r\nHost: www.google.com\r\n\r\n";
hostinfo = gethostbyname(hostname);
char *ip = inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[0]);
char buffer[1024] = {0};
printf("Creating socket...\n");
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
printf("Checking address...\n");
if(inet_pton(AF_INET, ip, &serv_addr.sin_addr) <= 0){
printf("\n Invalid IP/Address not supported \n");
return -1;
}
printf("Connecting to host %s...\n", ip);
if(connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
printf("\n Connection Failed \n");
return -1;
}
send(sock, request, strlen(request), 0);
printf("Message sent\n");
valread = read(sock, buffer, 1024);
printf("%s\n", buffer);
return 0;
}
I see two major problems.
You use the wrong port. Use port 80 for http.
Your read and printf is a dangerous combination that could easily cause access out of bounds (and undefined behavior). What you read from the socket will not be null terminated. You could instead do something like this:
...
printf("Message sent\n");
while((valread = read(sock, buffer, sizeof(buffer))) > 0) {
fwrite(buffer, valread, 1, stdout);
}
This will however block when everything has been read. See non-blocking I/O or consider using select, epoll or poll to wait for available data on sockets.
If you are only interested in getting the response and then disconnect, you could however use Connection: close to close the connection after the server has sent the response. Full code below:
#include <stdio.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#define PORT 80
int main(void) {
int sock = 0, valread;
struct hostent *hostinfo;
struct sockaddr_in serv_addr;
const char *hostname = "www.google.com";
const char *request = "GET / HTTP/1.1\r\n"
"Host: www.google.com\r\n"
"Connection: close\r\n\r\n"; // <- added
hostinfo = gethostbyname(hostname);
char *ip = inet_ntoa(*(struct in_addr*)hostinfo->h_addr_list[0]);
char buffer[1024] = {0};
printf("Creating socket...\n");
if((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0){
printf("\n Socket creation error \n");
return -1;
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(PORT);
printf("Checking address...\n");
if(inet_pton(AF_INET, ip, &serv_addr.sin_addr) <= 0){
printf("\n Invalid IP/Address not supported \n");
return -1;
}
printf("Connecting to host %s...\n", ip);
if(connect(sock, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0){
perror("connect()");
return -1;
}
send(sock, request, strlen(request), 0);
printf("Message sent\n");
while((valread = read(sock, buffer, sizeof(buffer))) > 0) {
fwrite(buffer, valread, 1, stdout);
}
}

How can I bypass websites that block direct connections?

I'm trying to access the website https://www.000webhost.com with C sockets:
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
int main(int argc, char** argv) {
struct sockaddr_in servaddr;
struct hostent *hp;
int sock_id;
char message[1024*1024];
char request[] = "GET / HTTP/1.1\n" "From: ...\n";
//get a socket
if((sock_id = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
fprintf(stderr,"Couldn't get a socket.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got a socket.\n");
}
memset(&servaddr,0,sizeof(servaddr));
//get address
if((hp = gethostbyname("000webhost.com")) == NULL) {
fprintf(stderr,"Couldn't get an address.\n");
exit(EXIT_FAILURE);
}else {
fprintf(stderr,"Got an address.\n");
}
memcpy((char *)&servaddr.sin_addr.s_addr, (char *)hp->h_addr, hp->h_length);
//port number and type
servaddr.sin_port = htons(80);
servaddr.sin_family = AF_INET;
//connect
if(connect(sock_id, (struct sockaddr *)&servaddr, sizeof(servaddr)) != 0) {
fprintf(stderr, "Couldn't connect.\n");
}else {
fprintf(stderr,"Got a connection.\n");
}
//request
write(sock_id,request,strlen(request));
//response
read(sock_id,message,1024*1024);
fprintf(stdout,"%s",message);
return 0;
}
If I change the request[] array from "GET / HTTP/1.1\n" "From: ...\n" to "GET / HTTP/1.1\n" "Host: https://www.000webhost.com" "From: ...\n" (therefore removing the direct-IP adress from the request), I still get the error Error 1003. Direct IP access not allowed. Is there some other part of the request that I need to modify? What else do I need to do?
Your request is malformed:
Each line needs to be terminated by \r\n, not just \n. (Some web servers will let you get away with \n, but I have no idea whether that'll work on 000webhost.)
Every header needs a \r\n after it. The modified code you mention in the last paragraph is missing a \r\n at the end of the Host header.
The Host header needs to just be a hostname (e.g, "Host: example.com"). Don't include http://.

Simple c++ web server sending wrong headers

I have very simple web server:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <err.h>
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: Keep-Alive\r\n"
"Server: michal\r\n"
"Vary: Accept-Encoding\r\n"
"Keep-Alive: timeout=5, max=100\r\n\r\n"
"<html><body><h1>It works!</h1>"
"<p>This is the default web page for this server.</p>"
"<p>The web server software is running but no content has been added, yet.</p>"
"</body></html>\r\n";
int main()
{
int one = 1, client_fd;
struct sockaddr_in svr_addr, cli_addr;
socklen_t sin_len = sizeof(cli_addr);
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
err(1, "can't open socket");
setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(int));
int port = 8080;
svr_addr.sin_family = AF_INET;
svr_addr.sin_addr.s_addr = INADDR_ANY;
svr_addr.sin_port = htons(port);
if (bind(sock, (struct sockaddr *) &svr_addr, sizeof(svr_addr)) == -1) {
close(sock);
err(1, "Can't bind");
}
listen(sock, 5);
while (1) {
client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len);
printf("got connection\n");
if (client_fd == -1) {
perror("Can't accept");
continue;
}
write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
close(client_fd);
}
}
This works in browser, the page is rendering correctly but when I am doing ab test I have error:
Benchmarking localhost (be patient)...apr_poll: The timeout specified has expired (70007)
Total of 2 requests completed
ab works fine when I am benchmarking localhost (Apache)
When I tried to download page with php file_get_contents I have following error:
PHP Notice: file_get_contents(): send of 2 bytes failed with errno=32 Broken pipe in /home/mitch/Dokumenty/projects/cpp/webserver/webserver/bench.php on line 7
What is wrong ?
My guess is that you are terminating connection before client has a chance to send all the request headers it wants.
So adding delay before close, or actually reading data before empty line is received would help.
Here is similar problem explained: https://stackoverflow.com/a/6269273/250944 (p.2)
Headers is ok, but you should read something from client first and only then write response, here is diff for your code snippet with added code for reading data:
## -8,6 +8,8 ##
#include <arpa/inet.h>
#include <err.h>
+#define MAXMSG 16384
+
char response[] = "HTTP/1.1 200 OK\r\n"
"Content-Type: text/html; charset=UTF-8\r\n"
"Connection: Keep-Alive\r\n"
## -24,6 +26,8 ## int main()
int one = 1, client_fd;
struct sockaddr_in svr_addr, cli_addr;
socklen_t sin_len = sizeof(cli_addr);
+ char read_buffer[MAXMSG];
+ int nbytes;
int sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0)
## -50,6 +54,13 ## int main()
perror("Can't accept");
continue;
}
+ nbytes = read (client_fd, read_buffer, MAXMSG);
+
+ if (nbytes < 0) {
+ perror("Can't read");
+ close(client_fd);
+ continue;
+ }
write(client_fd, response, sizeof(response) - 1); /*-1:'\0'*/
close(client_fd);

TCP client fetches HTML in C socket

I'm trying to write a TCP client that fetches HTML. The program would accept a website from user and print out the content. Right now my code only fetches a HTML back saying error 408 request timeout error page.
Where is the problem?
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <errno.h>
#include <arpa/inet.h>
int main(int argc, char* argv[])
{
char *domain = argv[1];
char *path = strchr(domain, '/');
*path++ = '\0';
//printf("host: %s; path: %s\n", domain, path);
int sock, bytes_recieved;
char send_data[1024],recv_data[9999];
struct sockaddr_in server_addr;
struct hostent *he;
he = gethostbyname(domain);
if (he == NULL){
herror("gethostbyname");
exit(1);
}
if ((sock = socket(AF_INET, SOCK_STREAM, 0))== -1){
perror("Socket");
exit(1);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(80);
server_addr.sin_addr = *((struct in_addr *)he->h_addr);
bzero(&(server_addr.sin_zero),8);
if (connect(sock, (struct sockaddr *)&server_addr,sizeof(struct sockaddr)) == -1){
perror("Connect");
exit(1);
}
snprintf(send_data, sizeof(send_data), "GET /%s HTTP/1.1\r\n Host: %s\r\n \r\n \r\n", path, domain);
//printf("%s\n", send_data);
send(sock, send_data, strlen(send_data), 0);
printf("Data sended.\n");
bytes_recieved = recv(sock, recv_data, 9999, 0);
recv_data[bytes_recieved] = '\0';
close(sock);
printf("Data reveieved.\n");
printf("%s\n", recv_data);
return 0;
}
For example, right now if I'm trying to run ./client www.facebook.com It would return a HTML page says error occurs
Check your HTTP Get request, it should be
GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n

Socket programming in C using http post

Want to do client-server programming using c in windows7, it should send string to server using http POST method. The paramater in POST method should include the ip-address etc:
I got this code from http://souptonuts.sourceforge.net/code/http_post.c.html and changed it for running it on windows, but still 1 error is coming:
#ifdef WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#include <windows.h>
#else
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <sys/wait.h>
#include <netdb.h>
#include <assert.h>
#endif
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <signal.h>
#include <ctype.h>
#define SA struct sockaddr
#define MAXLINE 4096
#define MAXSUB 200
#define LISTENQ 1024
extern int h_errno;
ssize_t process_http(int sockfd, char *host, char *page, char *poststr)
{
char sendline[MAXLINE + 1], recvline[MAXLINE + 1];
ssize_t n;
snprintf(sendline, MAXSUB,
"POST %s HTTP/1.0\r\n"
"Host: %s\r\n"
"Content-type: application/x-www-form-urlencoded\r\n"
"Content-length: %d\r\n\r\n"
"%s", page, host, strlen(poststr), poststr);
write(sockfd, sendline, strlen(sendline));
while ((n = read(sockfd, recvline, MAXLINE)) > 0) {
recvline[n] = '\0';
printf("%s", recvline);
}
return n;
}
int main(void)
{
int sockfd;
struct sockaddr_in servaddr;
char **pptr;
//********** You can change. Puy any values here *******
char *hname = "souptonuts.sourceforge.net";
char *page = "/chirico/test.php";
char *poststr = "mode=login&user=test&password=test\r\n";
//*******************************************************
char str[50];
struct hostent *hptr;
if ((hptr = gethostbyname(hname)) == NULL) {
fprintf(stderr, " gethostbyname error for host: %s: %s",
hname, hstrerror(h_errno));
exit(1);
}
printf("hostname: %s\n", hptr->h_name);
if (hptr->h_addrtype == AF_INET
&& (pptr = hptr->h_addr_list) != NULL) {
printf("address: %s\n",
inet_ntop(hptr->h_addrtype, *pptr, str,
sizeof(str)));
} else {
fprintf(stderr, "Error call inet_ntop \n");
}
sockfd = socket(AF_INET, SOCK_STREAM, 0);
// bzero(&servaddr, sizeof(servaddr));
memset(&servaddr, 0, sizeof(servaddr));
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(80);
inet_pton(AF_INET, str, &servaddr.sin_addr);
connect(sockfd, (SA *) & servaddr, sizeof(servaddr));
process_http(sockfd, hname, page, poststr);
close(sockfd);
exit(0);
}
The error which is coming on MinGW compiler is:
httppost.c:33:12: error: conflicting types for 'WSAGetLastError'
In file included from httppost.c:5:0:
c:\mingw\bin\../lib/gcc/mingw32/4.7.2/../../../../include/winsock2.h:594:32: n
e: previous declaration of 'WSAGetLastError' was here
The code you've got is under linux based systems, but in MinGW (Windows) unfortunately the identifier h_errno is taken before.
The problem is this line
extern int h_errno;
it's defined previously in windows header files, then you can not use it:
#define h_errno WSAGetLastError()
Just use another identifier instead of h_errno, or even just remove that line!
Maybe you should try the wininet library.
http://msdn.microsoft.com/en-us/library/windows/desktop/aa383630(v=vs.85).aspx

Resources