Beispiel #1
0
abyss_bool HandleTime(TSession *r)
{
    char z[50];
    time_t ltime;
    TDate date;
    const char * dateString;
    const char * answer;

    if (strcmp(r->uri,"/time")!=0)
        return FALSE;

    if (!RequestAuth(r,"Mot de passe","moez","hello"))
        return TRUE;

    time(&ltime);
    DateFromGMT(&date, ltime);

    DateToString(&date, &dateString);

    xmlrpc_asprintf(&answer, "The time is %s", dateString);

    Answer(r, 200, answer);

    xmlrpc_strfree(dateString);
    xmlrpc_strfree(answer);

    return TRUE;
}
Beispiel #2
0
	void AuthorizeEntry (ICLEntry *entry)
	{
		const auto account = entry->GetParentAccount ();
		const auto& id = entry->GetHumanReadableID ();
		account->Authorize (entry->GetQObject ());
		account->RequestAuth (id);

		const auto& e = Util::MakeANCancel ("org.LeechCraft.Azoth",
				"org.LC.Plugins.Azoth.AuthRequestFrom/" + entry->GetEntryID ());
		Core::Instance ().SendEntity (e);
	}
Beispiel #3
0
void CgiResponseHandler::SendResponse(MgHttpResponse* response)
{
    MG_TRY()

    Ptr<MgHttpResult> result = response->GetResult();
    STATUS status = result->GetStatusCode();
    if (status != 200)
    {
        STRING statusMessage = result->GetHttpStatusMessage();
        if (statusMessage == MapAgentStrings::FailedAuth1 ||
            statusMessage == MapAgentStrings::FailedAuth2)
        {
            RequestAuth();
        }
        else
        {
            //TODO: Use a resource for the HTML error message
            STRING shortError = result->GetErrorMessage();
            STRING longError = result->GetDetailedErrorMessage();
            printf(MapAgentStrings::StatusHeader, status, MG_WCHAR_TO_CHAR(statusMessage));
            printf(MapAgentStrings::ContentTypeHeader, MapAgentStrings::TextHtml, "");
            printf("\r\n"
                "<html>\n<head>\n"
                "<title>%s</title>\n"
                "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">\n"
                "</head>\n"
                "<body>\n<h2>%s</h2>\n%s\n</body>\n</html>\n",
                MG_WCHAR_TO_CHAR(statusMessage),
                MG_WCHAR_TO_CHAR(shortError),
                MG_WCHAR_TO_CHAR(longError));

            DumpMessage(MG_WCHAR_TO_CHAR(longError));
        }
    }
    else
    {
        DumpMessage(MapAgentStrings::StatusOkHeader);

        // Status was ok.  Send the real result back.
        STRING contentType = result->GetResultContentType();
        STRING stringVal;

        printf(MapAgentStrings::StatusOkHeader);
        if (contentType.length() > 0)
        {
            // If we are returning text, state that it is utf-8.
            string charSet = "";
            if (contentType.find(L"text") != contentType.npos)  //NOXLATE
            {
                charSet = MapAgentStrings::Utf8Text;
            }
            printf(MapAgentStrings::ContentTypeHeader, MG_WCHAR_TO_CHAR(contentType), charSet.c_str());
        }
        else
        {
            printf(MapAgentStrings::ContentTypeHeader, MapAgentStrings::TextPlain, MapAgentStrings::Utf8Text);
        }

        Ptr<MgByteReader> outputReader;
        Ptr<MgDisposable> resultObj = result->GetResultObject();
        MgDisposable* pResultObj = (MgDisposable*)resultObj;

        if (NULL != dynamic_cast<MgByteReader*>(pResultObj))
        {
            outputReader = (MgByteReader*) SAFE_ADDREF(pResultObj);
        }
        else if (NULL != dynamic_cast<MgStringCollection*>(pResultObj))
        {
            outputReader = ((MgStringCollection*)pResultObj)->ToXml();
        }
        else if (NULL != dynamic_cast<MgSpatialContextReader*>(pResultObj))
        {
            outputReader = ((MgSpatialContextReader*)pResultObj)->ToXml();
        }
        else if (NULL != dynamic_cast<MgLongTransactionReader*>(pResultObj))
        {
            outputReader = ((MgLongTransactionReader*)pResultObj)->ToXml();
        }
        else if (NULL != dynamic_cast<MgHttpPrimitiveValue*>(pResultObj))
        {
            stringVal = ((MgHttpPrimitiveValue*)pResultObj)->ToString();
        }

        if (stringVal.length() > 0)
        {
            string utf8 = MG_WCHAR_TO_CHAR(stringVal);
            printf(MapAgentStrings::ContentLengthHeader, utf8.length());
            printf("\r\n%s",utf8.c_str());
        }
        else if (outputReader != NULL)
        {
            Ptr<MgHttpHeader> respHeader = response->GetHeader();
            //Check for chunking hint
            if (respHeader->GetHeaderValue(MgHttpResourceStrings::hrhnTransfer_Encoding) == MgHttpResourceStrings::hrhnChunked)
            {
                CgiReaderStreamer crs(outputReader);
                crs.StreamResult();
            }
            else
            {
                INT64 outLen = outputReader->GetLength();
                printf(MapAgentStrings::ContentLengthHeader,(INT32)outLen);
                printf("\r\n");
                unsigned char buf[4096];
                int nBytes = outputReader->Read(buf,4096);
                while (nBytes > 0)
                {
                    fwrite(buf, 1, nBytes, stdout);
                    nBytes = outputReader->Read(buf,4096);
                }
            }
        }
        else
        {
            printf(MapAgentStrings::ContentLengthHeader, 0);
            printf("\r\n");
        }
    }

    MG_CATCH_AND_THROW(L"CgiResponseHandler.SendResponse");
}
Beispiel #4
0
c_RestResponse_HttpData* c_RestResponse::PrepareHttpData(c_RestRequest* RestRequest)
{
  
try
{


    
  char tempHeader[4096];
  
  //string sResponseHeader;
  Ptr<MgByteReader> outputReader;

  Ptr<c_RestResult> result = GetResult();
  STATUS status = result->GetStatusCode();
  STRING statusMessage = result->GetHttpStatusMessage();
  if (status != 200)
  {
    
    if (statusMessage == MapAgentStrings::FailedAuth1 ||
      statusMessage == MapAgentStrings::FailedAuth2
      )
    {
      RequestAuth(RestRequest);
    }
    else
    {
      //TODO: Use a resource for the HTML error message
      char content[4096];

      STRING shortError = result->GetErrorMessage();
      STRING longError = result->GetDetailedErrorMessage();

      sprintf(content,"\r\n"
        "<html>\n<head>\n"
        "<title>%s</title>\n"
        "<meta http-equiv=\"Content-type\" content=\"text/html; charset=utf-8\">\n"
        "</head>\n"
        "<body>\n<h2>%s</h2>\n%s\n</body>\n</html>\n",
        MG_WCHAR_TO_CHAR(statusMessage),
        MG_WCHAR_TO_CHAR(shortError),
        MG_WCHAR_TO_CHAR(longError));

      DWORD  content_length = strlen(content);

      /*
      sprintf(tempHeader, MapAgentStrings::StatusHeader, status, MG_WCHAR_TO_CHAR(statusMessage));
      sResponseHeader.append(tempHeader);

      sprintf(tempHeader, MapAgentStrings::ContentLengthHeader, (int)content_length);
      sResponseHeader.append(tempHeader);

      sprintf(tempHeader, MapAgentStrings::ContentTypeHeader, MapAgentStrings::TextHtml, "");
      sResponseHeader.append(tempHeader);
      */
      
      m_HttpData.SetStatusAndReason(status,MG_WCHAR_TO_CHAR(statusMessage));      
      m_HttpData.AddHeader(MapAgentStrings::ContentTypeKey,MapAgentStrings::TextHtml);
      sprintf(tempHeader, "%d", (int)content_length);
      m_HttpData.AddHeader(MapAgentStrings::ContentLengthKey,tempHeader);
      
      
      m_HttpData.SetContent(content);
  
    }
  }
  else
  {

    // Status was ok.  Send the real result back.
    STRING result_contentType = result->GetResultContentType();
    STRING response_contentType = GetContentType();
    std::string stringval_utf8;

    //sprintf(tempHeader, MapAgentStrings::StatusOkHeader);
    //sResponseHeader.append(tempHeader);
    
    m_HttpData.SetStatusAndReason(200,"OK");
    

    if (response_contentType.length() > 0)
    {
      // If we are returning text, state that it is utf-8.
      
      STRING mime = response_contentType;
      if( response_contentType == RestMimeType::JsonP )
      {
        mime = L"text/plain";
      }
      else
      { 
        if( response_contentType == RestMimeType::Json )
        {
          mime = L"text/plain";
        }
      } 
      
      string charSet = "";
      if (mime.find(L"text") != mime.npos)  //NOXLATE
      {
        charSet = MapAgentStrings::Utf8Text;
      }
      //sprintf(tempHeader, MapAgentStrings::ContentTypeHeader, MG_WCHAR_TO_CHAR(response_contentType), charSet.c_str());
      //sResponseHeader.append(tempHeader);
      sprintf(tempHeader, "%s%s", MG_WCHAR_TO_CHAR(mime), charSet.c_str());
      m_HttpData.AddHeader(MapAgentStrings::ContentTypeKey,tempHeader);
    }
    else
    {
      //sprintf(tempHeader, MapAgentStrings::ContentTypeHeader, MapAgentStrings::TextPlain, MapAgentStrings::Utf8Text);
      //sResponseHeader.append(tempHeader);
      sprintf(tempHeader, "%s%s", MapAgentStrings::TextPlain, MapAgentStrings::Utf8Text);
      m_HttpData.AddHeader(MapAgentStrings::ContentTypeKey,tempHeader);
    }
    



    Ptr<MgDisposable> resultObj = result->GetResultObject();
    MgDisposable* pResultObj = (MgDisposable*)resultObj;

    // Test type of response needed
    Ptr<c_RestDataReader> restreader = NULL;

    MgProxyFeatureReader* proxyfreader = dynamic_cast<MgProxyFeatureReader*>(pResultObj);
    if( proxyfreader )
    {
      restreader = new c_RestDataReader_MgFeatureReader(proxyfreader);
      
    }
    else
    {
      restreader = SAFE_ADDREF(dynamic_cast<c_RestDataReader*>(pResultObj));
    }
    
    
    if( RestRequest->m_CfgRepresentation && RestRequest->m_CfgRepresentation->GetType() == c_CfgRepresentation::e_Template)
    {
      if( response_contentType == RestMimeType::Html  )
      {      
        c_FeatureReaderToHtml::ToHtml(restreader,RestRequest,RestRequest->GetOriginalFullUri(),RestRequest->GetBaseUri(),stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);            
        
      }
      else
      {
        if( response_contentType == RestMimeType::Kml  )
        {
            c_FeatureReaderToHtml::ToKml(restreader.p,RestRequest,RestRequest->GetOriginalFullUri(),RestRequest->GetBaseUri(),stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);            
        }
        else
        {
          if( response_contentType == RestMimeType::Kmz  )
          {
              outputReader = c_FeatureReaderToHtml::ToKmz(restreader.p,RestRequest,RestRequest->GetOriginalFullUri(),RestRequest->GetBaseUri(),result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);
          }
          else
          {
            c_FeatureReaderToHtml::ToTemplate(false,restreader,RestRequest,RestRequest->GetOriginalFullUri(),RestRequest->GetBaseUri(),stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);              
            
          }
        }
      }
    }
    
    if( RestRequest->m_CfgRepresentation && RestRequest->m_CfgRepresentation->GetType() == c_CfgRepresentation::e_FeaturesXML)
    {
      if( restreader.p )
      {
        c_FeatureReaderToXML::ToXml(restreader.p,stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);
      }
    }
    if( RestRequest->m_CfgRepresentation && RestRequest->m_CfgRepresentation->GetType() == c_CfgRepresentation::e_Sitemap)
    {
      if( restreader.p )
      {
        c_FeatureReaderToXML::ToSitemapXml(restreader.p,RestRequest,RestRequest->GetBaseUri(),stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);
      }
    }
    if( RestRequest->m_CfgRepresentation && RestRequest->m_CfgRepresentation->GetType() == c_CfgRepresentation::e_FeaturesJSON)
    {
      if( restreader.p )
      {
        c_FeatureReaderToGeoJson::ToGeoJson(restreader.p,stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);
      }
    }
    if( RestRequest->m_CfgRepresentation && RestRequest->m_CfgRepresentation->GetType() == c_CfgRepresentation::e_Custom)
    {
      if( restreader.p )
      {
        //std::stringstream strs;
        c_CfgRepresentation_Custom *customrep =  (c_CfgRepresentation_Custom *)RestRequest->m_CfgRepresentation;
        
        Ptr<c_StreamResponse_FReader2Custom> streamout = new c_StreamResponse_FReader2Custom(restreader.p,customrep);
        m_HttpData.SetContent(streamout);
        
        return &m_HttpData;  // need to go out here - bellow is closing featuer reader - setting hhtp header for content length etc..
         
        ///(*(customrep->m_CustomRenderer->m_FuncContent2Stream))(restreader.p,&strs);
        
        //stringval_utf8 = strs.str();
        //c_FeatureReaderToGeoJson::ToGeoJson(restreader.p,stringval_utf8,result->m_FeatureReader_StartIndex,result->m_FeatureReader_Count);
      }
    }
    if( restreader.p )
    {
      restreader->Close();
    }
    
    if (NULL != dynamic_cast<c_RestPrimitiveValue*>(pResultObj))
    { 
      stringval_utf8 = ((c_RestPrimitiveValue*)pResultObj)->ToStringUTF8();;

    }
    else if (NULL != dynamic_cast<MgByteReader*>(pResultObj))
    {
      outputReader = (MgByteReader*) SAFE_ADDREF(pResultObj);

    }
    else if (NULL != dynamic_cast<MgStringCollection*>(pResultObj))
    {
      outputReader = ((MgStringCollection*)pResultObj)->ToXml();

    }
    else if (NULL != dynamic_cast<MgSqlDataReader*>(pResultObj))
    {
      outputReader = ((MgSqlDataReader*)pResultObj)->ToXml();

    }
    else if (NULL != dynamic_cast<MgDataReader*>(pResultObj))
    {
      outputReader = ((MgDataReader*)pResultObj)->ToXml();

    }    
    

    if (stringval_utf8.length() > 0)
    {

      if( response_contentType == RestMimeType::JsonP || 
        ( response_contentType == RestMimeType::Json && (GetJsonpCallbackName().length()>0))
        )
      {
        //utf8 = response->GetJsonpCallbackName() + "( \"" + utf8 + "\" )"; ...
        std::string jsonp;
        CreateJsonpCallbackString(GetJsonpCallbackName(),stringval_utf8,jsonp);
        stringval_utf8 = jsonp;
      }

      sprintf(tempHeader, "%d", stringval_utf8.length());
      m_HttpData.AddHeader(MapAgentStrings::ContentLengthKey,tempHeader);
      

      m_HttpData.SetContent(stringval_utf8);
      
    }
    else if (outputReader != NULL)
    {
      if( response_contentType == RestMimeType::Json || response_contentType == RestMimeType::JsonP )
      {
        MgXmlJsonConvert convert;
        convert.ToJson(outputReader);

        if( response_contentType == RestMimeType::JsonP )
        {
          string json;
          // set read back to text from Jason so it can be converted to string with function 'ToStringUtf8'
          outputReader->GetByteSource()->SetMimeType(RestMimeType::Text);
          outputReader->ToStringUtf8(json);

          string jsonp;
          CreateJsonpCallbackString(GetJsonpCallbackName(),json,jsonp);

          Ptr<MgByteSource> byteSource = new MgByteSource(
            (unsigned char*)jsonp.c_str(), (INT32)jsonp.length());
          byteSource->SetMimeType(RestMimeType::JsonP);
          outputReader.Attach(byteSource->GetReader());   
        }
      }

      INT64 outLen = outputReader->GetLength();
      sprintf(tempHeader, "%d", (INT32)outLen);
      
      m_HttpData.AddHeader(MapAgentStrings::ContentLengthKey,tempHeader);
      
     
      m_HttpData.SetContent(outputReader);

      // Tell IIS to keep the connection open 
      //DWORD dwState = HSE_STATUS_SUCCESS_AND_KEEP_CONN;
      //m_pECB->ServerSupportFunction(m_pECB->ConnID, HSE_REQ_DONE_WITH_SESSION, &dwState, NULL, 0);
    }
    else
    {
      
      sprintf(tempHeader, "%d", 0);

      m_HttpData.AddHeader(MapAgentStrings::ContentLengthKey,tempHeader);
      

      // Tell IIS to keep the connection open 
      //DWORD dwState = HSE_STATUS_SUCCESS_AND_KEEP_CONN;
      //m_pECB->ServerSupportFunction(m_pECB->ConnID, HSE_REQ_DONE_WITH_SESSION, &dwState, NULL, 0);
    }
  }
  
  return &m_HttpData;
}                                                                         
catch (MgException* exc)                                                    
{       
  SendError(exc);
  exc->Release();                                                                
}                                                                        
catch (exception& e)                                                     
{                                                                         
  Ptr<MgException> mgException = MgSystemException::Create(e, L"c_RestResponse::PrepareHttpData", __LINE__, __WFILE__); 
  SendError(mgException);
}                                                                         
catch (...)                                                               
{                                                                         
  Ptr<MgException> mgException = new MgUnclassifiedException(L"c_RestResponse::PrepareHttpData", __LINE__, __WFILE__, NULL, L"", NULL); 
  SendError(mgException);
}  

}//end of c_RestResponse::PrepareHttpData