Implementation of the HTTP proxy server under Windows

Source: Internet
Author: User
Tags add time

Implementation of the HTTP proxy server under Windows
Summary: This article describes the advantages of the proxy server, the implementation of the HTTP proxy server under Windows, and finally
Briefly discusses the meaning of implementing a proxy server.
Key words:
Proxy, HTTP, Windows, threads
Normal Internet access is a typical client/server (CLIENT/SERVER) structure: The user's local computer
The remote WWW server program responds to the request and provides the corresponding data on the client program, such as the browser.
And proxy is between the client and server, for the server, proxy is the client, make a request;
For a client, proxy is the server that accepts the request and provides the data.
Proxy Server model
The proxy server has several advantages:
(i) Safety barriers
Proxy is a software based on the application layer, its main function is as an important part of firewall, for it to protect
The protection of the host to the security barrier role. Proxy for flow control, packet filtering, access control, and operating system
User integration, so proxy can be a core part of Simple network management software, and it is also a small and medium-sized enterprise
An important part of the Interner access solution.
(ii) Improved network performance
When users of the internet grow exponentially, the congestion of the network is an important problem in network management. Proxy Server
It's like a big storage repository, it has a lot of cache space. If someone has been to a site through the proxy,
Lose some content, then proxy will be relevant information (including text, graphic, multimedia ...) Back up a copy of the proxy
The Server. When the user wants to access the same address through the proxy, the data can be obtained directly from the cache. One has
The proxy server will be able to control 90% of the usage under the proxy server. So not only is the user's access speed
Will improve, and the congestion of the network will be greatly improved.
(c) Multiple machines share an IP address
When a local area network has only one IP, it can be installed on the server proxy, the entire LAN users can pass a
IP access to the Internet, which will save the local area network users a large number of requests for IP costs.
WWW proxy Server including the main agent Gopher,http,ftp,telnet several services, a variety of agent services
The basic working principle is the same. HTTP Proxy Server is one of the most important parts of WWW proxy server
The implementation of the proxy server for HTTP is relatively simple, so here is a sample of the
The implementation method of the proxy server. This article only describes the implementation of the proxy server packet forwarding, because the cache is located
This article does not discuss the complexity of the theory.
1. Brower data format for accessing Web Server header files via proxy
Since it is not possible to interpret the HTTP protocol here, just give an example of the simplest data format:
Action: Brower get Http://202.196.48.9/index.htm
Brower the data sent to the proxy:
Brower Proxy Server
Get http://202.196.48.9/index.htm http/1.0
Accept: */*
Accept-language:zh-cn
Accept-encoding:gzip, deflate
user-agent:mozilla/4.0 (compatible; MSIE 4.01; Windows 95)
host:202.196.48.9
Proxy-connection:keep-alive
Server returns the data header of proxy
http/1.0 OK
server:microsoft-iis/2.0
Date:thu, Sep 1999 09:45:42 GMT
Content-type:text/html
Accept-ranges:bytes
Last-modified:wed, 1999 00:10:35 GMT
content-length:26402
2, the establishment of HTTP proxy server process:
1) proxy Start Monitoring Brower connection request
2) Proxy accepts Brower's application, brower data to Proxy
3) proxy resolves brower data, determines service type (HTTP), server address and service port number
4) Proxy Connection server
5) proxy starts the b_p_s (brower to Proxy to server) thread, which is responsible for brower the data to the
Proxy, and then the proxy to the server
6) b_p_s starts the S_p_b (server to Proxy to brower) thread, which is responsible for the server transmitting the data to the
Proxy, and then the proxy to the client
7) End of transfer complete b_p_s thread and s_p_b thread
8) loop start thread until all transfers are complete
Brower Proxy Server
1. Monitoring
2. Connect, send data
3. Parsing
Data
4. Connecting to a server 5. Start a thread
Initialization
b_p_s Threads
S_p_b Threads
Data
Data
Proxy_server Socket
Brower_proxy Socket
Brower_proxy Socket
Brower_proxy Socket
Proxy_server Socket
Proxy_server Socket
Receive
Send Receive
Send
The process of establishing an HTTP proxy server
3. Program Framework Structure
The HTTP protocol is a separate connection for each part of the Web page, each text, the picture as a separate unit
, in order to improve the efficiency of the proxy to prevent the main program blocking, deliberately start a level three thread.
The relationship between the threads
The main thread listens to the brower application on the proxy port and then initiates the b_p_s thread; b_p_s thread is responsible for accepting
Each brower application (each b_p_s is independent), at the completion of Brower to server one-way data transfer
When the s_p_b thread is started, the s_p_s thread completes the one-way data transfer from server to Brower. As Space
Limit, this article will briefly introduce the data analysis function, b_p_s thread function and s_p_b thread function sent to P by B.
(I) B data analysis issued to P
The main function of this section is to analyze the data that B sends to p to determine the service type and server-related information.
The client requests the server to accept HTTP data, and the first line of the HTTP command format it sends is: command
---URL---protocol, as mentioned above:
Get (command) http://202.196.48.9/index.htm (URL) http/1.0 (protocol)
The proxy program needs to determine if it is an HTTP service by looking up the "HTTP//" string in the URL, from the URL section
Resolves to the address of the Web server as the destination address for the proxy connection. At the same time, the "HTTP/server Address" section
Remove from the URL section to avoid some Web server redirection URLs.
int Getaddressandport (char * str, char *address, int * port)
{
Read "Get", "http://202.196.48.9/index.htm" and "HTTP1.0" respectively into S_cmd,s_server,
S_pro
Look for "http://" in S_pro, if not found, represents a non-HTTP service, returns 1
*port=80 http Default Port
Resolve Address "202.196.48.9" from S_server to *str
Remove the server address from the *STR, i.e. "Get http://202.196.48.9/index.htm HTTP1.0" è "get
/index.htm HTTP1.0 ", return 1
}
(II) b_p_s thread
This part of the function is: Brower the data to proxy, and then by proxy to the data to the server.
b_p_s threads are initiated by the main thread and are responsible for coordinating the thread initiation and synchronization of the S_p_b line throughout the proxy program
Ride. The thread replicates itself after accept Brower, because the Accept function is blocked and does not receive Brower
Connection request is blocked, there is at most one thread that is idle (in the Accept blocking state) at the same time.
UINT b_p_s (LPVOID pparm)
Main thread
1 Creating a listener socket
2 Monitoring the Brower application
3 Starting the b_p_s thread
b_p_s Threads
Accpet Brower
Copy yourself
Start S_p_b Thread
S_p_b Threads
{
Connection request from Accept Brower
AfxBeginThread (b_p_s) restart a b_p_s thread
Received a piece of brower sent data
Getaddressandport analyze the data and remove the server address from the data
Start S_p_b Thread
Wait 60 seconds or s_p_b line thread attached to connect to the server
Brower the data to proxy, and then to the server by proxy, loop until the data transfer is complete.
Wait for the s_p_b thread to end. Because b_p_s is the parent thread of thread s_p_b, before S_p_b ends b_p_s
Can't end early
}
(III) S_p_b
The main function of this part is: Server transfer data to proxy, and then to client by proxy
The S_p_b thread is started by the b_p_s thread and is a b_p_s child thread. Do not fit s_p_b thread into b_p_s line
The reason for this is that the s_p_b thread has a connect action that is blocking the remote server when the network transmission condition is more
When the action takes a lot of time, or even makes the program out of control, add time control to the b_p_s to protect
Operation of the certificate program. S_p_b is relatively simple, but because it is connected to the remote server, you must pay attention to the wrong
Handling of errors and exceptions, this article only introduces procedures, error and exception handling please refer to the relevant information.
UINT s_p_b (LPVOID pparm)
{
Resolving the domain name of the server
Create a new socket and connect to the server
The Server transmits the data to the proxy, then the proxy to the Brower, loops until the data transfer is complete
Return
}
From the above procedures can be seen, Proxy is Brower access server intermediary, it can monitor the brower issued by the
There are HTTP packets that block, forward, and log traffic to these packets. At the same time, it can also monitor the server
All HTTP packets emitted, blocking, forwarding, and logging of these packets.
For Browser, the HTTP service is connected, but not at the IP layer, but for the server, it
See is just an IP node. In an Internet environment, this is a one-way data transfer pattern.
All these characteristics, so that proxy becomes the core of network access management, the proxy simple extension, you can complete the Xu
Multifunctional. If the packet is filtered, it constitutes a fierwall, and if the traffic is recorded, it constitutes a
A billing system.
4, realize the meaning of proxy server
An HTTP proxy server is an important part of a small to Medium business Web Access solution, and its capabilities are
The simple extension, can constitute the access billing system and management system, therefore has the proxy server's kernel to the enterprise management
And programmers all make a lot of sense.
Zhou Yi Hous
(Department of Computer Communication, Zhengzhou University of Information Engineering)

Implementation of the HTTP proxy server under Windows

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.