Proxylab
mindbergh/ProxyLab: CMU 15-213 Proxy Lab – GitHub
####################################################################
# CS:APP Proxy Lab
#
# Student Source Files
This directory contains the files you will need for the CS:APP Proxy
Lab.
proxy. c
csapp. h
csapp. c
open_clientfd_r. c
These are starter files. csapp. c and csapp. h are described in
your textbook. open_clientfd_r. c is a thread-safe version of
open_clientfd() based on the getaddrinfo() system call.
You may make any changes you like to these files. And you may
create and handin any additional files you like.
Please use `’ or ” to generate
unused ports for your proxy or tiny server.
Makefile
This is the makefile that builds the proxy program. Type “make”
to build your solution, or “make clean” followed by “make” for a
fresh build.
Type “make handin” to create the tarfile that you will be handing
in. You can modify it any way you like. Autolab will use your
Makefile to build your proxy from source.
Generates a random port for a particular user
usage:. /
Handy script that identifies an unused TCP port that you can use
for your proxy or tiny.
usage:. /
The autograder for Basic, Caching, and CachingConcurrency.
helper for the autograder.
tiny
Tiny Web server from the CS:APP text
Development and sale of scientific products | Proxylab Belgium
Since 1999, Proxylab has been active in the development and sale of scientific products and devices designed for research in the field of molecular and cellular biology.
Serving life science since 1999
Cell culture and microscopy
As an exclusive dealer for ibidi in Belgium, we provide a complete range of tools, devices and reagents for cell culture and high resolution or life microscopy including µ-slides and µ-dishes for immunofluorescence, µ-slides for wound healing, chemotaxis, angiogenesis or flow assays, µ-dishes for high throughput applications as well as stage top incubators for an accurate control of physiological conditions during life microscopy.
Cryogenic equipment
µCryo is a complete range of devices for low and ultra-low temperature storage of your precious samples including: cryoboxes, racking systems for ultra-low freezers, cryogloves, LN2 tanks, …
Molecular biology equipment
We provide a complete range of state of the art equipment and reagents from most renowned manufacturers for molecular biology laboratories including: gel electrophoresis apparatus, UV transilluminators, imaging systems, …
General Laboratory Equipment
As an exclusive dealer in the Benelux for catalogue Labo Moderne as well as selected dealer for DLab equipment we provide high quality products including: shakers, liquid handling, hot plates and magnetic stirrers, mixers and rotators, overhead stirrers, rotary evaporators, centrifuges, …
This site uses cookies – small text files that are placed on your machine to help the site provide a better user experience. In general, cookies are used to maintain user preferences, store information for things like shopping carts and provide anonymized tracking data to third-party apps like Google Analytics. As a rule, cookies will make your browsing experience better. However, you may prefer to disable cookies on this site and others. The most effective way is to disable cookies in your browser. We suggest you visit the Help section of your browser or visit the About Cookies website, which offers tips for all modern browsers.
七PROXY LAB – 简书
这个LAB 是上完CMU CSAPP的21-25 LECTURE之后,就可以做了。
csapp 课程观看地址:lab 6 下载地址: 选择PROXY LAB, 点击SELF-STUDY HANDOUT
恭喜你,已经来到了最后一个LAB。我的系列也已经到了尾声。纪念这一个月来的努力。把自己所有的CODE,放到了GITHUB。
这次的作业主要分三个部分(详情参见WRITE-UP ):
Sequential Proxy: 接收客户端发送的 HTTP 请求,解析之后向目标服务器转发,获得响应之后再转发回客户端
Concurrent Proxy: 在第一步的基础上,支持多线程
Cache Web Objects: 使用 LRU 缓存单独的对象,而不是整个页面
PART 1
第一部分,我的思考笔记如下。
第一步,看懂TINY SERVER(HANDOUT里赠送)的代码。 就大概知道如何写一个SERVER。
第二步,根据WRITE-UP 4 Part I: Implementing a sequential web proxy
大概需要做如下编程工作。服务器端接受请求,解析GET HTTP/1. 1 转换为 GET /hub/ HTTP/1. 0, 同时拿到HOST 和 PORT,代理服务器自己作为CLIENT向目标发送HTTP 1. 0请求.
header 部分,先全部保持不变,随后改4个值,
分别为
Host: User-Agent: Mozilla/5. 0 (X11; Linux x86_64; rv:10. 0. 3) Gecko/20120305 Firefox/10. 3
Connection: close
Proxy-Connection: close
转发送后,把接受到的信息再作为代理服务器的输出,向原客户端转发。
第一部分就大功告成。
第三步 代码实现
3. 1 抄TINY SERVER的框架,把一些常量定义掉
#include
#include “csapp. h”
/* Recommended max cache and object sizes */
#define MAX_CACHE_SIZE 1049000
#define MAX_OBJECT_SIZE 102400
/* You won’t lose style points for including this long line in your code */
static const char *user_agent_hdr = “User-Agent: Mozilla/5. 3rn”;
static const char *conn_hdr = “Connection: closern”;
static const char *prox_hdr = “Proxy-Connection: closern”;
void doit(int fd);
void clienterror(int fd, char *cause, char *errnum,
char *shortmsg, char *longmsg);
void parse_uri(char *uri, char *hostname, char *path, int *port);
void build_requesthdrs(rio_t *rp, char *newreq, char *hostname);
void *thread(void *vargp);
int main(int argc, char **argv)
{
int listenfd, *connfd;
pthread_t tid;
char hostname[MAXLINE], port[MAXLINE];
socklen_t clientlen;
struct sockaddr_storage clientaddr;
/* Check command line args */
if (argc! = 2) {
fprintf(stderr, “usage:%s
exit(1);}
signal(SIGPIPE, SIG_IGN);
listenfd = Open_listenfd(argv[1]);
while (1) {
printf(“listening.. n”);
clientlen = sizeof(clientaddr);
connfd = Malloc(sizeof(int));
*connfd = Accept(listenfd, (SA *)&clientaddr, &clientlen);
Getnameinfo((SA *) &clientaddr, clientlen, hostname, MAXLINE,
port, MAXLINE, 0);
printf(“Accepted connection from (%s, %s)n”, hostname, port);
Pthread_create(&tid, NULL, thread, connfd);}}
/* Thread routine */
void *thread(void *vargp)
int connfd = *((int *)vargp);
Pthread_detach(pthread_self());
Free(vargp);
doit(connfd);
Close(connfd);
return NULL;}
/*
* doit – handle one HTTP request/response transaction
*/
/* $begin doit */
void doit(int client_fd)
int endserver_fd;
char buf[MAXLINE], method[MAXLINE], uri[MAXLINE], version[MAXLINE];
rio_t from_client, to_endserver;
/*store the request line arguments*/
char hostname[MAXLINE], path[MAXLINE];//path eg /hub/
int port;
/* Read request line and headers */
Rio_readinitb(&from_client, client_fd);
if (! Rio_readlineb(&from_client, buf, MAXLINE))
return;
sscanf(buf, “%s%s%s”, method, uri, version);
if (strcasecmp(method, “GET”)) {
clienterror(client_fd, method, “501”, “Not Implemented”,
“Proxy Server does not implement this method”);
return;}
//parse uri then open a clientfd
parse_uri(uri, hostname, path, &port);
char port_str[10];
sprintf(port_str, “%d”, port);
endserver_fd = Open_clientfd(hostname, port_str);
if(endserver_fd<0){
printf("connection failedn");
Rio_readinitb(&to_endserver, endserver_fd);
char newreq[MAXLINE]; //for end server req headers
//set up first line /hub/ HTTP/1. 0
sprintf(newreq, "GET%s HTTP/1. 0rn", path);
build_requesthdrs(&from_client, newreq, hostname);
Rio_writen(endserver_fd, newreq, strlen(newreq)); //send client header to real server
int n;
while ((n = Rio_readlineb(&to_endserver, buf, MAXLINE))) {//real server response to buf
//printf("proxy received%d bytes, then sendn", n);
Rio_writen(client_fd, buf, n); //real server response to real client}}
/* $end doit */
* clienterror - returns an error message to the client
/* $begin clienterror */
char *shortmsg, char *longmsg)
char buf[MAXLINE], body[MAXBUF];
/* Build the HTTP response body */
sprintf(body, "
sprintf(body, “%srn”, body);
sprintf(body, “%s%s:%srn”, body, errnum, shortmsg);
sprintf(body, “%s
%s:%srn”, body, longmsg, cause);
sprintf(body, “%s
The Proxy Web serverrn”, body);
/* Print the HTTP response */
sprintf(buf, “HTTP/1. 0%s%srn”, errnum, shortmsg);
Rio_writen(fd, buf, strlen(buf));
sprintf(buf, “Content-type: text/htmlrn”);
sprintf(buf, “Content-length:%drnrn”, (int)strlen(body));
Rio_writen(fd, body, strlen(body));}
/* $end clienterror */
3. 2 实现2个辅助函数
在写PARSE URI方法前,我们得回顾下C 的STR的用法
void parse_uri(char *uri, char *hostname, char *path, int *port) {
*port = 80;
//uri char* pos1 = strstr(uri, “//”);
if (pos1 == NULL) {
pos1 = uri;} else pos1 += 2;
//printf(“parse uri pos1%sn”, pos1);//pos1
char* pos2 = strstr(pos1, “:”);
/*pos1, pos2:8080/hub/ */
if (pos2! = NULL) {
*pos2 = ‘