#include "sockets/tcpsocket.h" #include "sockets/http.h" #include // g++-mp-4.5 -std=c++0x -I ../common testhttp.cpp -D_UNIX -L/opt/local/lib -lboost_regex-mt -lboost_thread-mt ../common/stringutils.cpp -liconv class httpsvrcb : public http::callback { stream_ptr _s; public: httpsvrcb(stream_ptr s) : _s(s) { } virtual void status(const std::string& version, const std::string& status, const std::string& desc) { printf("v%s stat=%s: '%s'\n", version.c_str(), status.c_str(), desc.c_str()); } virtual void request(const std::string& verb, const std::string& request, const std::string& request_version) { printf("req: %s %s %s\n", verb.c_str(), request.c_str(), request_version.c_str()); http svr(_s); headers reply; reply.add("Content-Type", "text/plain"); reply.add("Content-Size", "4"); svr.respond("200", reply, "data"); } virtual void headers(const http::headers& h) { printf("headers:\n%s\n", h.str().c_str()); } virtual void data(const uint8_t*data, size_t n) { printf("data:\n%s\n", hexdump(data, n).c_str()); } }; class httpcltcb : public http::callback { public: virtual void status(const std::string& version, const std::string& status, const std::string& desc) { printf("v%s stat=%s: '%s'\n", version.c_str(), status.c_str(), desc.c_str()); } virtual void request(const std::string& verb, const std::string& request, const std::string& request_version) { printf("req: %s %s %s\n", verb.c_str(), request.c_str(), request_version.c_str()); } virtual void headers(const http::headers& h) { printf("headers:\n%s\n", h.str().c_str()); } virtual void data(const uint8_t*data, size_t n) { printf("data:\n%s\n", hexdump(data, n).c_str()); } }; class bufstream : public stream { ByteQueue &_a; ByteQueue &_b; public: bufstream(ByteQueue& a, ByteQueue& b) : _a(a), _b(b) { } virtual size_t write(const unsigned char* data, size_t len) { _a.write(data, len); return len; } virtual size_t read(unsigned char* data, size_t len) { return _b.read(data, len); } virtual void shutdown() { _a.stop(); _b.stop(); } }; class line { ByteQueue _a2b; ByteQueue _b2a; stream_ptr _a; stream_ptr _b; public: line() : _a2b(1600, "a2b"), _b2a(1600, "b2a"), _a(new bufstream(a2b, b2a)), _b(bufstream(b2a, a2b)) { } stream_ptr a() { return _a; } stream_ptr b() { return _b; } }; int main(int, char**) { try { http::callback_ptr cltcb(new httpcltcb()); http::request req1("http://localhost/test?a=b&c=d"); req1.addhdr("Connection", "keep-alive"); line l; http::callback_ptr svrcb(new httpsvrcb(l.a())); http server(l.a(), svrcb); http client(l.b()); client.get(req1, cltcb); cltcb->wait(); } catch(...) { printf("E\n"); } printf("done\n"); return 0; }