Beispiel #1
0
void MusicModel :: parseReply(QNetworkReply *reply)
{
    if(!reply -> error())
    {
        files.clear();
        QByteArray responce(reply -> readAll());
        QJsonDocument document(QJsonDocument::fromJson(responce, 0));
        QJsonArray items(document.object().take(QString("response")).toObject().take(QString("items")).toArray());
        QJsonArray::const_iterator first(items.begin());
        QJsonArray::const_iterator last(items.end());
        for (QJsonArray::const_iterator it = first; it != last; ++it)
            files.push_back(parseAudioFile((*it).toObject()));
        insertRows(0, files.size());
    }

}
int
ParserCWHandler::getURL( const MC2String& urlStr, 
                         const MC2String& postData,
                         uint32 peerIP, uint32 fromByte, uint32 toByte,
                         LangTypes::language_t clientLang,
                         const HttpHeader* inHeaders,
                         HttpHeader& outHeaders, MC2String& reply,
                         uint32& startByte, uint32& endByte )
{
   uint32 startTime = TimeUtility::getCurrentTime();

   // Work with url
   MC2String serverURLStr( urlStr );
   mc2dbg2 << "serverURLStr " << serverURLStr << endl;
   updateRequest( serverURLStr, clientLang );

   bool fetchedThruProxy = false;
   if ( serverURLStr.find( "://sendThruProxyServer" ) != MC2String::npos ) {
      // This url should be fetched thru the proxy server
      
      // Remove 'sendThruProxyServer' from URL
      STLStringUtility::replaceString( serverURLStr, 
                                       "://sendThruProxyServer/",
                                       "://" );

      URL url(serverURLStr);
      HttpBody outBody;
      HttpVariableContainer myVar;
      myVar.https = ( serverURLStr.find( "https://" ) != MC2String::npos );
      
      // Fetch the url thru proxy server. If proxy fails it will be fetched without
      // proxy below.
      fetchedThruProxy =
         HttpProxyFunctions::fetchThruProxy( url, &outHeaders, &outBody, m_thread, &myVar );
      if ( fetchedThruProxy ) {
         // Successfully fetched thru proxy
         reply.append( outBody.getBody(), outBody.getBodyLength() );      
      } 
   }
   
   // Work with url
   mc2dbg2 << "serverURLStr " << serverURLStr << endl;
   updateRequest( serverURLStr, clientLang );
   URL url2( serverURLStr );
   int ures = 0;
   uint32 timeout = Properties::getUint32Property( "CW_TIMEOUT", 
                                                   5000 );
   
   if ( ! fetchedThruProxy ) {
      if ( serverURLStr.find( "internalInThisProcess" ) != MC2String::npos ) {
         // Handle internally
         reply.clear();
         if ( serverURLStr.find("/TMap/") != MC2String::npos ) {
            MC2String urlParam = STLStringUtility::basename( url2.getFile() );
            if ( !urlParam.empty() ) {
               DataBuffer* d = m_thread->getTileMap( urlParam.c_str() );
               if ( d != NULL ) {
                  ures = 200;
                  reply.append( reinterpret_cast<char*>( d->getBufferAddress() ),
                                d->getCurrentOffset() );
                  outHeaders.setStartLine( 200 );
               } else {
                  outHeaders.setStartLine( 503 );
               }
               delete d;
            }
         }
      }  else {
         // Send request using the parserthreads urlfetcher
         URLFetcher* f = m_thread->getURLFetcher();
         HttpHeader sendHeaders; // Extra headers to send
         // TODO: Add byterange using fromByte and toByte if not 0,MAX_UINT32
         //       so we don't download whole file all the time.
         MC2String peerIPstr = NetUtility::ip2str( peerIP );
         sendHeaders.addHeaderLine( "X-Forwarded-For", peerIPstr );
         
         static const MC2String notForwardHeadersStr[] = {
            "X-WAYF-CT",
            HttpHeaderLines::CONTENT_TYPE,
            HttpHeaderLines::CONTENT_LENGTH,
            HttpHeaderLines::CONNECTION,
            HttpHeaderLines::TRANSFER_ENCODING,
            HttpHeaderLines::X_FORWARDED_FOR,
            HttpHeaderLines::PROXY_CONNECTION,
            HttpHeaderLines::HOST,
            HttpHeaderLines::TE,
            HttpHeaderLines::TRAILER,
            HttpHeaderLines::KEEP_ALIVE,
            HttpHeaderLines::PROXY_AUTHENTICATE,
            HttpHeaderLines::PROXY_AUTHORIZATION,
            HttpHeaderLines::UPGRADE,
         };
         // TODO: Also remove all headers in Connection: header. Like
         //       "Connection: Keep-Alive, Trailer" should delete those two.
         static const set< MC2String, strNoCaseCompareLess > notForwardHeaders( 
            BEGIN_ARRAY( notForwardHeadersStr ), 
            END_ARRAY( notForwardHeadersStr ) );
         
         if ( inHeaders != NULL ) {
            const HttpHeader::HeaderMap& headers = inHeaders->getHeaderMap();
            for ( HttpHeader::HeaderMap::const_iterator it = headers.begin() ;
                  it != headers.end() ; ++it ) {
               if ( notForwardHeaders.find( it->first ) == 
                    notForwardHeaders.end() ) {
                  sendHeaders.addHeaderLine( it->first, *it->second );
               }
            }
         }
         
         if ( postData.empty() ) {
            ures = f->get(  reply, outHeaders, url2, 
                            timeout, &sendHeaders );
         } else {
            if ( inHeaders->getHeaderValue( "X-WAYF-CT" ) != NULL ) {
               sendHeaders.addHeaderLine( 
                  HttpHeaderLines::CONTENT_TYPE,
                  *inHeaders->getHeaderValue( "X-WAYF-CT" ) );
            } else {
               sendHeaders.addHeaderLine( 
                  HttpHeaderLines::CONTENT_TYPE, 
                  "application/x-www-form-urlencoded" );
            }
            ures = f->post( reply, outHeaders, url2, postData, 
                            timeout, &sendHeaders );
         }
         // Reset user agent
         //f->setDefaultUserAgent();
      }
   } // if (! fetchedThruProxy )
   
   // Remove chunked-encoding from reply (if present)
   const MC2String teh( "Transfer-Encoding" );
   const MC2String* te = outHeaders.getHeaderValue( &teh );
   if ( te != NULL && ( te->find( "chunked") != MC2String::npos ) ) {
      outHeaders.deleteHeaderLine( &teh );
   }
      
   // Check if web updated user
   const MC2String wfidh( "X-WFID-UPDATE" );
   const MC2String* wfid = outHeaders.getHeaderValue( &wfidh );
   if ( wfid != NULL ) {
      // Remove the uin from user cache
      uint32 uin = STLStringUtility::strtoul( *wfid );
      m_group->removeUserFromCache( uin );

      // And remove the header
      outHeaders.deleteHeaderLine( &wfidh );
   }

   // Make reply
   const MC2String eol( "\r\n" );
   if ( fromByte > toByte ) {
      toByte = fromByte;
   }
   const uint32 maxBytes = toByte - fromByte;
   uint32 lastByte = uint32( MAX( int32(reply.size()) - 1, 0 ) );
   startByte = MIN( fromByte, lastByte );
   endByte = startByte + MIN( lastByte - startByte, maxBytes );

   if ( ures > 0 && reply.size() > 0 && 
        (endByte != lastByte || startByte != 0) ) 
   {
      // Set byte range in reply
      MC2String rangeStr( "bytes " );
      STLStringUtility::uint2str( startByte, rangeStr );
      rangeStr.append( "-" );
      STLStringUtility::uint2str( endByte, rangeStr );
      rangeStr.append( "/" );
      STLStringUtility::uint2str( reply.size(), rangeStr ); // Real size
      outHeaders.addHeaderLine( "Content-Range", rangeStr );
      rangeStr = "";
      STLStringUtility::uint2str( endByte - startByte + 1, rangeStr );
      // Set right content length
      outHeaders.addHeaderLine( "Content-Length", rangeStr );
      // Set startline with 206 Partial Content
      MC2String responce( outHeaders.getStartLine()->substr( 0, 9 ) );
      responce.append( "206 Partial Content" );
      responce.append( eol );
      outHeaders.setStartLine( new MC2String( responce ) );
   } else if ( ures < 0 || outHeaders.getStartLine() == NULL ) {
      if ( TimeUtility::getCurrentTime() - startTime >= timeout ) {
         outHeaders.setStartLine( 503 );
      } else {
         outHeaders.setStartLine( 500 );
      }
   } else {
      // Set startline with eol
      outHeaders.setStartLine( 
         new MC2String( StringUtility::trimStartEnd( 
                           *outHeaders.getStartLine() ) + eol ) );
   }

   return ures;
}