FreeBSD/tools/tools/netrate/http/http.c
/*
 * Given a partially processed URL, fetch it from the specified host.
 */
Given a partially processed URL, fetch it from the specified host.
Given a partially processed URL fetch it from the specified host .
static int
http_fetch(struct sockaddr_in *sin, char *path, int quiet)
{
	u_char buffer[BUFFER];
	ssize_t len;
	size_t sofar;
	int sock;
	sock = socket(PF_INET, SOCK_STREAM, 0);
	if (sock < 0) {
		if (!quiet)
			warn("socket(PF_INET, SOCK_STREAM)");
		return (-1);
	}
	if (connect(sock, (struct sockaddr *)sin, sizeof(*sin)) < 0) {
		if (!quiet)
			warn("connect");
		close(sock);
		return (-1);
	}
	snprintf(buffer, BUFFER, "GET %s HTTP/1.0\n\n", path);
	sofar = 0;
	while (sofar < strlen(buffer)) {
		len = send(sock, buffer, strlen(buffer), 0);
		if (len < 0) {
			if (!quiet)
				warn("send");
			close(sock);
			return (-1);
		}
		if (len == 0) {
			if (!quiet)
				warnx("send: len == 0");
		}
		sofar += len;
	}
	while (1) {
		len = recv(sock, buffer, BUFFER, 0);
		if (len < 0) {
			if (!quiet)
				warn("recv");
			close(sock);
			return (-1);
		}
		if (len == 0)
			break;
	}
	close(sock);
	return (0);
}
static int http_fetch ( struct sockaddr_in * sin , char * path , int quiet ) { u_char buffer [ BUFFER ] ; ssize_t len ; size_t sofar ; int sock ; sock = socket ( PF_INET , SOCK_STREAM , 0 ) ; if ( sock < 0 ) { if ( ! quiet ) warn ( " " ) ; return ( -1 ) ; } if ( connect ( sock , ( struct sockaddr * ) sin , sizeof ( * sin ) ) < 0 ) { if ( ! quiet ) warn ( " " ) ; close ( sock ) ; return ( -1 ) ; } snprintf ( buffer , BUFFER , " \n \n " , path ) ; sofar = 0 ; while ( sofar < strlen ( buffer ) ) { len = send ( sock , buffer , strlen ( buffer ) , 0 ) ; if ( len < 0 ) { if ( ! quiet ) warn ( " " ) ; close ( sock ) ; return ( -1 ) ; } if ( len == 0 ) { if ( ! quiet ) warnx ( " " ) ; } sofar += len ; } while ( 1 ) { len = recv ( sock , buffer , BUFFER , 0 ) ; if ( len < 0 ) { if ( ! quiet ) warn ( " " ) ; close ( sock ) ; return ( -1 ) ; } if ( len == 0 ) break ; } close ( sock ) ; return ( 0 ) ; }
Given a partially processed URL, fetch it from the specified host.
Given a partially processed URL fetch it from the specified host .
/* XXX: Mark non-blocking. */ /* XXX: select for connection. */ /* Send a request. */ /* Read until done.  Not very smart. */