You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.7 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. #include "protocolHTTP.h"
  2. #define LOG( X ) basicLogCollection::getInstance()->Log( "protocolHTTP", this, X )
  3. #define LOG_ERROR( X ) basicLogCollection::getInstance()->Log( "protocolHTTP", this, _LOG_ERROR, X )
  4. // Constructor
  5. protocolHTTP::protocolHTTP(config_setting_t *Settings){
  6. m_pSettings = Settings;
  7. config_setting_t * pSetting;
  8. m_pCurlPostType = (CSTR)malloc(128);
  9. if( m_pCurlPostType == NULL ) return;
  10. if (!config_setting_lookup_string(m_pSettings, HTTP_PROTOCOL_CONTENT_SETTINGS, (const char**)&m_pCurlPostType)){
  11. sprintf( m_pCurlPostType, DEFAULT_CONTENT_TYPE );
  12. pSetting = config_setting_add(m_pSettings, HTTP_PROTOCOL_CONTENT_SETTINGS, CONFIG_TYPE_STRING);
  13. config_setting_set_string( pSetting, m_pCurlPostType );
  14. basicSettingsProvider::getInstance()->compromise();
  15. };
  16. LOG( m_pCurlPostType );
  17. basicSettingsProvider::getInstance()->validate();
  18. m_pHttpAccessManager = curlRequestsManager::getInstance();
  19. };
  20. protocolHTTP::~protocolHTTP(){
  21. delete m_pHttpAccessManager;
  22. m_pHttpAccessManager = NULL;
  23. };
  24. bool protocolHTTP::issueGet(CSTR URL){
  25. curlRequest *tRequest = new curlRequest(URL, m_pCurlPostType );
  26. if (tRequest != NULL){
  27. tRequest->setHandler(this, &protocolHTTP::requestFinished, tRequest);
  28. if (m_pHttpAccessManager->get(tRequest)){
  29. //LOG("GET Req");
  30. } else {
  31. LOG_ERROR("Cannot issue request!");
  32. };
  33. };
  34. return true;
  35. };
  36. bool protocolHTTP::issuePost(CSTR URL, char *Buffer, int &BufferSize ){
  37. curlRequest *tRequest = new curlRequest(URL, m_pCurlPostType);
  38. if (tRequest != NULL){
  39. tRequest->setPOST( Buffer, BufferSize );
  40. tRequest->setHandler(this, &protocolHTTP::requestFinished, tRequest );
  41. if (m_pHttpAccessManager->get(tRequest)){
  42. //LOG("POST Req");
  43. } else {
  44. LOG_ERROR("Cannot issue request!");
  45. };
  46. };
  47. return true;
  48. };
  49. void *protocolHTTP::requestFinished(void *ptr1, void *ptr2){
  50. if (!ptr1) return NULL;
  51. if (!ptr2) return NULL;
  52. protocolHTTP *me;
  53. curlRequest *rr;
  54. me = (protocolHTTP*)ptr1;
  55. rr = (curlRequest *)ptr2;
  56. //qDebug() << "MapNetwork::requestFinished" << rr->getPath();
  57. //(const char *)rr->getBuffer(), (int)rr->getSize()
  58. protocolPackage *Package;
  59. Package = NULL;
  60. Package = (protocolPackage*) malloc( sizeof( protocolPackage ));
  61. if( Package != NULL ){
  62. Package->m_pData = NULL;
  63. Package->m_nCode = 0;
  64. Package->m_nSize = 0;
  65. Package->m_pData = (U8 *) malloc( rr->getSize() );
  66. if( Package->m_pData != NULL ){
  67. memcpy( Package->m_pData, rr->getBuffer(), rr->getSize() );
  68. Package->m_nCode = 200;
  69. Package->m_nSize = rr->getSize();
  70. me->packetHandler( Package );
  71. free( Package->m_pData );
  72. };
  73. Package->m_nSize = 0;
  74. //me->m_lRequestsList.removeAll(rr);
  75. free( Package );
  76. };
  77. return NULL;
  78. };
  79. #undef LOG
  80. #undef LOG_ERROR