Пример #1
0
/*
 * Read client response from an HTTP request into a buffer. Return the number of
 * characters written to buf (EXCLUDING null character);
 */
void
extract_client_response(struct HTTPObject *h, EthernetClient client){
    extract_status_code(h, client);
    skip_http_headers(client);
    extract_body(h, client);

    // Clean up the connection
    client.flush();
    client.stop();
}
Пример #2
0
int retrieve_chatserver_info(char *chatserver_name, u_int16_t *tcp_port, u_int16_t *udp_port)
{
	int locn_socket_fd;
	char *buf;
	int buflen;
	int code;
	int  n;

	/* Initialize locnserver_addr. 
	 * We use a text file at a web server for location info
	 * so this is just contacting the CDF web server 
	 */

	/* 
	 * 1. Set up TCP connection to web server "www.cdf.toronto.edu", 
	 *    port 80 
	 */
	/* The code you write should initialize locn_socket_fd so that
	 * it is valid for the write() in the next step.
	 */
	
	int status;
	struct addrinfo hints;
	struct addrinfo *res;  // pointer for the results
	
	memset(&hints, 0, sizeof(hints)); // make sure the struct is empty
	hints.ai_family = AF_INET;
	hints.ai_socktype = SOCK_STREAM;
	
	status = getaddrinfo("www.cdf.toronto.edu", "http", &hints, &res);
	if (status != 0) {
		return -1;
	}
	
	// make a tcp socket
	locn_socket_fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol);
	if (locn_socket_fd < 0) {
		freeaddrinfo(res);
		return -1;
	}
	
	// connect to the server
	status = connect(locn_socket_fd, res->ai_addr, res->ai_addrlen);
	if (status != 0) {
		freeaddrinfo(res);
		close(locn_socket_fd);
		return -1;
	}
	
	/* 2. write HTTP GET request to socket */

	buf = (char *)malloc(MAX_MSG_LEN);
	bzero(buf, MAX_MSG_LEN);
	build_req(buf);
	buflen = strlen(buf);

	write(locn_socket_fd, buf, buflen);

	/* 3. Read reply from web server */

	read(locn_socket_fd, buf, MAX_MSG_LEN);

	/* 
	 * 4. Check if request succeeded.  If so, skip headers and initialize
	 *    server parameters with body of message.  If not, print the 
	 *    STATUS-CODE and STATUS-TEXT and return -1.
	 */

	/* Ignore version, read STATUS-CODE into variable 'code' , and record
	 * the number of characters scanned from buf into variable 'n'
	 */
	sscanf(buf, "%*s %d%n", &code, &n);


	if (code >= 200 && code < 300) { // success!
		
		int eol = find_nn(buf, MAX_MSG_LEN);
		assert(eol >= 0);
		
		//printf("HTTP %d", code); // for debugging
		int i;
		for (i = n; i < eol; ++i) {
			putchar(buf[i]);
		}
		printf("\n");
		
		// get the server info
		char *body = skip_http_headers(buf);
		sscanf(body, "%s %hu %hu", chatserver_name, tcp_port, udp_port);
		
	} else {
		// not success, so print out status and message
		int eol = find_nn(buf, MAX_MSG_LEN);
		assert(eol >= 0);
		
		printf("HTTP error %d", code);
		int i;
		for (i = n; i < eol; ++i) {
			putchar(buf[i]);
		}
		printf("\n");
		
		free(buf);
		freeaddrinfo(res);
		close(locn_socket_fd);
		return -1;
	}

	/* 5. Clean up after ourselves and return. */
	free(buf);
	
	freeaddrinfo(res);
	close(locn_socket_fd);
	
	return 0;

}