44 |
#include "protocol.h" |
#include "protocol.h" |
45 |
|
|
46 |
/* Private prototypes */ |
/* Private prototypes */ |
47 |
static char *get_host_header(Port_T, char *); |
static char *get_host_header(Socket_T, char *); |
48 |
|
|
49 |
|
|
50 |
/** |
/** |
51 |
* A simple HTTP test. |
* A simple HTTP test. |
68 |
* |
* |
69 |
* @file |
* @file |
70 |
*/ |
*/ |
71 |
int check_http(Port_T p) { |
int check_http(Socket_T s) { |
72 |
|
|
73 |
#define REQ_LENGTH 1024 |
#define REQ_LENGTH 1024 |
74 |
|
|
78 |
char msg[STRLEN]; |
char msg[STRLEN]; |
79 |
char host[STRLEN]; |
char host[STRLEN]; |
80 |
char proto[STRLEN]; |
char proto[STRLEN]; |
|
char request[REQ_LENGTH]; |
|
|
|
|
|
ASSERT(p); |
|
|
|
|
|
snprintf(request, REQ_LENGTH, |
|
|
"GET %s HTTP/1.1\r\n" |
|
|
"Host: %s\r\n" |
|
|
"Accept: */*\r\n" |
|
|
"User-Agent: %s/%s\r\n" |
|
|
"Connection: close\r\n\r\n", |
|
|
p->request?p->request:"/", |
|
|
get_host_header(p, host), prog, VERSION); |
|
81 |
|
|
82 |
if(port_send(p, request, strlen(request), 0) < 0) { |
ASSERT(s); |
83 |
|
|
84 |
|
if(socket_print(s, "GET / HTTP/1.1\r\n" |
85 |
|
"Host: %s\r\n" |
86 |
|
"Accept: */*\r\n" |
87 |
|
"User-Agent: %s/%s\r\n" |
88 |
|
"Connection: close\r\n\r\n", |
89 |
|
get_host_header(s, host), prog, VERSION) < 0) { |
90 |
log("HTTP: error sending data -- %s\n", STRERROR); |
log("HTTP: error sending data -- %s\n", STRERROR); |
91 |
return FALSE; |
return FALSE; |
92 |
} |
} |
93 |
|
|
94 |
if(port_recv(p, buf, sizeof(buf), 0) <= 0) { |
if(socket_read(s, buf, sizeof(buf)) <= 0) { |
95 |
log("HTTP: error receiving data -- %s\n", STRERROR); |
log("HTTP: error receiving data -- %s\n", STRERROR); |
96 |
return FALSE; |
return FALSE; |
97 |
} |
} |
116 |
* @return a "hostname:port" or a void string if host |
* @return a "hostname:port" or a void string if host |
117 |
* equals LOCALHOST or if it is an IP address |
* equals LOCALHOST or if it is an IP address |
118 |
*/ |
*/ |
119 |
static char *get_host_header(Port_T p, char *hostbuf) { |
static char *get_host_header(Socket_T s, char *hostbuf) { |
120 |
|
|
121 |
if(! strcmp(LOCALHOST, p->hostname) || inet_aton(p->hostname, NULL)) { |
if(! strcmp(LOCALHOST, Socket_get_remote_hostname(s)) || |
122 |
|
inet_aton(Socket_get_remote_hostname(s), NULL)) { |
123 |
|
|
124 |
*hostbuf= 0; |
*hostbuf= 0; |
125 |
|
|
126 |
} else { |
} else { |
127 |
|
|
128 |
snprintf(hostbuf, STRLEN, "%s:%d", p->hostname, p->port); |
snprintf(hostbuf, STRLEN, "%s:%d", |
129 |
|
Socket_get_remote_hostname(s), |
130 |
|
socket_get_remote_port(s)); |
131 |
|
|
132 |
} |
} |
133 |
|
|
134 |
return hostbuf; |
return hostbuf; |