00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <config.h>
00021
00022 #include <stdio.h>
00023 #include <errno.h>
00024
00025 #ifdef HAVE_SYS_TYPES_H
00026 #include <sys/types.h>
00027 #endif
00028
00029 #include <sys/socket.h>
00030
00031 #ifdef HAVE_STRING_H
00032 #include <string.h>
00033 #endif
00034
00035 #ifdef HAVE_NETINET_IN_H
00036 #include <netinet/in.h>
00037 #endif
00038
00039 #ifdef HAVE_ARPA_INET_H
00040 #include <arpa/inet.h>
00041 #endif
00042
00043
00044 #include "protocol.h"
00045
00046
00047 static char *get_host_header(Port_T, char *);
00048
00070 int check_http(Port_T p) {
00071
00072 #define REQ_LENGTH 1024
00073
00074 int n;
00075 int status;
00076 char buf[STRLEN];
00077 char msg[STRLEN];
00078 char host[STRLEN];
00079 char proto[STRLEN];
00080 char request[REQ_LENGTH];
00081
00082 snprintf(request, REQ_LENGTH,
00083 "GET %s HTTP/1.1\r\n"
00084 "Host: %s\r\n"
00085 "Accept: */*\r\n"
00086 "User-Agent: %s/%s\r\n"
00087 "Connection: close\r\n\r\n",
00088 p->request?p->request:"/",
00089 get_host_header(p, host), prog, VERSION);
00090
00091 if (sock_send(p->socket, request, strlen(request), 0) < 0) {
00092 log("HTTP: error sending data -- %s\n", STRERROR);
00093 return FALSE;
00094 }
00095
00096 if (sock_recv(p->socket, buf, sizeof(buf), 0) <= 0) {
00097 log("HTTP: error receiving data -- %s\n", STRERROR);
00098 return FALSE;
00099 }
00100
00101 chomp(buf);
00102
00103 n= sscanf(buf, "%s %d %s", proto, &status, msg);
00104 if(n!=3 || (status >= 400)) {
00105 log("HTTP error: %s\n", buf);
00106 return FALSE;
00107 }
00108
00109 return TRUE;
00110
00111 }
00112
00113
00114
00115
00116
00121 static char *get_host_header(Port_T p, char *hostbuf) {
00122
00123 if ( ! strcmp(LOCALHOST, p->hostname) || inet_aton(p->hostname, NULL) ) {
00124
00125 *hostbuf= 0;
00126
00127 } else {
00128
00129 snprintf(hostbuf, STRLEN, "%s:%d", p->hostname, p->port);
00130
00131 }
00132
00133 return hostbuf;
00134
00135 }
00136