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.

90 lines
2.9 KiB

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