8 #define CLIENT_TIMEOUT 15
9 #define sock_close(sock) close(sock)
11 int sock_connect(char *host, char *service, char *protocol)
16 struct sockaddr_in sin;
17 struct sockaddr_in egress_sin;
20 if ((host == NULL) || IsEmptyStr(host))
22 if ((service == NULL) || IsEmptyStr(service))
24 if ((protocol == NULL) || IsEmptyStr(protocol))
27 memset(&sin, 0, sizeof(sin));
28 sin.sin_family = AF_INET;
30 pse = getservbyname(service, protocol);
32 sin.sin_port = pse->s_port;
33 } else if ((sin.sin_port = htons((u_short) atoi(service))) == 0) {
34 lprintf(CTDL_CRIT, "Can't get %s service entry: %s\n",
35 service, strerror(errno));
38 phe = gethostbyname(host);
40 memcpy(&sin.sin_addr, phe->h_addr, phe->h_length);
41 } else if ((sin.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE) {
42 lprintf(CTDL_ERR, "Can't get %s host entry: %s\n",
43 host, strerror(errno));
46 if ((ppe = getprotobyname(protocol)) == 0) {
47 lprintf(CTDL_CRIT, "Can't get %s protocol entry: %s\n",
48 protocol, strerror(errno));
51 if (!strcmp(protocol, "udp")) {
57 s = socket(PF_INET, type, ppe->p_proto);
59 lprintf(CTDL_CRIT, "Can't create socket: %s\n", strerror(errno));
63 /* If citserver is bound to a specific IP address on the host, make
64 * sure we use that address for outbound connections. FIXME make this work in webcit
66 memset(&egress_sin, 0, sizeof(egress_sin));
67 egress_sin.sin_family = AF_INET;
68 // if (!IsEmptyStr(config.c_ip_addr)) {
69 // egress_sin.sin_addr.s_addr = inet_addr(config.c_ip_addr);
70 // if (egress_sin.sin_addr.s_addr == !INADDR_ANY) {
71 // egress_sin.sin_addr.s_addr = INADDR_ANY;
74 /* If this bind fails, no problem; we can still use INADDR_ANY */
75 bind(s, (struct sockaddr *)&egress_sin, sizeof(egress_sin));
78 /* Now try to connect to the remote host. */
79 if (connect(s, (struct sockaddr *) &sin, sizeof(sin)) < 0) {
80 lprintf(CTDL_ERR, "Can't connect to %s:%s: %s\n",
81 host, service, strerror(errno));
92 * sock_read_to() - input binary data from socket, with a settable timeout.
93 * Returns the number of bytes read, or -1 for error.
94 * If keep_reading_until_full is nonzero, we keep reading until we get the number of requested bytes
96 int sock_read_to(int sock, char *buf, int bytes, int timeout, int keep_reading_until_full)
110 retval = select(sock+1, &rfds, NULL, NULL, &tv);
112 if (FD_ISSET(sock, &rfds) == 0) { /* timed out */
113 lprintf(CTDL_ERR, "sock_read_to() timed out.\n");
117 rlen = read(sock, &buf[len], bytes-len);
119 lprintf(CTDL_ERR, "sock_read_to() failed: %s\n",
124 if (!keep_reading_until_full) return(len);
131 * sock_write() - send binary to server.
132 * Returns the number of bytes written, or -1 for error.
134 int sock_write(int sock, char *buf, int nbytes)
136 int bytes_written = 0;
138 while (bytes_written < nbytes) {
139 retval = write(sock, &buf[bytes_written],
140 nbytes - bytes_written);
144 bytes_written = bytes_written + retval;
146 return (bytes_written);
152 * Input string from socket - implemented in terms of sock_read_to()
155 int sock_getln(int sock, char *buf, int bufsize)
159 /* Read one character at a time.
162 if (sock_read_to(sock, &buf[i], 1, CLIENT_TIMEOUT, 1) < 0) return(-1);
163 if (buf[i] == '\n' || i == (bufsize-1))
167 /* If we got a long line, discard characters until the newline.
169 if (i == (bufsize-1))
170 while (buf[i] != '\n')
171 if (sock_read_to(sock, &buf[i], 1, CLIENT_TIMEOUT, 1) < 0) return(-1);
173 /* Strip any trailing CR and LF characters.
177 && ( (buf[i - 1]==13)
178 || ( buf[i - 1]==10)) ) {
188 * sock_puts() - send line to server - implemented in terms of serv_write()
189 * Returns the number of bytes written, or -1 for error.
191 int sock_puts(int sock, char *buf)
195 i = sock_write(sock, buf, strlen(buf));
197 j = sock_write(sock, "\n", 1);
208 * Fallback handler for fetch_http() that uses our built-in mini client
210 int fetch_http_using_mini_client(char *url, char *target_buf, int maxbytes)
217 int got_bytes = (-1);
218 int redirect_count = 0;
219 int total_bytes_received = 0;
223 snprintf(buf, (sizeof buf)-1, "%s", url);
224 i = parse_url(buf, httphost, &httpport, httpurl);
226 snprintf(buf, (sizeof buf)-1, "http://%s", url);
227 i = parse_url(buf, httphost, &httpport, httpurl);
231 i = parse_url(buf, httphost, &httpport, httpurl);
234 lprintf(CTDL_ALERT, "Invalid URL: %s (%d)\n", url, i);
238 retry: lprintf(CTDL_NOTICE, "Connecting to <%s>\n", httphost);
239 sprintf(buf, "%d", httpport);
240 sock = sock_connect(httphost, buf, "tcp");
242 lprintf(CTDL_DEBUG, "Connected!\n");
244 snprintf(buf, sizeof buf, "GET %s HTTP/1.0", httpurl);
245 lprintf(CTDL_DEBUG, "<%s\n", buf);
246 sock_puts(sock, buf);
248 snprintf(buf, sizeof buf, "Host: %s", httphost);
249 lprintf(CTDL_DEBUG, "<%s\n", buf);
250 sock_puts(sock, buf);
252 snprintf(buf, sizeof buf, "User-Agent: WebCit");
253 lprintf(CTDL_DEBUG, "<%s\n", buf);
254 sock_puts(sock, buf);
256 snprintf(buf, sizeof buf, "Accept: */*");
257 lprintf(CTDL_DEBUG, "<%s\n", buf);
258 sock_puts(sock, buf);
262 if (sock_getln(sock, buf, sizeof buf) >= 0) {
263 lprintf(CTDL_DEBUG, ">%s\n", buf);
264 remove_token(buf, 0, ' ');
269 while (got_bytes = sock_getln(sock, buf, sizeof buf),
270 (got_bytes >= 0 && (strcmp(buf, "")) && (strcmp(buf, "\r"))) ) {
271 /* discard headers */
274 while (got_bytes = sock_read_to(sock, buf, sizeof buf, CLIENT_TIMEOUT, 0),
277 if (total_bytes_received + got_bytes > maxbytes) {
278 got_bytes = maxbytes - total_bytes_received;
281 memcpy(&target_buf[total_bytes_received], buf, got_bytes);
282 total_bytes_received += got_bytes;
289 else if ( (!strncmp(buf, "30", 2)) && (redirect_count < 16) ) {
290 while (got_bytes = sock_getln(sock, buf, sizeof buf),
291 (got_bytes >= 0 && (strcmp(buf, "")) && (strcmp(buf, "\r"))) ) {
292 if (!strncasecmp(buf, "Location:", 9)) {
294 strcpy(buf, &buf[9]);
296 if (parse_url(buf, httphost, &httpport, httpurl) == 0) {
301 lprintf(CTDL_ALERT, "Invalid URL: %s\n", buf);
311 lprintf(CTDL_ERR, "Could not connect: %s\n", strerror(errno));
314 return total_bytes_received;
320 * Begin an HTTP fetch (returns number of bytes actually fetched, or -1 for error)
321 * We first try 'curl' or 'wget' because they have more robust HTTP handling, and also
322 * support HTTPS. If neither one works, we fall back to a built in mini HTTP client.
324 int fetch_http(char *url, char *target_buf, int maxbytes)
328 int bytes_received = 0;
331 memset(target_buf, 0, maxbytes);
334 snprintf(cmd, sizeof cmd, "curl -L %s </dev/null 2>/dev/null", url);
335 fp = popen(cmd, "r");
339 snprintf(cmd, sizeof cmd, "wget -q -O - %s </dev/null 2>/dev/null", url);
340 fp = popen(cmd, "r");
344 while ( (!feof(fp)) && (bytes_received < maxbytes) ) {
347 target_buf[bytes_received++] = ch;
351 return bytes_received;
354 /* Fall back to the built-in mini handler */
355 return fetch_http_using_mini_client(url, target_buf, maxbytes);