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 ASSERT(p);
00083
00084 snprintf(request, REQ_LENGTH,
00085 "GET %s HTTP/1.1\r\n"
00086 "Host: %s\r\n"
00087 "Accept: */*\r\n"
00088 "User-Agent: %s/%s\r\n"
00089 "Connection: close\r\n\r\n",
00090 p->request?p->request:"/",
00091 get_host_header(p, host), prog, VERSION);
00092
00093 if(port_send(p, request, strlen(request), 0) < 0) {
00094 log("HTTP: error sending data -- %s\n", STRERROR);
00095 return FALSE;
00096 }
00097
00098 if(port_recv(p, buf, sizeof(buf), 0) <= 0) {
00099 log("HTTP: error receiving data -- %s\n", STRERROR);
00100 return FALSE;
00101 }
00102
00103 chomp(buf);
00104
00105 n= sscanf(buf, "%s %d %s", proto, &status, msg);
00106 if(n!=3 || (status >= 400)) {
00107 log("HTTP error: %s\n", buf);
00108 return FALSE;
00109 }
00110
00111 return TRUE;
00112
00113 }
00114
00115
00116
00117
00118
00123 static char *get_host_header(Port_T p, char *hostbuf) {
00124
00125 if(! strcmp(LOCALHOST, p->hostname) || inet_aton(p->hostname, NULL)) {
00126
00127 *hostbuf= 0;
00128
00129 } else {
00130
00131 snprintf(hostbuf, STRLEN, "%s:%d", p->hostname, p->port);
00132
00133 }
00134
00135 return hostbuf;
00136
00137 }
00138