Example #1
0
bool RestServ::user_auth(mg_connection& nc, HttpMessage data)
{
    char user[64]{0x00}, pass[64]{0x00};
    auto ul = mg_get_http_var(&(data.get()->body), "user", user, sizeof(user));
    auto pl = mg_get_http_var(&(data.get()->body), "pass", pass, sizeof(pass));

    try{
        if (ul > 0 && pl > 0){
            //TODO Check valid
        }else{
        }

    } catch(std::exception& e){
        out_ << e.what();
        return false;
    }

    return true;
}
Example #2
0
int ACDAudio::run(void* pArg)
{
   Os::Logger::instance().log(FAC_ACD, gACD_DEBUG, "ACDAudio::run - starting get from: %s", mUriString.data());

   HttpMessage *pGetRequest = new HttpMessage;

   pGetRequest->get(mUri, HTTP_GET_TIMEOUT);

   UtlString status;

   pGetRequest->getResponseStatusText(&status);

   if (status == "OK") {
      UtlString audioData;
      ssize_t audioLength;

      const HttpBody* pResponseBody = pGetRequest->getBody();
      pResponseBody->getBytes(&audioData, &audioLength);

      Os::Logger::instance().log(FAC_ACD, gACD_DEBUG, "ACDAudio::run - received %zd bytes from: %s\n",
                    audioLength, mUriString.data());

      // Now save the downloaded audio to a local file
      size_t writeLength;

      OsFile audioFile(mAudioPath);
      if (audioFile.open(OsFile::CREATE) != OS_SUCCESS) {
         Os::Logger::instance().log(FAC_ACD, PRI_ERR, "ACDAudio::run - "
                       "Unable to create audio file: %s", mAudioPath.data());
      }
      else {
         audioFile.write(audioData, audioLength, writeLength);
         audioFile.close();
      }
   }
   else {
      Os::Logger::instance().log(FAC_ACD, PRI_ERR, "ACDAudio::run - failed get from: %s", mUriString.data());
   }

   delete pGetRequest;

   return 0;
}
Example #3
0
std::shared_ptr<Session> RestServ::push_session(HttpMessage data)
{
    char user[64]{0x00}, pass[64]{0x00};
    auto ul = mg_get_http_var(&(data.get()->body), "user", user, sizeof(user));
    auto pl = mg_get_http_var(&(data.get()->body), "pass", pass, sizeof(pass));

    auto s = std::make_shared<Session>();

    s->created = s->last_used = mg_time();
    s->user = std::string(user, ul);
    s->pass = std::string(pass, pl);

    s->id = std::hash<std::shared_ptr<Session>>()(s);
    std::string&& seed = std::string(user) + std::to_string(s->id);
    s->id = std::hash<std::string>()(seed);

    session_list_.push_back(s);

    return s;
}
Example #4
0
bool RestServ::remove_from_session_list(HttpMessage data)
{
    mg_str* cookie_header = mg_get_http_header(data.get(), "cookie");;
    if (cookie_header == nullptr) 
        return false;

    char ssid[32]{0x00};
    if (!mg_http_parse_header(cookie_header, SESSION_COOKIE_NAME, ssid, sizeof(ssid)))
        return false;

    auto sid = std::stoul(ssid, nullptr, 10);

    for (auto iter = session_list_.begin(); iter != session_list_.end(); ++iter)
    {
        if ( (*iter)->id == sid )
        {
            iter = session_list_.erase(iter);
        }
    }

    return true;
}
Example #5
0
std::shared_ptr<Session> RestServ::get_from_session_list(HttpMessage data)
{
    mg_str* cookie_header = mg_get_http_header(data.get(), "cookie");;
    if (cookie_header == nullptr) 
        return nullptr;

    char ssid[32]{0x00};
    if (!mg_http_parse_header(cookie_header, SESSION_COOKIE_NAME, ssid, sizeof(ssid)))
        return nullptr;

    auto sid = std::stoul(ssid, nullptr, 10);

    auto ret = std::find_if(session_list_.begin(), session_list_.end(), [&sid](std::shared_ptr<Session> p){
            return sid == p->id;
            });

    if (ret == session_list_.end())
        return nullptr;

    (*ret)->last_used = mg_time();
    return *ret;
}
Example #6
0
bool XmlRpcRequest::execute(XmlRpcResponse& response)
{
   bool result = false;

   // End of constructing the XML-RPC body
   mpRequestBody->append(END_PARAMS END_METHOD_CALL);

   if (OsSysLog::willLog(FAC_XMLRPC, PRI_INFO))
   {
      UtlString logString;
      ssize_t   logLength;
      mpRequestBody->getBytes(&logString, &logLength);
      if (logString.length() > XmlRpcBody::MAX_LOG)
      {
         logString.remove(XmlRpcBody::MAX_LOG);
         logString.append("\n...");
      }
      UtlString urlString;
      mUrl.toString(urlString);
      OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                    "XmlRpcRequest::execute XML-RPC to '%s' request =\n%s",
                    urlString.data(),
                    logString.data());
   }

   mpHttpRequest->setContentLength(mpRequestBody->getLength());
   mpHttpRequest->setBody(mpRequestBody);
   mpRequestBody = NULL; // the HttpMessage now owns the request body

   // Create an empty response object and sent the built up request
   // to the XML-RPC server
   HttpMessage httpResponse;

   int statusCode = httpResponse.get(mUrl,*mpHttpRequest,XML_RPC_TIMEOUT,true /* persist conn */ );
   if (statusCode/100 == 2)
   {
      UtlString bodyString;
      ssize_t   bodyLength;

      httpResponse.getBody()->getBytes(&bodyString, &bodyLength);

      UtlString logString;
      if (bodyString.length() > XmlRpcBody::MAX_LOG)
      {
         logString.append(bodyString, 0, XmlRpcBody::MAX_LOG);
         logString.append("\n...");
      }
      else
      {
         logString = bodyString;
      }

      if (response.parseXmlRpcResponse(bodyString))
      {
         result = true;
         OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                       "XmlRpcRequest::execute XML-RPC received valid response = \n%s",
                       logString.data());
      }
      else
      {
         OsSysLog::add(FAC_XMLRPC, PRI_ERR,
                       "XmlRpcRequest::execute XML-RPC received fault response = \n%s",
                       logString.data());
      }
   }
   else if (statusCode == -1)
   {
      response.setFault(XmlRpcResponse::ConnectionFailure, CONNECTION_FAILURE_FAULT_STRING);

      OsSysLog::add(FAC_XMLRPC, PRI_ERR,
                    "XmlRpcRequest::execute http connection failed");
   }
   else // some non-2xx HTTP response
   {
      UtlString statusText;

      httpResponse.getResponseStatusText(&statusText);
      response.setFault(XmlRpcResponse::HttpFailure, statusText.data());

      OsSysLog::add(FAC_XMLRPC, PRI_INFO,
                    "XmlRpcRequest::execute http request failed; status = %d %s",
                    statusCode, statusText.data());
   }

   return result;
}
Example #7
0
void RestServ::httpStatic(mg_connection& nc, HttpMessage data)
{
    mg_serve_http(&nc, data.get(), httpoptions_);
}