void doit(int fd) { int clientfd; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE]; char host[MAXLINE], port[MAXLINE]; char filename[MAXLINE]; char cache_buf[MAX_OBJECT_SIZE]; size_t buflen; struct cache *cache; int cache_size = 0, cache_flag = 1; rio_t c_rio, s_rio; /* Read request line and headers */ Rio_readinitb(&c_rio, fd); if (!Rio_readlineb(&c_rio, buf, MAXLINE)) return; printf("%s", buf); sscanf(buf, "%s %s", method, uri); if (strcasecmp(method, "GET")) { printf("this proxy can handle only \"GET\"\n"); return; } construct_requesthdrs(&c_rio, buf, filename, uri, host, port); if (find_cache(&cache, uri) == 1) { Rio_writen(fd, cache->data, strlen(cache->data)); } else { clientfd = Open_clientfd(host, port); if (clientfd < 0) { printf("there is no such server\n"); return; } Rio_readinitb(&s_rio, clientfd); Rio_writen(clientfd, buf, strlen(buf)); while ((buflen = Rio_readlineb(&s_rio, buf, MAXLINE)) != 0) { Rio_writen(fd, buf, buflen); if (cache_size + buflen > MAX_OBJECT_SIZE) { cache_flag = 0; } else { memcpy(cache_buf + cache_size, buf, buflen); cache_size += buflen; } } if (cache_flag == 1) create_cache(cache_buf, uri); Close(clientfd); } }
/* * doit - handle one HTTP request/response transaction */ void doit(int fd) { int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio_c, rio_h; /* Ren's Local Vars */ char host[MAXLINE], url[MAXLINE], request[MAXLINE], header[MAXLINE]; int hostfd; /* Ren's Local Vars END */ /* Read request line and headers */ Rio_readinitb(&rio_c, fd); Rio_readlineb(&rio_c, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); if (strcasecmp(method, "GET")) { clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method"); return; } printf("STUFF FROM THE CLIENT:\n"); printf("%s\n",buf); read_requesthdrs(&rio_c); /* Ren's code */ parseURL(buf, host, uri, version); /* parse url for hostname and uri */ hostfd = Open_clientfd(host, PORT); /* connect to host as client */ Rio_readinitb(&rio_h, hostfd); /* set up host file discriptor */ /* generate and send request to host*/ genrequest(request, method, uri); genheader(host, header); strcat(request, header); printf("%s\n",request); Rio_writen(hostfd, request, strlen(request)); /* stream information from server to client */ printf("STUFF FROM THE SERVER:\n"); while(Rio_readlineb(&rio_h, buf, MAXLINE)){ printf("%s\n",buf); Rio_writen(fd, buf, MAXLINE); } printf("stream ended\n"); /* Ren's code */ }
void check_clients(pool *p) { int i, connfd, n; char buf[MAXLINE]; rio_t rio; for (i = 0; (i <= p->maxi) && (p->nready > 0); i++) { connfd = p->clientfd[i]; rio = p->clientrio[i]; //if the descriptor is ready, echo a text line from it if ( (connfd > 0) && (FD_ISSET(connfd, &p->ready_set))) { p->nready--; if ( (n = Rio_readinitb(&rio, buf, MAXLINE)) != 0) { byte_cnt += n; printf("server received %d (%d total) bytes on fd %d\n", n, byte_cnt, connfd); Rio_writen(connfd, buf, n); } //EOF detected, remove descriptor from pool else { Close(connfd); FD_CLR(connfd, &p->read_set); p->clienfd[i] = -1; } } } }
void * keyboard(void * vargp) { char buf[MAXLINE]; rio_t rio; fprintf(stderr,"fprintf 7\n"); int clientfd = *((int*) vargp); fprintf(stderr,"fprintf 8\n"); Rio_readinitb(&rio, clientfd); while(Fgets(buf, MAXLINE, stdin) != NULL) { fprintf(stderr,"fprintf 9\n"); if(active) Rio_writen(clientfd, buf, strlen(buf)); else break; } if(active) { fprintf(stderr,"fprintf 10\n"); strcpy(buf, "$q\n"); Rio_writen(clientfd, buf, strlen(buf)); } return NULL; }
int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); printf("type:"); fflush(stdout); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Rio_readlineb(&rio, buf, MAXLINE); printf("echo:"); Fputs(buf, stdout); printf("type:"); fflush(stdout); } Close(clientfd); exit(0); }
/* Read a request from a connection socket and parses its information into a * req_t struct. * Initally based on csapp echo() p.911 * Allocates memory for request hdrs * returns -1 on failure, 1 if the content is local, and 0 if the content is * remote */ int process_request(int fd, req_t* req){ size_t n; char buf[MAXLINE]; rio_t rio; req->domain = NULL; req->path = NULL; req->hdrs = NULL; //Parse domain and path information Rio_readinitb(&rio, fd); if((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){ if(parse_req(buf, req) == -1) return -1; } else return -1; //parse header information while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0){ if(strcmp(buf, "\r\n") == 0) break; if(req->hdrs != NULL){ n = strlen(req->hdrs) + strlen(buf) + 1; req->hdrs = Realloc(req->hdrs,strlen(req->hdrs)+ n); strcat(req->hdrs, handle_hdr(buf)); } else { req->hdrs = Malloc(n+1); strcpy(req->hdrs, handle_hdr(buf)); } } return 0; }
void receive_message(){ char buf[MAXLINE]; char header[MAXLINE]; char buf1[MAXLINE]; char buf2[MAXLINE]; char buf3[MAXLINE]; rio_t client; Rio_readinitb(&client, clientfd); while(1) { //rio_readp(clientfd,buf,MAXLINE); Rio_readlineb(&client,buf,MAXLINE); if (strcmp(buf,"error\r\n")==0){ printf("The file you want to download doesn't exist!\n"); close(clientfd); exit(1); } if (strcmp(buf, "finish\r\n") == 0){ break; } sscanf(buf, "%s %s %s %s", header, buf1, buf2, buf3); if (strcmp(header, "total") == 0){ total_num = atoi(buf1); user_list = malloc(sizeof(user_list)*total_num); } if (strcmp(header,"user")==0){ int i = atoi(buf1); user_list[i-1].IP = strdup(buf2); user_list[i-1].port = atoi(buf3); } //printf("buf is %s\n",buf); memset(buf, 0, strlen(buf)); } close(clientfd); }
Response response_read(int fd) { rio_t rio; char buf[MAXLINE]; Rio_readinitb(&rio, fd); ssize_t r = Rio_readlineb(&rio, buf, MAXLINE); if (r == 0) { printf("Received empty response\n"); return NULL; } Response response = malloc(sizeof(struct Response)); sscanf(buf, "%s %d %s", response->version, &response->status_code, response->status_name); header_read(&rio, buf, &response->header); HeaderEntry* content_length_entry = header_find_entry(&response->header, "Content-Length"); int content_length = content_length_entry == NULL ? 0 : atoi(content_length_entry->value); response->content_length = content_length; response->content = malloc(response->content_length * sizeof(char)); char *content = response->content; while (content_length > 0) { r = Rio_readlineb(&rio, content, (size_t) content_length); if (r > 0) { content += r; content_length -= r; } } return response; }
/* * Read the HTTP response and print it out */ void clientPrint(int fd) { rio_t rio; char buf[MAXBUF]; int length = 0; int n; Rio_readinitb(&rio, fd); /* Read and display the HTTP Header */ n = Rio_readlineb(&rio, buf, MAXBUF); while (strcmp(buf, "\r\n") && (n > 0)) { printf("Header: %s", buf); n = Rio_readlineb(&rio, buf, MAXBUF); /* If you want to look for certain HTTP tags... */ if (sscanf(buf, "Content-Length: %d ", &length) == 1) { printf("Length = %d\n", length); } } /* Read and display the HTTP Body */ n = Rio_readlineb(&rio, buf, MAXBUF); while (n > 0) { printf("%s", buf); n = Rio_readlineb(&rio, buf, MAXBUF); } }
int main(int argc, char** argv) { int clientfd; char* hostname = argv[1]; char* filename = argv[2]; int port = atoi(argv[3]); char data[60]; strcpy(data, "GET /"); strcat(data, filename); strcat(data, " HTTP/1.1\r\nHost: "); strcat(data, hostname); strcat(data, "\r\n\r\n"); printf("Constructed Data: %s\n", data); char buffer[MAXLINE]; // Using MAXLINE from csapp bzero(buffer, MAXLINE); buffer[0] = 'x'; rio_t rio; clientfd = Open_clientfd(hostname, port); Rio_readinitb(&rio, clientfd); rio_writen(clientfd, data, strlen(data)); while (strlen(buffer)>0) { Rio_readlineb (&rio, buffer, MAXLINE); printf("%s\n", buffer); } close(clientfd); exit(0); }
void getInfo(int fd, rio_t client, int id){ char buf1[MAXLINE]; int jobID=id; int numBytes; char * saveptr=NULL; Rio_readinitb( &client,fd); while(true){ numBytes = Rio_readlineb(&client, buf1, MAXLINE); id1 = strtok_r(buf1, " \r\n",&saveptr); int fileStartID=atoi(id1); id2 = strtok_r(buf1, " \r\n",&saveptr); int fileEndID=atoi(id2); numBytes = Rio_readlineb(c, buf, MAXLINE); if(numBytes<0){ Close(fd); return ans; } buf[numBytes]=0; seperate(buf, &ans.pre); fd=Open_clientfd(ipAddress, atoi(port)); }
int main(int argc, char **argv) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { printf("client get: %s\n",buf); Rio_writen(clientfd, buf, strlen(buf)); printf("client buf: %s\n",buf); Rio_readlineb(&rio, buf, MAXLINE); Fputs(buf, stdout); } // printf("outside\n"); Close(clientfd); //line:netp:echoclient:close exit(0); }
int main(int argc, char **argv) { int n; rio_t rio; char buf[MAXLINE]; if (argc > 2) { fprintf(stderr, "usage: %s [filename]\n", argv[0]); return -1; } /* 这里将标准输入的文件描述符,重定位到输入文件的描述符 */ if (argc == 2) { char *filename = argv[1]; int fd = Open(filename, O_RDONLY, 0); int ret = dup2(fd, STDIN_FILENO); /* 将标准输入重定位到 fd */ if (ret < 0) { perror("dup2"); return -1; } } /* 没有输入文件名,将标准输入拷贝到标准输出 */ Rio_readinitb(&rio, STDIN_FILENO); while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) Rio_writen(STDOUT_FILENO, buf, n); return 0; }
int getSize(int fd) { //stats.req_arrival = id.req_arrival; int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; //stats.size = -1; Rio_readinitb(&rio, fd); Rio_readlineb(&rio, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); //close(fd); printf("%s %s %s\n", method, uri, version); if (strcasecmp(method, "GET")) { requestError(fd, method, "501", "Not Implemented", "CS537 Server does not implement this method"); return 0; } requestReadhdrs(&rio); is_static = requestParseURI(uri, filename, cgiargs); if (stat(filename, &sbuf) < 0) { requestError(fd, filename, "404", "Not found", "CS537 Server could not find this file"); return 0; } return sbuf.st_size; }
int echoClient(int argc, char** argv) { int clientfd, port; char* host, buf[MAXLINE]; rio_t rio; if (argc != 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while (Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); // get response from server Rio_readlineb(&rio, buf, MAXLINE); //Fputs(buf, stdout); } Close(clientfd); exit(0); }
int main(int argc, char *argv[]) { int clientfd, port; char *host, buf[MAXLINE]; rio_t rio; /* if(argc!= 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(1); } */ //host = argv[1]; //port = atoi(argv[2]); host ="127.0.0.1"; port=8080; clientfd = Open_clientfd(host, port); Rio_readinitb(&rio, clientfd); while(Fgets(buf, MAXLINE, stdin) != NULL) { Rio_writen(clientfd, buf, strlen(buf)); Fputs(buf, stdout); } close(clientfd); exit(0); }
int main(int argc, char **argv) { if(!(argc >=2) ) //If commandline args are not provided { printf("No file provided\n"); return -1; } //Use csapp Open to get fd number (creates files) int fd = Open(argv[1],O_CREAT | O_WRONLY,0); int n; //Number of bytes rio_t rio; //RIO type that contains file info char buf[MAXLINE]; //Character buffer with max size defined by csapp //Associate a descriptor with a read buffer and reset buffer //the POSIX <unistd.h> definition is STDIN_FILENO Rio_readinitb(&rio,STDIN_FILENO); while((n = rio_readnb(&rio, buf, MAXLINE)) != 0) //While it has read more than zero bytes { Rio_writen(fd, buf, n);//Write n bytes from buf } Close(fd); exit(0); }
char* getInfo(int fd){ char* buf = (char*)malloc(MAXLINE); rio_t rio; Rio_readinitb(&rio, fd); Rio_readlineb(&rio, buf, MAXLINE); requestReadhdrs(&rio); return buf; }
request_t requestParse(request_t request) { int fd = request.connfd; int is_static; struct stat stat_buf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; Rio_readinitb(&rio, fd); Rio_readlineb(&rio, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); printf("%s %s %s\n", method, uri, version); if (strcasecmp(method, "GET")) { requestError(fd, method, "501", "Not Implemented", "CS537 Server does not implement this method"); request.is_static = -1; return request; } requestReadhdrs(&rio); is_static = requestParseURI(uri, filename, cgiargs); if (stat(filename, &stat_buf) < 0) { requestError(fd, filename, "404", "Not found", "CS537 Server could not find this file"); request.is_static = -1; return request; } if (is_static) { if (!(S_ISREG(stat_buf.st_mode)) || !(S_IRUSR & stat_buf.st_mode)) { requestError(fd, filename, "403", "Forbidden", "CS537 Server could not read this file"); request.is_static = -1; return request; } } else { if (!(S_ISREG(stat_buf.st_mode)) || !(S_IXUSR & stat_buf.st_mode)) { requestError(fd, filename, "403", "Forbidden", "CS537 Server could not run this CGI program"); request.is_static = -1; return request; } } // save relevant information for handling the request later strcpy(request.filename, filename); strcpy(request.cgiargs, cgiargs); request.stat_buf = stat_buf; request.is_static = is_static; request.file_size = stat_buf.st_size; return request; }
/* $begin doit */ void * doit( void * fd_ptr) { int fd = * (int *)fd_ptr; printf("\nFile Desc = %d", fd); int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; /* Read request line and headers */ Rio_readinitb(&rio, fd); if (!Rio_readlineb(&rio, buf, MAXLINE)) { //line:netp:doit:readrequest { pthread_exit(NULL); return NULL; } printf("%s", buf); sscanf(buf, "%s %s %s", method, uri, version); //line:netp:doit:parserequest if (strcasecmp(method, "GET")) { //line:netp:doit:beginrequesterr clienterror(fd, method, "501", "Not Implemented", "Tiny does not implement this method"); pthread_exit(NULL); return NULL; } //line:netp:doit:endrequesterr read_requesthdrs(&rio); //line:netp:doit:readrequesthdrs /* Parse URI from GET request */ is_static = parse_uri(uri, filename, cgiargs); //line:netp:doit:staticcheck if (stat(filename, &sbuf) < 0) { //line:netp:doit:beginnotfound clienterror(fd, filename, "404", "Not found", "Tiny couldn't find this file"); pthread_exit(NULL); return NULL; } //line:netp:doit:endnotfound if (is_static) { /* Serve static content */ if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) { //line:netp:doit:readable clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't read the file"); pthread_exit(NULL); return NULL; } serve_static(fd, filename, sbuf.st_size); //line:netp:doit:servestatic } else { /* Serve eynamic content */ if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) { //line:netp:doit:executable clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program"); pthread_exit(NULL); return NULL; } serve_dynamic(fd, filename, cgiargs); //line:netp:doit:servedynamic } Close(fd); pthread_exit(NULL); return NULL; }
void echo(int connfd) { size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { printf("server received %d bytes\n", n); Rio_writen(connfd, buf, n); } }
void echo(int connfd, unsigned int thread_id) { size_t n; char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { //line:netp:echo:eof printf("NET*** [ 0x%x ] *** Received %d bytes\n", thread_id,n); Rio_writen(connfd, buf, n); } }
void echo(int connfd) { size_t n; /* size_t在64微操作系统中代表 long unsigned int */ char buf[MAXLINE]; rio_t rio; Rio_readinitb(&rio, connfd); /* rio缓冲区读connfd中的数据 */ while ((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) { printf("server received %lu bytes\n", n); Rio_writen(connfd, buf, n); } }
/** @brief Handles HTTP GET requests. * Reads and parses the request, the uri, and dispatches the appropriate * functions to serve content. * @param conn_fd The connection file descriptor. * @return none. */ void handle_request(int conn_fd) { rio_t rio; char request[MAXLINE], uri[MAXLINE]; char file_name[MAXLINE], cgi_args[MAXLINE]; int is_static; struct stat st_buf; Rio_readinitb(&rio, conn_fd); /* Read first header line of incoming request. */ if (rio_readlineb(&rio, request, MAXLINE) < 0) { dbg_printf("rio_readlineb error\n"); return; } dbg_printf("Request: %s", request); if (parse_request_header(conn_fd, request, uri) != SUCCESS) { return; } if (read_request_headers(&rio) != SUCCESS) { return; } find_file(uri, file_name, cgi_args, &is_static); if (stat(file_name, &st_buf) < 0) { dbg_printf("404: File not found: %s\n", file_name); send_error_msg(conn_fd, "404", "File not found"); return; } /* Handle static content */ if (is_static) { if (!(S_ISREG(st_buf.st_mode)) || !(S_IRUSR & st_buf.st_mode)) { dbg_printf("403: Can't read the file: %s\n", file_name); send_error_msg(conn_fd, "403", "Can't read the file."); return; } serve_static(conn_fd, file_name, st_buf.st_size); } /* Handle dynamic content */ else { if (!(S_ISREG(st_buf.st_mode)) || !(S_IXUSR & st_buf.st_mode)) { dbg_printf("403: Can't run the CGI program: %s\n", file_name); send_error_msg(conn_fd, "403", "Can't run the CGI program."); return; } serve_dynamic(conn_fd, file_name, cgi_args); } }
int main(int argc, char * argv[]) { int clientfd; char *host, buf[MAXLINE]; int port; rio_t rio; pthread_t tid; fprintf(stderr,"fprintf 1\n"); if(argc < 3) { fprintf(stderr, "usage: %s <host> <port>\n", argv[0]); exit(0); } host = argv[1]; port = atoi(argv[2]); fprintf(stderr,"fprintf 2\n"); clientfd = Open_clientfd(host, port); fprintf(stderr,"fprintf 3\n"); active = 1; Pthread_create(&tid, NULL, keyboard, &clientfd); fprintf(stderr,"fprintf 4\n"); Rio_readinitb(&rio, clientfd); while(Rio_readlineb(&rio, buf, MAXLINE) != 0) { fprintf(stderr,"fprintf 5\n"); if(strcmp(buf, "$q") == 0) { printf("received $q command from server\n"); active = 0; } else { fprintf(stderr,"fprintf 6\n"); Fputs(buf, stdout); } } Close(clientfd); /* note: if the server closes the connection, the keyboard thread will be in Fgets and will try to write to a closed connection... */ return 0; }
int main(int argc, char **argv) { int n; rio_t rio; char buf[MAXLINE]; Rio_readinitb(&rio, STDIN_FILENO); while((n = Rio_readlineb(&rio, buf, MAXLINE)) != 0) Rio_writen(STDOUT_FILENO, buf, n); /* $end cpfile */ exit(0); /* $begin cpfile */ }
/* $begin ServeClient */ void ServeClient(int fd) { int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; cgiargs[0] = 0; /* Read request line and headers */ Rio_readinitb(&rio, fd); Rio_readlineb(&rio, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); if ( strcasecmp(method, "GET") == 0 ) { is_static = parse_uri(uri, filename, cgiargs); } else if ( strcasecmp(method, "POST") == 0 ) { is_static = parse_uri(uri, filename, cgiargs); } else { clienterror(fd, method, "501", "Not Implemented", "Server does not implement this method"); return; } if (stat(filename, &sbuf) < 0) { clienterror(fd, filename, "404", "Not found", "Tiny couldn't find this file"); return; } if (is_static) /* Serve static content */ { if (!(S_ISREG(sbuf.st_mode)) || !(S_IRUSR & sbuf.st_mode)) { clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't read the file"); return; } serve_static(fd, filename, sbuf.st_size); } else /* Serve dynamic content */ { if (!(S_ISREG(sbuf.st_mode)) || !(S_IXUSR & sbuf.st_mode)) { clienterror(fd, filename, "403", "Forbidden", "Tiny couldn't run the CGI program"); return; } serve_dynamic(fd, filename, cgiargs, &rio); } }
void preRequestHandle(request* req) { int fd = req->fd; int is_static; struct stat sbuf; char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE]; char filename[MAXLINE], cgiargs[MAXLINE]; rio_t rio; Rio_readinitb(&rio, fd); Rio_readlineb(&rio, buf, MAXLINE); sscanf(buf, "%s %s %s", method, uri, version); printf("%s %s %s\n", method, uri, version); if(strcasecmp(method, "GET")) { requestError( fd, method, "501", "Not Implemented", "CS537 Server does not implement this method" ); return; } requestReadhdrs(&rio); is_static = requestParseURI(uri, filename, cgiargs); if(stat(filename, &sbuf) < 0) { requestError( fd, filename, "404", "Not found", "CS537 Server could not find this file" ); return; } req->ready = 1; req->sbuf = sbuf; req->is_static = is_static; req->cgiargs = cgiargs; /* TODO: Free req->filename in worker thread */ req->filename = malloc(sizeof(char)*MAXLINE); strncpy(req->filename, filename, MAXLINE); }
/* * forward_to_client - forward without write to cache * * used for non GET methods; * * return -1 on error * return 0 on success */ int forward_to_client(int to_client_fd, int to_server_fd) { rio_t rio_server; char buf[MAXLINE]; unsigned int length = 0, size = 0; Rio_readinitb(&rio_server, to_server_fd); // forward status line if (Rio_readlineb(&rio_server, buf, MAXLINE) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, strlen(buf)) == -1) { return -1; } // forward response headers while (strcmp(buf, "\r\n") != 0 && strlen(buf) > 0) { if (Rio_readlineb(&rio_server, buf, MAXLINE) == -1) { return -1; } get_size(buf, &size); if (Rio_writen(to_client_fd, buf, strlen(buf)) == -1) { return -1; } } // forward response body if (size > 0) { while (size > MAXLINE) { if ((length = Rio_readnb(&rio_server, buf, MAXLINE)) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } size -= MAXLINE; } if (size > 0) { if ((length = Rio_readnb(&rio_server, buf, size)) == -1) { return -1; } if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } } } else { while ((length = Rio_readnb(&rio_server, buf, MAXLINE)) > 0) { if (Rio_writen(to_client_fd, buf, length) == -1) { return -1; } } } return 0; }
/* * 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); }