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.

134 lines
3.3 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
7 years ago
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
  1. #include "protocolHTTP.h"
  2. #define LOG(X) m_pLog->Log("HTTP", this, X)
  3. #define LOG_ERROR(X) m_pLog->Log("HTTP", this, _LOG_ERROR, X)
  4. // Constructor
  5. protocolHTTP::protocolHTTP()
  6. {
  7. m_pLog = basicLogCollection::getInstance();
  8. m_pSettings = NULL; // Settings;
  9. m_pCurlPostType = (CSTR) malloc(128);
  10. if (m_pCurlPostType == NULL)
  11. {
  12. return;
  13. }
  14. sprintf(m_pCurlPostType, DEFAULT_CONTENT_TYPE_JSON);
  15. LOG(m_pCurlPostType);
  16. m_pHttpAccessManager = curlRequestsManager::getInstance();
  17. }
  18. protocolHTTP::protocolHTTP(config_setting_t * Settings)
  19. {
  20. m_pLog = basicLogCollection::getInstance();
  21. m_pSettings = Settings;
  22. m_pCurlPostType = (CSTR) malloc(128);
  23. if (m_pCurlPostType == NULL)
  24. {
  25. return;
  26. }
  27. if (m_pSettings != NULL)
  28. {
  29. config_setting_t * pSetting;
  30. if (! config_setting_lookup_string(m_pSettings, HTTP_PROTOCOL_CONTENT_SETTINGS, (const char **) &m_pCurlPostType))
  31. {
  32. sprintf(m_pCurlPostType, DEFAULT_CONTENT_TYPE);
  33. pSetting = config_setting_add(m_pSettings, HTTP_PROTOCOL_CONTENT_SETTINGS, CONFIG_TYPE_STRING);
  34. config_setting_set_string(pSetting, m_pCurlPostType);
  35. basicSettingsProvider::getInstance()->compromise();
  36. };
  37. basicSettingsProvider::getInstance()->validate();
  38. }
  39. else
  40. {
  41. sprintf(m_pCurlPostType, DEFAULT_CONTENT_TYPE_JSON);
  42. };
  43. LOG(m_pCurlPostType);
  44. m_pHttpAccessManager = curlRequestsManager::getInstance();
  45. }
  46. protocolHTTP::~protocolHTTP()
  47. {
  48. // Its singletone!!
  49. // delete m_pHttpAccessManager;
  50. m_pHttpAccessManager = NULL;
  51. }
  52. bool protocolHTTP::issueGet(CSTR URL)
  53. {
  54. /*
  55. curlRequest *tRequest = new curlRequest(URL, m_pCurlPostType );
  56. if (tRequest != NULL){
  57. tRequest->setHandler(this, &protocolHTTP::requestFinished, tRequest);
  58. if (m_pHttpAccessManager->get(tRequest)){
  59. LOG("GET Req");
  60. } else {
  61. LOG_ERROR("Cannot issue request!");
  62. };
  63. }
  64. return true;
  65. ;*/
  66. return m_pHttpAccessManager->get(URL, m_pCurlPostType);
  67. }
  68. bool protocolHTTP::issuePost(CSTR URL, char * Buffer, int & BufferSize)
  69. {
  70. /*
  71. curlRequest *tRequest = new curlRequest(URL, m_pCurlPostType);
  72. if (tRequest != NULL){
  73. tRequest->setPOST( Buffer, BufferSize );
  74. tRequest->setHandler(this, &protocolHTTP::requestFinished, tRequest );
  75. if (m_pHttpAccessManager->get(tRequest)){
  76. LOG("POST Req");
  77. } else {
  78. LOG_ERROR("Cannot issue request!");
  79. };
  80. }
  81. return true;
  82. ;*/
  83. BufferSize = strlen( Buffer );
  84. return m_pHttpAccessManager->post(URL, Buffer, m_pCurlPostType);
  85. }
  86. void * protocolHTTP::requestFinished(void * ptr1, void * ptr2)
  87. {
  88. if (! ptr1)
  89. {
  90. return NULL;
  91. }
  92. if (! ptr2)
  93. {
  94. return NULL;
  95. }
  96. protocolHTTP * me;
  97. curlRequest * rr;
  98. me = (protocolHTTP *) ptr1;
  99. rr = (curlRequest *) ptr2;
  100. // qDebug() << "MapNetwork::requestFinished" << rr->getPath();
  101. //(const char *)rr->getBuffer(), (int)rr->getSize()
  102. protocolPackage * Package;
  103. Package = NULL;
  104. Package = (protocolPackage *) malloc(sizeof(protocolPackage));
  105. if (Package != NULL)
  106. {
  107. Package->m_pData = NULL;
  108. Package->m_nCode = 0;
  109. Package->m_nSize = 0;
  110. Package->m_pData = (U8 *) malloc(rr->getSize());
  111. if (Package->m_pData != NULL)
  112. {
  113. memcpy(Package->m_pData, rr->getBuffer(), rr->getSize());
  114. Package->m_nCode = 200;
  115. Package->m_nSize = rr->getSize();
  116. me->packetHandler(Package);
  117. free(Package->m_pData);
  118. };
  119. Package->m_nSize = 0;
  120. // me->m_lRequestsList.removeAll(rr);
  121. free(Package);
  122. };
  123. return NULL;
  124. }
  125. #undef LOG
  126. #undef LOG_ERROR