Пример #1
0
void HttpServer::onConnection(const TcpConnectionPtr& conn)
{
     LOG_INFO("HttpServer::onConnection get one client %d", conn->fd());
     if (conn->connected())
     {
         conn->setContext(HttpContext());
     }
}
Пример #2
0
void HttpServer::response(const TcpConnectionPtr& conn, const HttpRequest& req)
{
    const string& connection = req.getHeader("Connection");
    bool close = connection == "close" || (req.version() == HTTP_VERSION_1_0 && connection != "Keep-Alive");

    HttpResponse response(close);
    response.setStatusCode(HttpStatusOk);
    response.setServerName("MyHttpServer");

    methodCallback(req, &response);    // callback, for init response

    ByteBuffer buf;
    response.compileToBuffer(&buf);
    //printf("[%s]\n", buf.toString().c_str());
    conn->send(&buf);

    LOG_INFO("HttpServer::response send data [%d]", conn->fd());
    if (response.closeConnection())
    {
        LOG_INFO("HttpServer::response close this[%d]", conn->fd());
        conn->shutdown();
    }
}
Пример #3
0
void HttpServer::onMessage(const TcpConnectionPtr& conn, ByteBuffer *buf, Timestamp receiveTime)
{
     LOG_INFO("HttpServer::onConnection recv data [%d][%s]", conn->fd(), buf->toString().c_str());

     HttpContext *context = zl::stl::any_cast<HttpContext>(conn->getMutableContext());
     assert(context);
     if(!context->parseRequest(buf, receiveTime))
     {
         conn->send("HTTP/1.1 400 Bad Request\r\n\r\n");
         conn->send("HTTP/1.1 400 Bad Request\r\n\r\n");
         conn->shutdown();
     }

     if (context->gotAll())
     {
         LOG_INFO("HttpServer::onMessage  parse request over.");
         response(conn, context->request());
         context->reset();  // process request and return response, then reset, for long-connection
     }
}
Пример #4
0
 void onMessage(const TcpConnectionPtr& conn, ByteBuffer *buf, Timestamp time)
 {
     string msg(buf->retrieveAllAsString());
     printf("onMessage(): recv a message [%s]\n", msg.c_str());
     LOG_INFO("[%d] recv %d bytes at %s", conn->fd(), msg.size(), time.toString().c_str());
 }
Пример #5
0
void defaultMessageCallback(const TcpConnectionPtr& conn, ByteBuffer* buf, Timestamp receiveTime)
{
    LOG_INFO("defaultMessageCallback : [%d][%s]", conn->fd(), buf->toString().c_str());
}
Пример #6
0
	void SpcServer::onMessage(TcpConnectionPtr conn){
		string msg=conn->receive();
		cout << msg << endl;
		MyTask task(msg, conn->fd());
		_threadPool.addTask(std::bind(& MyTask::execute, task));
	}