echoping/SRC/http.c

150 lines
3.7 KiB
C
Raw Normal View History

2000-10-05 13:27:15 +00:00
/* $Id$ */
2000-04-13 09:19:23 +00:00
#include "echoping.h"
#ifdef HTTP
2000-04-13 09:19:23 +00:00
#include "HTParse.h"
2007-03-04 21:35:27 +00:00
char big_recvline[MAXTOREAD];
2000-04-13 09:19:23 +00:00
2007-03-04 21:35:27 +00:00
char *
make_http_sendline(char *url, char *host, int port, int nocache)
2000-04-13 09:19:23 +00:00
{
2007-03-04 21:35:27 +00:00
short sport = (short) port;
int size = 200; /* Enough? */
char *sendline = (char *) malloc(size);
char *hostname = (char *) malloc(size);
char *cache_directive = "";
2000-04-13 09:19:23 +00:00
#ifdef HTTP10
2007-03-04 21:35:27 +00:00
if (nocache)
cache_directive = "Pragma: no-cache\r\n"; /* RFC 1945,
* "Hypertext
* Transfer Protocol
* -- HTTP/1.0" */
2007-03-04 21:35:27 +00:00
sprintf(sendline,
"GET %s HTTP/1.0\r\nUser-Agent: Echoping/%s\r\n%s\r\n",
url, VERSION, cache_directive);
2000-04-13 09:19:23 +00:00
#else
2007-03-04 21:35:27 +00:00
if (nocache) {
if (nocache == 1)
cache_directive = "Cache-control: max-age=0\r\n"; /* Simply
* force
* a
* recheck
* with
* the
* server
*/
2007-03-04 21:35:27 +00:00
else
cache_directive = "Cache-control: no-cache\r\n"; /* RFC
* 2616
* "Hypertext
* Transfer
* Protocol --
2007-03-04 21:35:27 +00:00
* HTTP/1.1" */
}
strcpy(hostname, HTParse(url, "", PARSE_HOST));
if (!strcmp(hostname, ""))
sprintf(hostname, "%s:%d", host, sport);
sprintf(sendline,
"GET %s HTTP/1.1\r\nUser-Agent: Echoping/%s\r\nHost: %s\r\nConnection: close\r\n%s\r\n",
url, VERSION, hostname, cache_directive);
free(hostname);
2000-04-13 09:19:23 +00:00
#endif
2007-03-04 21:35:27 +00:00
return sendline;
2000-04-13 09:19:23 +00:00
}
int
2007-03-04 21:35:27 +00:00
read_from_server(CHANNEL fs, short ssl, boolean accept_redirects)
2000-04-13 09:19:23 +00:00
{
2007-03-04 21:35:27 +00:00
int nr = 0;
int total = 0;
char reply_code;
int first_line = TRUE;
short body = FALSE;
#ifdef OPENSSL
2007-03-04 21:35:27 +00:00
int sslcode;
#endif
2007-03-04 21:35:27 +00:00
while (!body && !timeout_flag) {
if (!ssl)
nr = readline(fs.fs, big_recvline, MAXTOREAD, TRUE);
2000-11-04 12:36:37 +00:00
#ifdef OPENSSL
2007-03-04 21:35:27 +00:00
else {
nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, TRUE);
if (nr == -1) {
sslcode = ERR_get_error();
err_ret("SSL_readline error: %s",
ERR_error_string(sslcode, NULL));
}
}
2000-11-04 12:36:37 +00:00
#endif
#ifdef GNUTLS
2007-03-04 21:35:27 +00:00
else
{
nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, TRUE);
if (nr == -1) {
err_ret("TLS_readline error: %s",
gnutls_strerror(nr));
2007-03-04 21:35:27 +00:00
}
}
#endif
/*
2007-03-04 21:35:27 +00:00
* printf ("DEBUG: reading \"%s\"\n (%d chars)\n",
* big_recvline, nr);
*/
/*
2007-03-04 21:35:27 +00:00
* HTTP replies should be separated by CR-LF. Unfortunately,
* some servers send only CR :-(
*/
body = ((nr == 2) || (nr == 1)); /* Empty line CR-LF seen */
if ((nr < 1) && (timeout_flag)) /* Probably a timeout */
return -1;
if (nr < 1)
/* SourceForge bug #109385 */
/* err_sys ("Error reading HTTP header"); */
return -1;
/*
2007-03-04 21:35:27 +00:00
* if ((int) big_recvline[nr-1] == 10) nr--;
*/
if (first_line) {
reply_code = big_recvline[9]; /* 9 because "HTTP/1.x
* 200..." */
if (reply_code != '2'
&& !(reply_code == '3' && accept_redirects))
/*
2007-03-04 21:35:27 +00:00
* Status codes beginning with 3 are not
* errors See bug #850674 and RFC 2616,
* section 10.3
*/
err_quit("HTTP error \"%s\"", big_recvline);
}
total = total + nr;
first_line = FALSE;
2000-04-13 09:19:23 +00:00
}
2007-03-04 21:35:27 +00:00
/* Read the body */
if (!ssl)
nr = readline(fs.fs, big_recvline, MAXTOREAD, FALSE);
2000-11-04 12:36:37 +00:00
#ifdef OPENSSL
2007-03-04 21:35:27 +00:00
else
nr = SSL_readline(fs.ssl, big_recvline, MAXTOREAD, FALSE);
#endif
#ifdef GNUTLS
2007-03-04 21:35:27 +00:00
else
nr = TLS_readline(fs.tls, big_recvline, MAXTOREAD, FALSE);
2000-11-04 12:36:37 +00:00
#endif
/*
2007-03-04 21:35:27 +00:00
* printf ("DEBUG: reading body \"%s\"\n (%d chars)\n", big_recvline,
* nr);
*/
if ((nr < 2) && (timeout_flag)) /* Probably a timeout */
return -1;
if (nr < 2) /* Hmm, if the body is empty, we'll get a *
2007-03-04 21:35:27 +00:00
* meaningless error message */
err_sys("Error reading HTTP body");
total = total + nr;
return total; /* How to do if we want only the body's size? */
2000-04-13 09:19:23 +00:00
}
2007-03-04 21:35:27 +00:00
#endif /* HTTP */