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.

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