// internal callback, calling user CB void MQTT::_onMqttDataCb(const char* topic, uint32_t topic_len, const char* buf, uint32_t buf_len) { if (onMqttDataRawCb) { onMqttDataRawCb(topic, topic_len, buf, buf_len); } if (onMqttDataCb) { char* topicCpy = (char*)malloc(topic_len+1); memcpy(topicCpy, topic, topic_len); topicCpy[topic_len] = 0; // string it String topicStr(topicCpy); char* bufCpy = (char*)malloc(buf_len+1); memcpy(bufCpy, buf, buf_len); bufCpy[buf_len] = 0; // string it String bufStr(bufCpy); onMqttDataCb(topicStr, bufStr); free(topicCpy); free(bufCpy); } }
bool CompareToString(midY *pBuffer, std::string str) { uint8_t len = str.length(); const char *tempPtr = (const char*)pBuffer; std::string bufStr (tempPtr, len); return str.compare(bufStr) == 0; }
Response ConnectionImpl::Request( const std::string & method, const std::string & url, const HeaderMap & header ) { HeaderMap hm; hm["Host"] = this->HostName; hm["Connection"] = "close"; hm.Update( header ); std::ostringstream ossReq( std::ios::binary ); // \r\n を正しく処理するためバイナリとする。 ossReq << method << " " << url << " HTTP/1.1\r\n" << hm.ToString() << "\r\n" << '\0'; this->makeConnection(); this->sendRequest( ossReq.str() ); std::ostringstream ossResult( std::ios::binary ); const int BufferSize = 256; char buf[BufferSize+1]; // サーバからヘッダ部を受信 while( 1 ) { int readSize = this->receive( buf, BufferSize ); if( readSize > 0 ) { buf[readSize] = '\0'; std::string bufStr( buf ); std::string::size_type endOfHeader = bufStr.find( "\r\n\r\n" ); if( endOfHeader == std::string::npos ) { ossResult.write( buf, readSize ); } else { ossResult.write( buf, readSize ); break; } } else if( readSize == 0 ) { break; } else { throw CACOON_EXCEPTION( "recv エラー" ); } } std::string respHeaderStr = ossResult.str(); std::string::size_type firstLine = respHeaderStr.find( "\r\n" ); std::string::size_type lastLine = respHeaderStr.find( "\r\n\r\n" ); HeaderMap respHeader( respHeaderStr.substr( firstLine + 2, lastLine - firstLine - 2 ) ); if( respHeader["Transfer-Encoding"] == "chunked" ) { // サーバから続きを受信 (chunked バージョン) int chunkSize = 0; while( 1 ) { int readSize = this->receive( buf, BufferSize ); if( chunkSize == 0 ) { if( readSize == 0 ) { break; } buf[readSize-2] = '\0'; chunkSize = strtol( buf, NULL, 16 ); } else { if( readSize > 0 ) { ossResult.write( buf, readSize ); chunkSize -= readSize; } else if( readSize == 0 ) { break; } else { throw CACOON_EXCEPTION( "recv エラー" ); } } } } else { // サーバから続きを受信 (普通) while( 1 ) { int readSize = this->receive( buf, BufferSize ); if( readSize > 0 ) { ossResult.write( buf, readSize ); break; } else if( readSize == 0 ) { break; } else { throw CACOON_EXCEPTION( "recv エラー" ); } } } return Response( ossResult.str() ); }