Server::Server(const std::string& address, uint16_t port)
 {
   m_sock = -1;
   m_address = address;
   m_port = port;
   SetEncapsulatedFormat(Json::Rpc::RAW);
 }
 Client::Client(const std::string& address, uint16_t port)
 {
   m_sock = -1;
   m_address = address;
   m_port = port;
   SetEncapsulatedFormat(Json::Rpc::RAW);
   memset(&m_sockaddr, 0x00, sizeof(struct sockaddr_storage));
   m_sockaddrlen = 0;
 }
    bool HttpServer::Recv(int fd)
    {
      Json::Value response;
      ssize_t nb = -1;
      char buf[1500];

      nb = ::recv(fd, buf, sizeof(buf), 0);

      /* give the message to JsonHandler */
      if(nb > 0)
      {
        std::string msg = std::string(buf, nb);

        if(GetEncapsulatedFormat() == Json::Rpc::NETSTRING)
        {
          try
          {
            msg = netstring::decode(msg);
          }
          catch(const netstring::NetstringException& e)
          {
            /* error parsing Netstring */
            std::cerr << e.what() << std::endl;
            return false;
          }
        }
        if (0 == msg.compare(0,15,"POST / HTTP/1.1"))
        {
            SetEncapsulatedFormat(Json::Rpc::HTTP_POST);
        }
        else if (m_httpmsgs.count(fd) > 0)
        {
            SetEncapsulatedFormat(Json::Rpc::HTTP_POST);
            msg = m_httpmsgs[fd] + msg;
        }
        //std::cerr << msg << std::endl;
        if (GetEncapsulatedFormat() == Json::Rpc::HTTP_POST)
        {
                //printf("orig_msg: %s\n\n", msg.c_str()); fflush(stdout);
                size_t istart = msg.find("Content-Length: ");
                size_t iend = msg.find("\r\n\r\n{");
                if (iend == std::string::npos)
                {
                    m_httpmsgs[fd] = msg;
                    return false;
                }
                size_t content_msg_size = ::atoi(msg.substr(istart+16,iend-istart-16).c_str());
                //printf("content_msg_size: %d\n", content_msg_size); fflush(stdout);
                std::string msg_json = msg.substr(iend+4);
                //printf("msg_json: %s msg_size: %d\n", msg_json.c_str(), msg_json.size()); fflush(stdout);
                if (content_msg_size != msg_json.size())
                {
                    m_httpmsgs[fd] = msg;
                    return false;
                }
                msg = msg_json;
                m_httpmsgs.erase(fd);
        }

        m_jsonHandler.Process(msg, response);

        /* in case of notification message received, the response could be Json::Value::null */
        if(response != Json::Value::null)
        {
          Send(fd, m_jsonHandler.GetString(response));
        }

        return true;
      }
      else
      {
        m_purge.push_back(fd);
        return false;
      }
    }