/* Delete a Listen Tool Object ** --------------------------- */ PRIVATE BOOL ListenTool_delete (ListenTool * me) { if (me) { HTRequest_delete(me->request); HT_FREE(me); return YES; } return NO; }
BinURLInputStream::BinURLInputStream(const XMLURL& urlSource) : fBuffer(0) , fBufferSize(0) , fBufferIndex(0) , fRemoteFileSize(0) , fAnchor(0) , fBytesProcessed(0) , fMemoryManager(urlSource.getMemoryManager()) { fBuffer = (XMLByte*) fMemoryManager->allocate ( URLISBUFMAXSIZE * sizeof(XMLByte) );//new XMLByte[URLISBUFMAXSIZE]; const XMLCh* uri = urlSource.getURLText(); char* uriAsCharStar = localTranscode(uri, fMemoryManager); // // First find the size of the remote resource being asked for. // We use the ContentCounter stream provided by libWWW. // fAnchor = HTAnchor_findAddress(uriAsCharStar); HTRequest* request = HTRequest_new(); HTRequest_setOutputFormat(request, WWW_SOURCE); HTStream* counterStrm = HTContentCounter(HTBlackHole(), request, 0xFFFF); BOOL status = HTLoadToStream(uriAsCharStar, counterStrm, request); if (status == YES) { HTParentAnchor * anchor = HTRequest_anchor(request); fRemoteFileSize=HTAnchor_length(anchor); if(fRemoteFileSize < 0) { // Patch by Artur Klauser // When a redirection is processed in libWWW, it seems that // HTAnchor_length(anchor) == -1 on the original anchor, whereas // HTResponse_length(response) gives the correct content length of // the redirection target. This has confused fRemoteFileSize and it was // not checked for a -1 response at all. HTResponse * response = HTRequest_response (request); fRemoteFileSize = HTResponse_length(response); if (fRemoteFileSize < 0) { ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_LengthError, fMemoryManager); } } } // Cleanup, before you throw any errors. fMemoryManager->deallocate(uriAsCharStar); HTRequest_delete(request); // Don't know whether I am supposed to delete counterStrm. if (status == NO) { ThrowXMLwithMemMgr(NetAccessorException, XMLExcepts::NetAcc_LengthError, fMemoryManager); } }
/* ** We get called here from the event loop when we are done ** loading. Here we terminate the program as we have nothing ** better to do. */ int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) { /* Delete our request again */ HTRequest_delete(request); /* Delete our profile */ HTProfile_delete(); exit(status ? status : 0); }
PRIVATE int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) { /* We are done with this request */ HTRequest_delete(request); /* Terminate libwww */ HTProfile_delete(); exit(0); }
/* Delete a Command Line Object ** ---------------------------- */ PRIVATE BOOL ComLine_delete (ComLine * me) { if (me) { HTRequest_delete(me->request); if (me->log) HTLog_close(me->log); if (me->output && me->output != STDOUT) fclose(me->output); HT_FREE(me->cwd); HT_FREE(me); return YES; } return NO; }
PRIVATE int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) { /* We are done with this request */ HTRequest_delete(request); HTPrint("If you didn't see any tags then it is likely that your document wasn't xml\n"); /* Terminate libwww */ HTProfile_delete(); exit(0); }
PRIVATE int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) { /* We are done with this request */ HTRequest_delete(request); if (status == HT_ERROR) HTPrint("Can't listen on this port\n"); /* Terminate libwww */ HTProfile_delete(); exit(0); }
int main (int argc, char ** argv) { HTRequest * request; HTProfile_newPreemptiveClient("HTTPHeadApplication", "1.0"); request = HTRequest_new(); #if 0 /* Turn on TRACE so we can see what is going on */ HTSetTraceMessageMask("sop"); #endif /* We don't want any progress notifications */ HTAlert_setInteractive(NO); /* Parse the command line options */ if (argc >= 2) { char * url = argv[1]; /* Set the header field match (if any) */ if (argc >= 3) match = argv[2]; /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Add our own filter to handle termination */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Set the output format to source and put output to stdout */ HTRequest_setOutputStream(request, HTFWriter_new(request, stdout, YES)); /* Start the load */ if (url && *url) { HTHeadAbsolute(url, request); /* wait until the request has finished */ HTEventList_loop (request); } else HTPrint("Bad parameters - please try again\n"); } else { HTPrint("Type the URL to perform a HEAD request on.\n"); HTPrint("\t%s <url> <header-prefix>\n", argv[0]); HTPrint("where <header-prefix> is the prefix of the header fields\n"); HTPrint("you want to see, for example 'content*. If no \'*\' is used'\n"); HTPrint("then an exact match is needed. Default value is all header fields'\n"); } HTRequest_delete(request); /* Delete the request object */ HTProfile_delete(); return 0; }
PRIVATE int terminate_handler (HTRequest * request, HTResponse * response, void * param, int status) { if (status == HT_LOADED && result && HTChunk_data(result)) { HTPrint("%s", HTChunk_data(result)); HTChunk_delete(result); } /* We are done with this request */ HTRequest_delete(request); /* Terminate libwww */ HTProfile_delete(); exit(0); }
static char* url_to_local_file(const char* url) { HTRequest* request = NULL; #ifdef __CYGWIN__ char* tmpfile = sidl_String_concat3("cyg", tmpnam(NULL), ".dll"); #else char* tmpfile = sidl_String_concat3("lib", tmpnam(NULL), ".so"); #endif HTProfile_newPreemptiveClient("sidl_DLL", "1.0"); HTAlert_setInteractive(FALSE); request = HTRequest_new(); HTLoadToFile(url, request, tmpfile); HTRequest_delete(request); HTProfile_delete(); return tmpfile; }
/* ** Loads a URL to a local file */ int main (int argc, char ** argv) { HTRequest * request; HTProfile_newPreemptiveClient("TestApp", "1.0"); request = HTRequest_new(); if (argc == 3) { char * url = argv[1]; char * filename = argv[2]; if (url && *url && filename && *filename) HTLoadToFile(url, request, filename); else printf("Bad parameters - please try again\n"); } else { printf("Type the URL to fetch and the name of the local file to put it in\n"); printf("\t%s <url> <filename>\n", argv[0]); } HTRequest_delete(request); /* Delete the request object */ HTProfile_delete(); return 0; }
/* ** A small AFTER filter that flushes the PIPE buffer so that we can ** get the rest of the data */ PRIVATE int HTCacheFlushFilter (HTRequest * request, HTResponse * response, void * param, int mode) { HTStream * pipe = (HTStream *) param; if (pipe) { HTTRACE(STREAM_TRACE, "Cache Flush. Flushing and freeing PIPE buffer\n"); (*pipe->isa->flush)(pipe); #if 0 /* @@ JK: flush converts the pipe to an open one, we shouldn't free it as we'll loose our references */ (*pipe->isa->_free)(pipe); #endif } /* ** We also delete the request obejct and stop more filters from being called. ** As this is our own request, it's OK to do that */ HTRequest_delete(request); return HT_ERROR; }
// (maybe this should be CL_DL_DownloadLoop) dlStatus_t DL_DownloadLoop() { if ( !dl_running ) { Com_DPrintf( "DL_DownloadLoop: unexpected call with dl_running == qfalse\n" ); return DL_DONE; } if ( HTEventList_pump() ) { return DL_CONTINUE; } /* reset the timeout so it doesn't trigger when no-one wants it un-register the current timeout first so it doesn't explode in our hands */ HTEventList_unregisterAll(); HTHost_setEventTimeout( -1 ); /* NOTE: in some samples I've seen, this is in the terminate_handler */ HTRequest_delete( dl_request ); dl_request = NULL; dl_running = 0; Cvar_Set( "ui_dl_running", "0" ); /* NOTE: there is HTEventList_status, but it says != HT_OK as soon as HTEventList_pump returns NO */ if ( terminate_status < 0 ) { Com_DPrintf( "DL_DownloadLoop: request terminated with failure status %d\n", terminate_status ); return DL_FAILED; } return DL_DONE; }
unsigned int BinURLInputStream::readBytes(XMLByte* const toFill , const unsigned int maxToRead) { unsigned int retval = 0; unsigned int bytesAsked = maxToRead; unsigned int bytesForCopy = 0; // Wipe out the old stuff from the destination buffer to fill. memset((void*)toFill, 0x00, sizeof(XMLByte) * maxToRead); // You can only read till the end of the remote resource file. // So, adjust the count of bytes you want to read now. if (fBytesProcessed + bytesAsked >= fRemoteFileSize) { bytesAsked = fRemoteFileSize - fBytesProcessed; } if (fBufferSize > 0) bytesForCopy = fBufferSize - fBufferIndex; if (bytesAsked <= bytesForCopy) { // ...then you can satisfy this request completely from fBuffer. // Simply copy over the bytes to the destination array. memcpy((void*) toFill, (void*) (fBuffer + fBufferIndex), bytesAsked); fBufferIndex += bytesAsked; if (fBufferIndex >= fBufferSize) { fBufferSize = 0; fBufferIndex = 0; } fBytesProcessed += bytesAsked; retval = bytesAsked; } else { // ...will need to read some more bytes out of the stream. unsigned int bufToFillIndex = 0; HTRequest* request = HTRequest_new(); HTChunk* result = NULL; char ranges[64]; // First copy over what is left in fBuffer, before reading another // chunk out of the stream. if (bytesForCopy != 0) { memcpy((void*) toFill, (void*) (fBuffer + fBufferSize), bytesForCopy); fBufferSize = 0; fBufferIndex = 0; fBytesProcessed += bytesForCopy; bufToFillIndex = bytesForCopy; retval = bytesForCopy; } unsigned int bytesRemainingForCopy = bytesAsked - bytesForCopy; // Now read a new chunk from the stream. HTTP lets you specify the // range of bytes that you would like. sprintf(ranges, "%ld-%ld", fBytesProcessed, fRemoteFileSize<(fBytesProcessed + URLISBUFMAXSIZE)? fRemoteFileSize - 1: fBytesProcessed + URLISBUFMAXSIZE - 1); HTRequest_addRange(request, "bytes", ranges); HTRequest_setOutputFormat(request, WWW_SOURCE); result = HTLoadAnchorToChunk(fAnchor, request); fBufferSize = HTChunk_size(result); if (fBufferSize > 0) { // Store the read chunk in fBuffer. memset((void*) fBuffer, 0x00, URLISBUFMAXSIZE); memcpy((void*) fBuffer, (void*) HTChunk_data(result), fBufferSize); fBufferIndex = 0; } HTRequest_delete(request); HTChunk_delete(result); // Now fill the destination buffer with the new data just read. bytesForCopy = fBufferSize; if (bytesRemainingForCopy > fBufferSize) { bytesRemainingForCopy = fBufferSize; } memcpy((void*) (toFill + bufToFillIndex), (void*) fBuffer, bytesRemainingForCopy); // Update counters. retval += bytesRemainingForCopy; fBufferIndex += bytesRemainingForCopy; fBytesProcessed += bytesRemainingForCopy; } return retval; }