Пример #1
0
    // Process Response
    static void processHttpResponse(HttpResponse* response, std::string& errorStr)
    {
        auto request = response->getHttpRequest();
        long responseCode = -1;
        int retValue = 0;
        HttpConnection xhr;
        bool ok = false;
        bool manualAuthReqd = false;

        // Process the request -> get response packet
        switch (request->getRequestType())
        {
        case HttpRequest::Type::GET: // HTTP GET
            ok = (xhr.init(request) && xhr.open("GET", manualAuthReqd, s_cookieFilename) && xhr.send());
            break;

        case HttpRequest::Type::POST: // HTTP POST
            ok = (xhr.init(request) && xhr.open("POST", manualAuthReqd, s_cookieFilename) && xhr.send());
            break;

        case HttpRequest::Type::PUT: // HTTP PUT
            ok = (xhr.init(request) && xhr.open("PUT", manualAuthReqd, s_cookieFilename) && xhr.send());
            break;

        case HttpRequest::Type::DELETE: // HTTP DELETE
            ok = (xhr.init(request) && xhr.open("DELETE", manualAuthReqd, s_cookieFilename) && xhr.send());
            break;

        default:
            CCASSERT(true, "CCHttpClient: unknown request type, only GET and POST are supported");
            break;
        }

        writeData(xhr.getResponseHeader(), response->getResponseHeader());
        writeData(xhr.getResponseData(), response->getResponseData());
        retValue = ok ? 0 : 1;
        errorStr = xhr.getErrorMessage();
        responseCode = xhr.getStatusCode();

        // write data to HttpResponse
        response->setResponseCode(responseCode);

        if (retValue != 0)
        {
            response->setSucceed(false);
            response->setErrorBuffer(errorStr.c_str());
        }
        else
        {
            response->setSucceed(true);
        }
    }
Пример #2
0
int HttpUploader::upload(const StringBuffer& luid, InputStream* inputStream) 
{
    int status = 0;

    // safe checks
    if (!inputStream || !inputStream->getTotalSize()) {
        LOG.error("upload error: no data to transfer");
        return 1;
    }
    if (luid.empty() || syncUrl.empty()  || sourceURI.empty()) {
        LOG.error("upload error: some params are not set");
        return 2;
    }
    
    StringBuffer fullUrl = composeURL();
    URL url(fullUrl.c_str());
    HttpConnection* httpConnection = getHttpConnection();
   
    httpConnection->setCompression(false);
    status = httpConnection->open(url, HttpConnection::MethodPost);
    
    if (status) { 
        delete httpConnection;

        return status;
    }

    httpConnection->setKeepAlive(keepalive);
    httpConnection->setRequestChunkSize(maxRequestChunkSize);
   
    // Set headers (use basic auth)
    HttpAuthentication* auth = new BasicAuthentication(username, password);
    httpConnection->setAuthentication(auth);
    setRequestHeaders(luid, *httpConnection, *inputStream);
    
    // Send the HTTP request
    StringOutputStream response;
    status = httpConnection->request(*inputStream, response);
    LOG.debug("response returned = %s", response.getString().c_str());
    
    // Manage response headers
    if (useSessionID) {
        // Server returns the jsessionId in the Set-Cookie header, can be used for 
        // the subsequent calls of upload().
        StringBuffer hdr = httpConnection->getResponseHeader(HTTP_HEADER_SET_COOKIE);
        sessionID = httpConnection->parseJSessionId(hdr);
    }
    
    httpConnection->close();
    
    delete auth;
    delete httpConnection;
    return status;
}
Пример #3
0
    /**
     * Tests a GET on a specific URL, prints the response.
     */
    void testGET(const URL& testURL) 
    {
        LOG.debug("test GET on %s", testURL.fullURL);
        int ret = httpConnection.open(testURL, HttpConnection::MethodGet);
        LOG.debug("open, ret = %d", ret);
        
        BufferInputStream inputStream("");
        StringOutputStream outputStream;
        
        httpConnection.setRequestHeader(HTTP_HEADER_ACCEPT,          "*/*");
        httpConnection.setRequestHeader(HTTP_HEADER_CONTENT_LENGTH,  0);

        ret = httpConnection.request(inputStream, outputStream);
        LOG.debug("request, ret = %d", ret);
        LOG.debug("response = \n%s", outputStream.getString().c_str());
        
        httpConnection.close();
    }
/*
    Returns the token for wassup based authentication or an empty string if errors occur
    Parameters:
                username: as inserted by the user
                password: as inserted by the user
                err: output flag to check for errors
 */
std::string WassupTokenRequestManager::getToken(std::string username, std::string password, bool *err, int* requestCode)
{
    std::string token = "";
    *err = false;
    *requestCode = HTTP_OK;
    
    //request token over http
    LOG.debug("Getting wassup token");
    
    StringOutputStream response;
    HttpConnection *httpConnection = NULL;
    URL requestUrl;

    std::string formattedURL(_wassupURI);
    std::stringstream ss;
    std::string xmlResponse;

    //URL encode usr and pwd
    const char * usernameEncoded = URL::urlEncode(username.c_str());
    const char * passwordEncoded = URL::urlEncode(password.c_str());
    
    ss << _wassupURI << "?" << _wassupUsrParam << usernameEncoded << "&" << _wassupPwdParam << passwordEncoded << "&" << _wassupAdditionalParam;
    
    formattedURL = ss.str();
    
    requestUrl.setURL(formattedURL.c_str());
    
    httpConnection = new HttpConnection(_userAgent);
    httpConnection->setSSLVerifyServer(verifyServerSSL);
    
    if (httpConnection->open(requestUrl, HttpConnection::MethodGet, false)!= 0) {
        LOG.error("%s: error opening connection", __FUNCTION__);
        *err = true;
        *requestCode = -1;
        delete httpConnection;
   
        return "";
    } else {
        int requestStatus = HTTP_OK;
        
        if ((requestStatus = httpConnection->request(NULL, response, false)) != HTTP_OK) {
            LOG.error("%s: error sending Wassup access token request", __FUNCTION__);
            *err = true;
            if ((requestStatus == HttpConnection::StatusNetworkError) || 
                (requestStatus == HttpConnection::StatusReadingError) ||
                (requestStatus == HttpConnection::StatusTimeoutError) ||
                (requestStatus == HttpConnection::StatusWritingError)) {
                *requestCode = -1;
            } else {
                *requestCode = requestStatus;
            }
            
            httpConnection->close();
            delete httpConnection;
   
            return "";
        } else {
            xmlResponse.assign(response.getString().c_str());
            //LOG.debug("Wassup access token request response received: %s", xmlResponse.c_str());
        }
    }
    
    httpConnection->close();
    delete httpConnection;
    
    //parse response to extract token
    unsigned int startPos = 0;
    unsigned int endPos   = 0;
    bool found = false;
    while(!found & !*err)
    {
        if(XMLProcessor::getElementAttributes(xmlResponse.c_str(), "ident", &startPos, &endPos)==NULL)
        {
            LOG.error("%s: error parsing XML response", __FUNCTION__);
            *err = true;
        }
        else
        {
            std::string attributeName = xmlResponse.substr(startPos+6, 6);
            int tokenStartIndex = startPos + 21;
            int tokenEndIndex   = endPos - 3;
            int tokenLenght     = tokenEndIndex - tokenStartIndex + 1;
            
            if (attributeName=="cooses") {
                token = xmlResponse.substr(tokenStartIndex, tokenLenght);
                found = true;
            }
            else//try next tag
            {
                xmlResponse = xmlResponse.substr(endPos+1);
            }
        }
    }
        
    return token;
}