virtual void httpFinished(HttpConnection*, int result) { if(result == 304) { //Not Modified printf("Program unchanged.\n"); #ifdef MA_PROF_SUPPORT_STYLUS printf("Tap screen or press Fire to run.\n"); #else // MA_PROF_SUPPORT_STYLUS printf("Press Fire to run.\n"); #endif // MA_PROF_SUPPORT_STYLUS mState = eReady; return; } if(result >= 200 && result <= 299) { //OK printf("HTTP %i\n", result); String cls; int res = mHttp.getResponseHeader("content-length", &cls); if(res < 0) { printf("CL error: %i\n", res); return; } int contentLength = stringToInteger(cls); printf("Content-Length: %i\n", contentLength); maDestroyObject(mProgram); if(maCreateData(mProgram, contentLength) == RES_OUT_OF_MEMORY) { printf("Out of memory!\n"); return; } mHttp.readToData(mProgram, 0, contentLength); return; } else { //error printf("HTTP %s %i\n", (result < 0) ? "error" : "response", result); } }
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; }
// 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); } }
virtual void connReadFinished(Connection*, int result) { if(result < 0) { printf("Read error %i\n", result); return; } //save Last-Modified header to cache mHttp.getResponseHeader("last-modified", &mLastModified); MAHandle data = maCreatePlaceholder(); maCreateData(data, mLastModified.length()); maWriteData(data, mLastModified.c_str(), 0, mLastModified.length()); MAHandle store = maOpenStore(MODSAV, MAS_CREATE_IF_NECESSARY); if(store < 0) { printf("Meta-cache open error: %i\n", store); } else { int res = maWriteStore(store, data); if(res < 0) { printf("Meta-cache write error: %i\n", res); } maCloseStore(store, 0); } maDestroyPlaceholder(data); //save program to cache store = maOpenStore(SAV, MAS_CREATE_IF_NECESSARY); if(store < 0) { printf("Cache open error: %i\n", store); } else { int res = maWriteStore(store, mProgram); if(res < 0) { printf("Cache write error: %i\n", res); } maCloseStore(store, 0); } printf("Program downloaded.\n"); #ifdef MA_PROF_SUPPORT_STYLUS printf("Tap screen or press Fire to run.\n"); #else // MA_PROF_SUPPORT_STYLUS printf("Press Fire to run.\n"); #endif // MA_PROF_SUPPORT_STYLUS mState = eReady; }