⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 proxy_lab.htm

📁 ICS 课程的Lab8
💻 HTM
📖 第 1 页 / 共 4 页
字号:

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><b><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Part II: Dealing with multiple requests<o:p></o:p></span></b></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>Real proxies do not process requests sequentially. They deal with multiple
requests in parallel. This is particularly important when handling a request
can involve a lot of waiting (as it can when you are, for instance, contacting
a remote web server). While your proxy is waiting for a remote server to
respond to a request so that it can serve one browser, it could be working on a
pending request from another browser. <o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>Thus, once you have a logging proxy, you should alter it to handle
multiple requests simultaneously. There are two basic approaches to doing this:<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:12.0pt;text-align:left;
text-indent:-12.0pt;mso-char-indent-count:-1.0;mso-layout-grid-align:none;
text-autospace:none'><b><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>Threads </span></b><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>A common way of dealing with concurrent requests is for a server to spawn
a thread to deal with each request that comes in. In this architecture, the
main server thread simply accepts connections and spawns off worker threads
that actually deal with the requests (and terminate when they are done). If you
choose this method, however, you will have the problem that multiple peer
threads will be trying to access the log file at once. If they do not somehow
synchronize with each other, the log file will be corrupted (for instance, one
line in the file might begin in the middle of another). You will need to use a semaphore
or <span class=SpellE>mutex</span> to control access to the file, so that only
one peer thread can modify it at a time. <o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:12.0pt;text-align:left;
text-indent:-12.0pt;mso-char-indent-count:-1.0;mso-layout-grid-align:none;
text-autospace:none'><span class=GramE><b><span lang=EN-US style='font-size:
12.0pt;mso-font-kerning:0pt'>select(</span></b></span><b><span lang=EN-US
style='font-size:12.0pt;mso-font-kerning:0pt'>2) </span></b><span lang=EN-US
style='font-size:12.0pt;mso-font-kerning:0pt'>Another way to deal with
concurrent requests is to multiplex between connections by hand using the
select(2) system call. The select call takes a set of file descriptors and
waits until one of them is ready for reading or writing. You can use this call
to simultaneously wait on all open socket connections, and process whichever
one is ready first. For instance, your proxy might be waiting for a request to
come from a client on one socket, for a response to come from a server on
another socket, and at the same time listening for new connections on its
server socket. Your code would use <span class=GramE>select(</span>2) to wait
for all of these things at once, handling whichever happened first. With this
architecture, you will not need to deal with synchronization among concurrent
processes, since there is only one process running, but you will need to
correctly multiplex on several connections, without blocking on any of them. <o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:12.0pt;text-align:left;
text-indent:-12.0pt;mso-char-indent-count:-1.0;mso-layout-grid-align:none;
text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:12.0pt;text-align:left;
text-indent:-12.0pt;mso-char-indent-count:-1.0;mso-layout-grid-align:none;
text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>Again, see the Resources section for further information on these topics.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><b><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Evaluation<o:p></o:p></span></b></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=GramE><span lang=EN-US style='font-size:
12.0pt;mso-font-kerning:0pt'>Logging Proxy (15 points).</span></span><span
lang=EN-US style='font-size:12.0pt;mso-font-kerning:0pt'> A proxy that can
filter and forward requests correctly will receive 10 points. Logging and error
handling take the rest 5 points.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=GramE><span lang=EN-US style='font-size:
12.0pt;mso-font-kerning:0pt'>Handling concurrent requests (10 points).</span></span><span
lang=EN-US style='font-size:12.0pt;mso-font-kerning:0pt'> 4 points are for
correct filtering and forwarding and 6 points for concurrency handling.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><b><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Checking your work<o:p></o:p></span></b></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>We have provided some tools to help you check your work.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-family:"Courier New";
mso-font-kerning:0pt'>driver.pl<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'>The driver.pl program starts the proxy as a child process, sends it
requests, checks replies from the proxy and display a sample score for this
lab. The sample score would give you an idea of how you are going to be graded.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=SpellE><span class=GramE><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>unix</span></span></span><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>&gt;
./driver.pl<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-family:"Courier New";
mso-font-kerning:0pt'>usage:./driver.pl [port-number] [proxy-log] [lab-part]<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=GramE><span lang=EN-US style='font-size:
12.0pt;mso-font-kerning:0pt'>port-number</span></span><span lang=EN-US
style='font-size:12.0pt;mso-font-kerning:0pt'> is the port number that your
proxy will be listening to accept requests and proxy-log is the log file output
by your proxy. <span class=GramE>lab-part</span> is the <span class=SpellE>the</span>
part of the lab to test and it can be part1, part2, all. For example,<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=SpellE><span class=GramE><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>unix</span></span></span><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>&gt;
./driver.pl 4502 <span class=SpellE>proxy.log</span> part1<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span class=GramE><span lang=EN-US style='font-size:
12.0pt;mso-font-kerning:0pt'>will</span></span><span lang=EN-US
style='font-size:12.0pt;mso-font-kerning:0pt'> start to test Part I of the lab
at port 4502, assuming a log file named <span class=SpellE>proxy.log</span>.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><b><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Resources and Hints<o:p></o:p></span></b></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;text-align:left;
text-indent:-21.0pt;mso-list:l0 level1 lfo1;tab-stops:list 21.0pt;mso-layout-grid-align:
none;text-autospace:none'><![if !supportLists]><span lang=EN-US
style='font-size:12.0pt;font-family:Wingdings;mso-fareast-font-family:Wingdings;
mso-bidi-font-family:Wingdings;mso-font-kerning:0pt'><span style='mso-list:
Ignore'>l<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><![endif]><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Read Chapter 11 and 12 in your textbook. They contain
useful information on network programming, HTTP protocols, and concurrent
programming.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;text-align:left;
text-indent:-21.0pt;mso-list:l0 level1 lfo1;tab-stops:list 21.0pt;mso-layout-grid-align:
none;text-autospace:none'><![if !supportLists]><span lang=EN-US
style='font-size:12.0pt;font-family:Wingdings;mso-fareast-font-family:Wingdings;
mso-bidi-font-family:Wingdings;mso-font-kerning:0pt'><span style='mso-list:
Ignore'>l<span style='font:7.0pt "Times New Roman"'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
</span></span></span><![endif]><span lang=EN-US style='font-size:12.0pt;
mso-font-kerning:0pt'>Since HTTP is just plain text, you can actually try out
both the client and server halves of your proxy by hand. You can use telnet to
simulate a web browser: Just telnet to the host and port where you are running
the proxy, and type your request by hand (like GET http://www.yahoo.com/). For example,
suppose your proxy is <span class=SpellE>listeing</span> on port 2400. You can
use the following command to test if the proxy gets the request:<o:p></o:p></span></p>

<p class=MsoNormal align=left style='text-align:left;mso-layout-grid-align:
none;text-autospace:none'><span lang=EN-US style='font-size:12.0pt;mso-font-kerning:
0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
class=SpellE><span class=GramE><span lang=EN-US style='font-family:"Courier New";
mso-font-kerning:0pt'>unix</span></span></span><span lang=EN-US
style='font-family:"Courier New";mso-font-kerning:0pt'>&gt; telnet <span
class=SpellE>localhost</span> 2400<o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>Trying
127.0.0.1...<o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
class=GramE><span lang=EN-US style='font-family:"Courier New";mso-font-kerning:
0pt'>Connected to <span class=SpellE>localhost</span>.</span></span><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'><o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>Escape
character is ’&#710;]’.<o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
lang=EN-US style='font-family:"Courier New";mso-font-kerning:0pt'>GET
http://www.yahoo.com/<o:p></o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
lang=EN-US style='font-size:12.0pt;mso-font-kerning:0pt'><o:p>&nbsp;</o:p></span></p>

<p class=MsoNormal align=left style='margin-left:21.0pt;mso-para-margin-left:
2.0gd;text-align:left;mso-layout-grid-align:none;text-autospace:none'><span
lang=EN-US style='font-size:12.0pt;mso-font-kerning:0pt'>To experiment with the
client side of your proxy, we are providing a “reverse telnet” utility
(courtesy of Blake Scholl). This program listens on a given port for a
connection, then accepts the connection and displays incoming text on the
screen. Whatever you type on <span class=SpellE>stdin</span> is sent to the
process that connected. So if you start reverse telnet on port 80 and point
your web browser or proxy to that port, you will see HTTP requests on the
screen and can actually type back a web page. <o:p></o:p></span></p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -