ULXR_API_IMPL(Cpp8BitString) HttpClient::msgPOST( const Cpp8BitString &msg, const CppString &type, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("msgPOST")); Cpp8BitString ret; if (!protocol->isOpen() ) protocol->open(); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("POST"), resource, type, msg.length()); protocol->writeBody(msg.data(), msg.length()); StringProcessor sp (ret); receiveResponse(sp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); return ret; }
ULXR_API_IMPL(void) HttpClient::filePUT(const CppString &filename, const CppString &type, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("filePUT")); if (!protocol->isOpen() ) protocol->open(); FILE *ifs = fopen (getLatin1(filename).c_str(), "rb"); if (ifs == 0) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Cannot open file: "))+filename); struct stat statbuf; if (0 != stat (getLatin1(filename).c_str(), &statbuf) ) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Could not get information about file: "))+filename); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("PUT"), resource, type, statbuf.st_size); char buffer [ULXR_SEND_BUFFER_SIZE]; long readed; try { while (!feof(ifs)) { readed = fread(buffer, 1, sizeof(buffer), ifs); if (readed < 0) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Could not read from file: "))+filename); protocol->writeBody(buffer, readed); } } catch (...) { fclose(ifs); throw; } // bool eof_reached = feof(ifs); fclose(ifs); BodyProcessor bp; receiveResponse(bp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
// 404 void respErrorPage( headerOut *header_out , int status_code ) { if (status_code >= 400 && status_code <= 507) { int ret = respDefinedErrorPage( header_out , status_code ); if (ret == AE_TRUE) { return; } } // get header info headerStatus error_page = getHttpStatus( status_code ); int datalen = strlen( error_page.data ); // header append createCommonHeader( header_out , status_code ); headerAppendLength( header_out , datalen ); appendRespHeader( header_out , HEADER_END_LINE ); // send httpResponseWrite( header_out->req->connfd , header_out->data , header_out->length ); if (header_out->req->nobody != AE_TRUE) { httpResponseWrite( header_out->req->connfd , error_page.data , datalen ); } // nobody强制关闭 httpClose( header_out->req , header_out->req->nobody ); }
// this function only process status=200 page, not include resp_error_page void createCommonHeader( headerOut *header_out , int status_code ) { headerStatus error_page = getHttpStatus( status_code ); // header append appendRespHeader( header_out , HEADER_STATUS , error_page.status ); appendRespHeader( header_out , HEADER_SERVER ); appendRespHeader( header_out , HEADER_CONTENT_TYPE ); appendRespHeader( header_out , HEADER_KEEP_ALIVE ); }
ULXR_API_IMPL(void) HttpClient::doDELETE(const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("doDELETE")); if (!protocol->isOpen() ) protocol->open(); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("DELETE"), resource, ULXR_PCHAR(""), 0); BodyProcessor bp; receiveResponse(bp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
ULXR_API_IMPL(void) HttpClient::fileGET(const CppString &filename, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("fileGET")); if (!protocol->isOpen() ) protocol->open(); std::ofstream ofs (getLatin1(filename).c_str(), std::ios::out | std::ios::binary); if (!ofs.good() ) throw Exception(SystemError, ulxr_i18n(ULXR_PCHAR("Cannot create file: "))+filename); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("GET"), resource, ULXR_PCHAR(""), 0); FileProcessor fp(ofs, filename); receiveResponse(fp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
ULXR_API_IMPL(void) HttpClient::msgPUT(const Cpp8BitString &msg, const CppString &type, const CppString &resource) { ULXR_TRACE(ULXR_PCHAR("msgPUT")); if (!protocol->isOpen() ) protocol->open(); sendAuthentication(); protocol->sendRequestHeader(ULXR_PCHAR("PUT"), resource, type, msg.length()); #ifdef ULXR_USE_WXSTRING protocol->writeBody(msg.data(), msg.length()); #else protocol->writeBody(msg.data(), msg.length()); #endif BodyProcessor bp; receiveResponse(bp); if (getHttpStatus() != 200) throw ConnectionException(TransportError, getHttpPhrase(), getHttpStatus()); if (!protocol->isPersistent() ) protocol->close(); }
void httpRedirect( httpHeader *req_header , char *uri ) { headerOut header_out; memset( &header_out , 0 , sizeof( header_out ) ); header_out.req = req_header; headerStatus error_page = getHttpStatus( 301 ); // header append createCommonHeader( &header_out , 301 ); appendRespHeader( &header_out , HEADER_LOCATION , uri ); appendRespHeader( &header_out , HEADER_CONTENT_LENGTH , 0 ); appendRespHeader( &header_out , HEADER_END_LINE ); httpResponseWrite( req_header->connfd , header_out.data , header_out.length ); httpClose( req_header , 0 ); }
void checkAndGzip(buffer *tmpFilePath, buffer *targetFilePath, buffer *expectFilePath, buffer *responsePath){ char *responseCnt = readResponse(responsePath->ptr); int httpStatus = getHttpStatus(responseCnt); if(200 == httpStatus) { if(-1 != rename(tmpFilePath->ptr, targetFilePath->ptr)) { //system invoke unzip buffer *unzipCmd = buffer_init_size(20 + targetFilePath->used + expectFilePath->used); stringAppend(unzipCmd, GZIP_CMD, GZIP_CMD_LEN); stringAppend(unzipCmd, targetFilePath->ptr, targetFilePath->used); stringAppend(unzipCmd, " > ", 3); stringAppend(unzipCmd, expectFilePath->ptr, expectFilePath->used); debug_buffer(unzipCmd, "unzipCmd"); if(-1 == system(unzipCmd->ptr)) { fprintf(stderr, "system(gzip....) error:%s\n", unzipCmd->ptr); exit(5); } buffer_free(unzipCmd); } } free(responseCnt); }
void intervalWork(VersionUpdateConfig *config) { if(debug) { fprintf(stdout, "start intervalWork\n"); } //clean old responsefile remove(config->reponseFilePath->ptr); buffer *lastModifiedTime = NULL; int paramLen = config->URLDomain->used + config->tmpVersionFilePath->used + config->reponseFilePath->used; buffer *param = buffer_init_size(20 + paramLen); stringAppend(param, " ", 1); stringAppend(param, config->URLDomain->ptr, config->URLDomain->used); stringAppend(param, " -O ", 4); stringAppend(param, config->tmpVersionFilePath->ptr, config->tmpVersionFilePath->used); stringAppend(param, " > ", 3); stringAppend(param, config->reponseFilePath->ptr, config->reponseFilePath->used); stringAppend(param, " 2>&1 ", 6); while(1) { char *responseCnt = readResponse(config->reponseFilePath->ptr); int httpStatus = getHttpStatus(responseCnt); if(debug) { fprintf(stdout, "httpStatus:%d\n", httpStatus); } // ignore the lock checke for the first version file request if(0 != httpStatus && 404 != httpStatus) { int bLocked = checkLockStatus(config->lockRequestURL->ptr); if(bLocked){ sleep(config->intervalSecond); continue; } } if(304 != httpStatus) { getLastModified(responseCnt, &lastModifiedTime); } debug_buffer(lastModifiedTime, "lastModifiedTime"); //thread interval exec buffer *cmdBuf = buffer_init_size(LAST_MODIFIED_LEN + WGET_CMD_LEN + HEADER_LEN + param->used); stringAppend(cmdBuf, WGET_CMD, WGET_CMD_LEN); if(NULL != lastModifiedTime) { stringAppend(cmdBuf, HEADER, HEADER_LEN); stringAppend(cmdBuf, lastModifiedTime->ptr, lastModifiedTime->used); stringAppend(cmdBuf, "\"", 1); } stringAppend(cmdBuf, param->ptr, param->used); debug_buffer(cmdBuf, "cmdBuf"); if(-1 == system(cmdBuf->ptr)) { fprintf(stderr, "system (wget...) error:%s\n", cmdBuf->ptr); exit(4); } free(responseCnt); buffer_free(cmdBuf); checkAndGzip(config->tmpVersionFilePath, config->versionFilePath, config->expectVersionFilePath, config->reponseFilePath); sleep(config->intervalSecond); } }