How to write simple HTTP proxy with Boost.Asio

How to write simple HTTP proxy with Boost.Asio

Russian · English

In this article I describe process of writing of simple cross-platform HTTP proxy.

What we need

To develop this example (source code) I used Boost version 1.35. To build example, you can use cmake (but you can also build sources manually). To configure and build you need to run following commands (on Unix-like OSes)1:

> cmake .
> make

and after compilation you'll get proxy-asio-async executable, that you can run from command line. This program accepts only one argument — number of threads, that will perform request processing (by default, this value is equal 2). Port number on which requests will accepted is hardcoded in source code and equal to 100012.

Architecture

As in previous examples, our program consists from three parts:

  • the main function, that parses command line, creates separate threads for asio services together with server object, and then enters into request processing loop;
  • server class, that accepts requests, and creates connection object, that implements all logic of connection handling;
  • connection class, that implements all logic, and pass data between client & web-server.

The data processing is performed in asynchronous mode, and to distribute load between processors, we can use several independent asio services, that perform dispatching of calls (asio::io_service).

Note: Most hard part of the development of asynchronous code is proper design of data flow. I usually draw a state diagram and then transform each state to separate function. Presence of such diagram is very helpful for understanding of code by other developers.

Implementation

The main function is pretty simple, so we'll not analyze it — you can just look to its source code and understand, what it does (all common definitions are in file common.h.

Implementation of server (the server class — proxy-server.hpp & proxy-server.cpp) also not so much different from previous examples — changes were made only for method, that is used to select service, that will implement dispatching. In our example new service is selected from circular list of services, that allow us to get some load balancing for requests.

All data processing logic is implemented in connection class (proxy-conn.hpp & proxy-conn.cpp. I want to say, that parsing of headers was done without any optimisation3.

Data processing is started from call to start function from server class, that accepts connection and creates new object of connection class. This function initiates asynchronous reading of request headers from browser.

Reading of request headers is performed in the handle_browser_read_headers function, that is called when we get some part of data from browser. I need to mention, that if we get incomplete headers (there is no empty string (\r\n\r\n)), then this function initiates new reading of headers, trying to get them all.

After we get all headers, this function parses them and extracts version of HTTP protocol, used method and address of web-server (some of these data will be required to detect persistent connections).

After parsing of headers, this function calls start_connect, that parses address of web-server, and if we don't have opened connection to this server, then it initiates process of name resolution. If we have opened connection, then we simply start data transfer with start_write_to_server function.

The handle_resolve function is called after name resolution, and if we get address of server, then it initiates process of connection establishing. Result of this process is handled by handle_connect function, that initiates process of data transfer to the server with start_write_to_server function, that forms correct headers, and pass these data to the server.

After transferring data to server, in function handle_server_write we initiate reading of response (only headers first) from server. Processing of headers is handled by handle_server_read_headers function, that is similar to the handle_browser_read_headers, but it also tries to understand — should we close connection after data transfer, or not. After processing of headers, this function initiates process of sending data to browser.

After sending of headers, we create a loop, that transfer body of response from server to browser. In this loop we use two functions — handle_server_read_body and handle_browser_write, each of them calls another function until we don't finish reading of data from server (either number of bytes, specified in headers) or don't get end of file.

If we'll get end of file, then we'll pass rest of data to the browser and close connection. Or if we use persistent connection, then we'll pass control to the start function, that initiates reading of new headers from browser.

That's all. As I already mentioned above, main problem — building of right data flow sequence.


1. If cmake can't find required libraries, you can specify their location with two <em>cmake's variables — CMAKE_INCLUDE_PATH и CMAKE_LIBRARY_PATH, by running cmake following way:

> cmake . -DCMAKE_INCLUDE_PATH=~/exp/include -DCMAKE_LIBRARY_PATH=~/exp/lib

2. I could also implement code, that allow to specify port number in command line, but I was lazy, as this example was just a prototype to check some of my ideas.

3. There is also cpp-netlib project, that has (development in progress) parsers for basic protocols — HTTP, SMTP и т.п.

How to write simple HTTP proxy with Boost.Asio的更多相关文章

  1. 使用Boost.Asio编写通信程序

    摘要:本文通过形像而活泼的语言简单地介绍了Boost::asio库的使用,作为asio的一个入门介绍是非常合适的,可以给人一种新鲜的感觉,同时也能让体验到asio的主要内容. Boost.Asio是一 ...

  2. boost.asio包装类st_asio_wrapper开发教程(2013.12.8更新)(二)

    如果你是偶然浏览到这里,请先看 源代码及例程下载地址:命令行:svn checkout http://st-asio-wrapper.googlecode.com/svn/trunk/ st-asio ...

  3. c++ boost asio库初学习

    前些日子研究了一个c++的一个socket库,留下范例代码给以后自己参考. 同步server: // asio_server.cpp : コンソール アプリケーションのエントリ ポイントを定義します. ...

  4. 如何在多线程leader-follower模式下正确的使用boost::asio。

    #include <assert.h> #include <signal.h> #include <unistd.h> #include <iostream& ...

  5. BOOST.Asio——Tutorial

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  6. BOOST.Asio——Overview

    =================================版权声明================================= 版权声明:原创文章 谢绝转载  啥说的,鄙视那些无视版权随 ...

  7. boost asio sync

    Service: #include<boost/asio.hpp> #include<boost/thread.hpp> #include<iostream> #i ...

  8. 网络库crash以及boost asio strand dispath分析

    最近在做服务器的稳定性的相关测试,服务器的网络底层使用的是boost asio,然后自己做的二次封装以更好的满足需求. 服务器昨天晚上发现crash了一次,之前测试了将近半个多月,有一次是莫名的退出了 ...

  9. boost asio tcp server 拆分

    从官方给出的示例中对于 boost::asio::ip::tcp::acceptor 类的使用,是直接使用构造函数进行构造对象,这一种方法用来学习是一个不错的方式. 但是要用它来做项目却是不能够满足我 ...

随机推荐

  1. 关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10

    关于 调用 JNI JAR 的说明和注意事项,调用第三方 JAR SDK 和 翻译 安卓 JAVA 代码 的说明 V2015.6.10 转载请标明出处,否则死全家.选择[复制链接]即可得到出处. (* ...

  2. OpenRisc-35-基于orpsoc,eCos的sd card controller的测试实验

    引言 之前,曾经在orpsoc的平台上,测试验证过其sd card controller的linux的驱动,但是并不是很完美,经过努力,终于在eCos下完成了其全部功能的验证,包括驱动层验证,文件系统 ...

  3. 基于visual Studio2013解决面试题之0510连续数之和

     题目

  4. HDU 4350 Card

    打表找规律,比赛应付了一下,其实还可以把内存再优化一半掉,下面的0都是手动填充的,可以优化掉 题意: T个测试数据 下面52个数字表示 从栈顶到栈底的52个数 n l r表示 从栈顶下数 [l,r] ...

  5. 如何在SourceInsight中选中匹配的大括号中的内容

    如何在SourceInsight中选中匹配的大括号中的内容 要分析的代码很长,多个for,if等分析嵌套在一起,代码有点乱,找到了这个分支的头,却不知道尾在哪,找到了尾却不知道哪是开头,在网上找了一下 ...

  6. OCP-1Z0-051-题目解析-第33题

    33. You want to create an ORD_DETAIL table to store details for an order placed having the following ...

  7. C++学习之路—多态性与虚函数(一)利用虚函数实现动态多态性

    (根据<C++程序设计>(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 多态性是面向对象程序设计的一个重要特征.顾名思义 ...

  8. JQuery - 留言之后,不重新加载数据,直接显示发表内容

    留言板中,发表信息的时候,使用Ajax存储到后台数据库,如果存储成功,不重新加载数据库,直接显示发表内容. 代码: var Nicehng = ''; var kkimgpath = ''; var ...

  9. GetCursorPos/WindowFromPoint/SendMessage

    GetCursorPos/WindowFromPoint/SendMessage (用API函数向Edit框发送字符) GetCursorPos(mPoint); DTWND:=WindowFromP ...

  10. Android高手进阶——Adapter深入理解与优化

    Android高手进阶--Adapter深入理解与优化 通常是针对包括多个元素的View,如ListView,GridView.ExpandableListview,的时候我们是给其设置一个Adapt ...