/* * handle_request - handle one HTTP request/response transaction */ void handle_request(int fd) { struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char headers[MAXBUF]; char request[MAXLINE], hostname[MAXLINE], arguments[MAXLINE], port[MAXLINE]; int serverfd; rio_t rio_client; rio_t rio_server; /* Read request line and headers */ Rio_readinitb(&rio_client, fd); if (!Rio_readlineb(&rio_client, buf, MAXLINE)) return; printf("%s", buf); sscanf(buf, "%s %s %s", method, uri, version); if (strcasecmp(method, "GET")) { clienterror(fd, method, "501", "Not Implemented", "Proxy does not implement this method"); return; } process_uri(uri, hostname, arguments, port); process_hdrs(&rio_client, headers, hostname); printf("%s\n", request); printf("%s\n", headers); sprintf(request, "GET %s HTTP/1.0 \r\n", arguments); /* send request to server */ serverfd = Open_clientfd(hostname, port); Rio_writen(serverfd, request, strlen(request)); Rio_writen(serverfd, headers, strlen(headers)); /* answer to client */ Rio_readinitb(&rio_server, serverfd); process_answer(&rio_server, fd); }
/******************************************************************************* * The entry point to client. Builds a request packet, and then sends it. The * way the packet is built depends on whether the URI and method have been * specified in the command line arguments. *******************************************************************************/ int main(int argc, char **argv) { int num_bytes; socket_t sockfd; int port = 0; char hostname[MAXHOSTNAMESIZE + 1] = { 0 }; char method[MAXMETHODSIZE + 1] = { 0 }; int pot_number = 0; char additions[400] = { 0 }; char message[80]; char packet[MAXPACKETSIZE + 1] = { 0 }; int i; /*check the arguments*/ printf("\n"); for (i = 1; i < argc; ++i) { if (strcmp(argv[i], "-f") == 0) { ++i; continue; } else if (strcmp(argv[i], "-u") == 0) { process_uri(argv[++i], &pot_number, additions, hostname, &port); continue; } else if (strcmp(argv[i], "-m") == 0) { strcpy(method, argv[++i]); continue; } else { printf("usage: %s [-f \"file\"] | [[-m \"method\"] [- u \"URI\"]]", argv[0]); exit(1); } } if (method[0] == '\0') { get_method(method); } /*allows the user to choose the message for a BREW/POST request*/ if (strcmp(method, "BREW") == 0 || strcmp(method, "POST") == 0) { get_message(message); } else { strcpy(message, "Content-length:0\r\n\r\n"); } if (strstr(message, "start") && additions[0] == '\0') { get_additions(additions); } if (pot_number == 0) { pot_number = get_pot_number(); } if (hostname[0] == '\0') { get_hostname(hostname); } if (port == 0) { get_port(&port); } /*build the packet from the indivdual elements built up to now*/ sprintf(packet, "%s pot-%d HTCPCP/1.0\r\n%s%s", method, pot_number, additions, message); sockfd = connect_to_server(hostname, port); /*send the packet*/ num_bytes = send_message(sockfd, packet); if (num_bytes == 0) { printf("Failed to send message %s", packet); } /*wait for response*/ do { num_bytes = receive_message(sockfd, packet, MAXPACKETSIZE); if (num_bytes != 0) { packet[num_bytes] = '\0'; printf("received: %s\n", packet); } } while (packet[0] == '\0'); disconnect_from_server(sockfd); return 0; }