PRIVATE Robot * Robot_new (void) { Robot * me; if ((me = (Robot *) HT_CALLOC(1, sizeof(Robot))) == NULL || (me->tv = (struct timeval*) HT_CALLOC(1, sizeof(struct timeval))) == NULL) HT_OUTOFMEM("Robot_new"); me->htext = 0; me->tv->tv_sec = DEFAULT_TIMEOUT; me->cwd = HTGetCurrentDirectoryURL(); me->output = OUTPUT; me->urilist = HTList_new(); me->count = 0; /* We keep an extra timeout request object for the timeout_handler */ me->timeout = HTRequest_new(); /* Bind the Robot object together with the Request Object */ me->request = HTRequest_new(); HTRequest_setContext (me->request, me); HTRequest_setPreemptive(me->request, YES); /* Make a new profile */ HTProfile_newPreemptiveRobot ("w3clibtcl", "1.0"); return me; }
int optionsURL_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv) { if (argc == 2) { char *url = argv[1]; if (url) { HTRequest *req = HTRequest_new(); BOOL work; HTChunk *chunk = HTChunk_new(0x2000); HTStream *stream = HTStreamToChunk(req, &chunk, 0); HTRequest_setOutputFormat(req, DEFAULT_FORMAT); HTRequest_setOutputStream(req, stream); HTRequest_setPreemptive(req, YES); work = HTOptionsAbsolute(url, req); Tcl_AppendResult(interp, work ? "YES" : "NO", HTChunkData(chunk), NULL); HT_FREE(url); HTRequest_kill(req); HT_FREE(stream); HTChunkFree(chunk); return TCL_OK; } Tcl_AppendResult(interp, bad_vars, NULL); return TCL_ERROR; } else { Tcl_AppendResult(interp, err_string, argv[0], " url", NULL); return TCL_ERROR; } }
int postURL_tcl(ClientData clientData, Tcl_Interp *interp, int argc, char **argv) { if (argc == 3) { char *url = argv[1]; char *dest = argv[2]; if (url && dest) { HTParentAnchor *source = (HTParentAnchor *) HTAnchor_findAddress(url); HTRequest *req = HTRequest_new(); HTChunk *chunk = HTChunk_new(0x2000); HTStream *stream = HTStreamToChunk(req, &chunk, 0); BOOL work; HTRequest_setOutputFormat(req, DEFAULT_FORMAT); HTRequest_setOutputStream(req, stream); HTRequest_setPreemptive(req, YES); work = HTPostAbsolute (source, dest, req); Tcl_AppendResult(interp, work ? "YES" : "NO", HTChunkData(chunk), NULL); HT_FREE(url); HTRequest_kill(req); HTAnchor_delete(source); HT_FREE(stream); HTChunkFree(chunk); return TCL_OK; } Tcl_AppendResult(interp, bad_vars, NULL); return TCL_ERROR; } else { Tcl_AppendResult(interp, err_string, argv[0], " url destination", NULL); return TCL_ERROR; } }
int main (int argc, char ** argv) { HTRequest * request = NULL; HTAnchor * anchor = NULL; char * uri = NULL; /* Create a new premptive client */ HTProfile_newNoCacheClient("UpgradeApp", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Set trace messages and alert messages*/ #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own AFTER filter to terminate the app */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Don't ask - don't tell */ HTAlert_setInteractive(NO); /* Setup a timeout on the request for 15 secs */ HTHost_setEventTimeout(15000); /* Handle command line args */ if (argc >= 2) uri = argv[1]; if (uri && *uri) { /* Create a request */ request = HTRequest_new(); /* Get an anchor object for the URI */ anchor = HTAnchor_findAddress(uri); /* Get the source stream asis */ HTRequest_setOutputFormat(request, WWW_SOURCE); /* Get the source stream asis */ HTRequest_setOutputStream(request, HTFWriter_new(request, stdout, YES)); /* Issue the GET */ if (HTLoadAnchor(anchor, request)) { /* Go into the event loop... */ HTEventList_newLoop(); } } else { HTPrint("Type the URI to get (to test for upgrade).\n"); HTPrint("If an 101 Switching Protocols status is recieved\n"); HTPrint("then print everything following to stdout.\n\n"); HTPrint("\t%s <uri>\n", argv[0]); } return 0; }
int main (int argc, char ** argv) { char * uri = NULL; /* Create a new premptive client */ HTProfile_newHTMLNoCacheClient ("ShowLinks", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Set trace messages and alert messages */ #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own termination filter */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* ** Register our HTML link handler. We don't actually create a HText ** object as this is not needed. We only register the specific link ** callback. */ HText_registerLinkCallback(foundLink); /* Setup a timeout on the request for 15 secs */ HTHost_setEventTimeout(15000); /* Handle command line args */ if (argc >= 2) uri = HTParse(argv[1], NULL, PARSE_ALL); if (uri) { HTRequest * request = NULL; HTAnchor * anchor = NULL; BOOL status = NO; /* Create a request */ request = HTRequest_new(); /* Get an anchor object for the URI */ anchor = HTAnchor_findAddress(uri); /* Issue the GET and store the result in a chunk */ status = HTLoadAnchor(anchor, request); /* Go into the event loop... */ if (status == YES) HTEventList_loop(request); } else { HTPrint("Type the URI to print out a list of embedded links\n"); HTPrint("\t%s <uri>\n", argv[0]); HTPrint("For example:\n"); HTPrint("\t%s http://www.w3.org\n", argv[0]); } return 0; }
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); } }
int main (int argc, char ** argv) { char * uri = NULL; /* Create a new premptive client */ HTProfile_newHTMLNoCacheClient ("ShowXML", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Set trace messages */ #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own termination filter */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Register our new XML Instance handler */ HTXMLCallback_registerNew (HTXML_newInstance, NULL); /* Setup a timeout on the request for 15 secs */ HTHost_setEventTimeout(15000); /* Handle command line args */ if (argc >= 2) uri = HTParse(argv[1], NULL, PARSE_ALL); if (uri) { HTRequest * request = NULL; HTAnchor * anchor = NULL; BOOL status = NO; /* Create a request */ request = HTRequest_new(); /* Get an anchor object for the URI */ anchor = HTAnchor_findAddress(uri); /* Issue the GET and store the result in a chunk */ status = HTLoadAnchor(anchor, request); /* Go into the event loop... */ if (status == YES) HTEventList_loop(request); } else { HTPrint("Type the URI to parse\n"); HTPrint("\t%s <uri>\n", argv[0]); HTPrint("For example:\n"); HTPrint("\t%s http://www.yoursite.com/your.xml\n", argv[0]); } return 0; }
int main (int argc, char ** argv) { char * uri = NULL; char * cwd = HTGetCurrentDirectoryURL(); /* Create a new premptive client */ HTProfile_newHTMLNoCacheClient ("ParseRDF", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Set trace messages */ #if 0 HTSetTraceMessageMask("sopx"); #endif /* Add our own termination filter */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Setup a timeout on the request for 15 secs */ HTHost_setEventTimeout(15000); /* Handle command line args */ if (argc >= 2) uri = HTParse(argv[1], cwd, PARSE_ALL); HT_FREE(cwd); /* Set up our RDF To Triple conveter stream */ HTFormat_addConversion("text/rdf", "*/*", HTRDFParser_new, 1.0, 0.0, 0.0); /* Set our new RDF parser instance callback */ HTRDF_registerNewParserCallback (new_parser_handler, NULL); if (uri) { HTRequest * request = HTRequest_new(); HTAnchor * anchor = HTAnchor_findAddress(uri); BOOL status = NO; /* Start the GET request */ status = HTLoadAnchor(anchor, request); /* Go into the event loop... */ if (status == YES) HTEventList_loop(request); } else { HTPrint("Type the URI to parse\n"); HTPrint("\t%s <uri>\n", argv[0]); HTPrint("For example:\n"); HTPrint("\t%s http://www.yoursite.com/your.rdf\n", argv[0]); } return 0; }
PRIVATE HTRequest * create_request (void) { HTRequest * me = HTRequest_new(); /* set output and debug streans */ HTRequest_setOutputStream(me, HTFWriter_new(me, stdout, YES)); HTRequest_setDebugStream(me, HTFWriter_new(me, stderr, YES)); HTRequest_setOutputFormat(me,HTAtom_for("text/xml")); HTRequest_setDebugFormat(me,WWW_SOURCE); return me; }
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; }
/* Create a Command Line Object ** ---------------------------- */ PRIVATE ComLine * ComLine_new (void) { ComLine * me; if ((me = (ComLine *) HT_CALLOC(1, sizeof(ComLine))) == NULL) HT_OUTOFMEM("ComLine_new"); me->timer = DEFAULT_TIMEOUT*MILLIES; me->cwd = HTGetCurrentDirectoryURL(); me->output = OUTPUT; /* Bind the ConLine object together with the Request Object */ me->request = HTRequest_new(); HTRequest_setOutputFormat(me->request, DEFAULT_FORMAT); HTRequest_setContext (me->request, me); return me; }
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; }
int main (int argc, char ** argv) { int status = 0; int arg = 0; char * outputfile = NULL; char * getme = NULL; HTRequest * request = NULL; /* Initiate W3C Reference Library with a client profile */ HTProfile_newNoCacheClient(APP_NAME, APP_VERSION); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own filter to terminate the application */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Setup cookies */ HTCookie_init(); HTCookie_setCallbacks(setCookie, NULL, findCookie, NULL); /* Set the timeout for long we are going to wait for a response */ HTHost_setEventTimeout(10000); /* Scan command line for parameters */ for (arg=1; arg<argc; arg++) { if (!strcmp(argv[arg], "-o")) { outputfile = (arg+1 < argc && *argv[arg+1] != '-') ? argv[++arg] : DEFAULT_OUTPUT_FILE; } else { getme = argv[arg]; } } /* Make sure we have an output */ if (!outputfile) outputfile = DEFAULT_OUTPUT_FILE; if (getme && *getme) { request = HTRequest_new(); /* Start the load */ status = HTLoadToFile(getme, request, outputfile); /* Go into the event loop... */ HTEventList_loop(request); } else { HTPrint("Type the URI of document you want to load and the name of the local file.\n"); HTPrint("\t%s <address> -o <localfile>\n", argv[0]); HTPrint("For example, %s http://www.w3.org -o w3chome.html\n", argv[0]); /* Delete our profile if no load */ HTProfile_delete(); } return 0; }
/* =============== inspired from http://www.w3.org/Library/Examples/LoadToFile.c setup the download, return once we have a connection =============== */ int DL_BeginDownload( const char *localName, const char *remoteName, int debug ) { char *access = NULL; char *url = NULL; char *login = NULL; char *path = NULL; char *ptr = NULL; if ( dl_running ) { Com_Printf(_( "ERROR: DL_BeginDownload called with a download request already active\n" )); return 0; } terminate_status = HT_UNDEF; #ifdef HTDEBUG if ( debug ) { WWWTRACE = SHOW_ALL_TRACE; } #endif if ( !localName || !remoteName ) { Com_DPrintf( "Empty download URL or empty local file name\n" ); return 0; } DL_InitDownload(); /* need access for ftp behaviour and HTTP Basic Auth */ access = HTParse( remoteName, "", PARSE_ACCESS ); /* Set the timeout for how long we are going to wait for a response This needs to be set and reset to 0 after dl each time idcvs/2003-January/000449.html http://lists.w3.org/Archives/Public/www-lib/2003AprJun/0033.html In case of ftp download, we leave no timeout during connect phase cause of libwww bugs show_bug.cgi?id=605 */ if ( !Q_stricmp( access, "ftp" ) ) { dl_is_ftp = 1; HTHost_setEventTimeout( -1 ); } else { dl_is_ftp = 0; HTHost_setEventTimeout( 30000 ); } dl_request = HTRequest_new(); /* HTTP Basic Auth */ if ( !Q_stricmp( access, "http" ) ) { HTBasic *basic; login = HTParse( remoteName, "", PARSE_HOST ); path = HTParse( remoteName, "", PARSE_PATH + PARSE_PUNCTUATION ); ptr = strchr( login, '@' ); if ( ptr ) { /* Uid and/or passwd specified */ char *passwd; *ptr = '\0'; passwd = strchr( login, ':' ); if ( passwd ) { /* Passwd specified */ *passwd++ = '\0'; HTUnEscape( passwd ); } HTUnEscape( login ); /* proactively set the auth */ basic = HTBasic_new(); StrAllocCopy( basic->uid, login ); StrAllocCopy( basic->pw, passwd ); basic_credentials( dl_request, basic ); HTBasic_delete( basic ); /* correct the HTTP */ url = HT_MALLOC( 7 + strlen( ptr + 1 ) + strlen( path ) + 1 ); sprintf( url, "http://%s%s", ptr + 1, path ); Com_DPrintf( "HTTP Basic Auth – %s %s %s\n", login, passwd, url ); HT_FREE( login ); HT_FREE( path ); } else { StrAllocCopy( url, remoteName ); } } else { StrAllocCopy( url, remoteName ); } HT_FREE( access ); FS_CreatePath( localName ); /* Start the load */ if ( HTLoadToFile( url, dl_request, localName ) != YES ) { Com_DPrintf( "HTLoadToFile failed\n" ); HT_FREE( url ); HTProfile_delete(); return 0; } HT_FREE( url ); /* remove possible login/pass part for the ui */ access = HTParse( remoteName, "", PARSE_ACCESS ); login = HTParse( remoteName, "", PARSE_HOST ); path = HTParse( remoteName, "", PARSE_PATH + PARSE_PUNCTUATION ); ptr = strchr( login, '@' ); if ( ptr ) { /* Uid and/or passwd specified */ Cvar_Set( "cl_downloadName", va( "%s://*:*%s%s", access, ptr, path ) ); } else { Cvar_Set( "cl_downloadName", remoteName ); } HT_FREE( path ); HT_FREE( login ); HT_FREE( access ); if ( dl_is_ftp ) { HTHost_setEventTimeout( 30000 ); } /* Go into the event loop... */ HTEventList_init( dl_request ); dl_running = 1; return 1; }
/* Partial Response MIME parser stream ** ----------------------------------- ** In case we sent a Range conditional GET we may get back a partial ** response. This response must be appended to the already existing ** cache entry before presented to the user. ** We do this by continuing to load the new object into a temporary ** buffer and at the same time start the cache load of the already ** existing object. When we have loaded the cache we merge the two ** buffers. */ PUBLIC HTStream * HTMIMEPartial (HTRequest * request, void * param, HTFormat input_format, HTFormat output_format, HTStream * output_stream) { #ifndef NO_CACHE HTParentAnchor * anchor = HTRequest_anchor(request); HTFormat format = HTAnchor_format(anchor); HTStream * pipe = NULL; /* ** The merge stream is a place holder for where we can put data when it ** arrives. We have two feeds: one from the cache and one from the net. ** We call the stream stack already now to get the right output stream. ** We can do this as we already know the content type from when we got the ** first part of the object. */ HTStream * merge = HTMerge(HTStreamStack(format, output_format, output_stream, request, YES), 2); /* ** Now we create the MIME parser stream in partial data mode. We also ** set the target to our merge stream. */ HTStream * me = HTMIMEConvert(request, param, input_format, output_format, output_stream); me->mode |= HT_MIME_PARTIAL; me->target = merge; #if 0 /* JK: this doesn't work because this work is repeated before */ /* ** Create the cache append stream, and a Tee stream */ { HTStream * append = HTStreamStack(WWW_CACHE_APPEND, output_format, output_stream, request, NO); if (append) me->target = HTTee(me->target, append, NULL); } #endif /* ** Create the pipe buffer stream to buffer the data that we read ** from the network */ if ((pipe = HTPipeBuffer(me->target, 0))) me->target = pipe; /* ** Now start the second load from the cache. First we read this data from ** the cache and then we flush the data that we have read from the net. */ { HTRequest * cache_request = HTRequest_new(); /* ** Set the output format to source and the output stream to the ** merge stream. As we have already set up the stream pipe, we just ** load it as source. */ HTRequest_setOutputFormat(cache_request, WWW_SOURCE); HTRequest_setOutputStream(cache_request, merge); /* ** Bind the anchor to the new request and also register a local ** AFTER filter to flush the pipe buffer so that we can get ** rest of the data through. */ HTRequest_setAnchor(cache_request, (HTAnchor *) anchor); HTRequest_addBefore(cache_request, HTCacheLoadFilter, NULL, NULL, HT_FILTER_FIRST, YES); HTRequest_addAfter(cache_request, HTCacheFlushFilter, NULL, pipe, HT_ALL, HT_FILTER_FIRST, YES); HTTRACE(STREAM_TRACE, "Partial..... Starting cache load\n"); HTLoad(cache_request, NO); } return me; #else return NULL; #endif }
PRIVATE int ServEvent (SOCKET soc, void * pVoid, HTEventType type) { https_info * http = (https_info *)pVoid; int status = HT_ERROR; HTNet * net = http->net; HTRequest * request = HTNet_request(net); if (!net || !request) { HTTRACE(PROT_TRACE, "Serv HTTP... Invalid argument\n"); return HT_ERROR; } if (type == HTEvent_CLOSE) { /* Interrupted */ ServerCleanup(request, net, HT_INTERRUPTED); return HT_OK; } else http = (https_info *) HTNet_context(net); /* Get existing copy */ /* Now jump into the machine. We know the state from the previous run */ while (1) { switch (http->state) { case HTTPS_BEGIN: { /* ** Create the request to handle the request and inherit the old ** context */ HTRequest * client = HTRequest_new(); void * context = HTRequest_context(request); if (context) HTRequest_setContext(client, context); HTRequest_setOutputConnected(client, NO); HTRequest_setGnHd(client, HTRequest_gnHd(request)); HTRequest_setRsHd(client, HTRequest_rsHd(request)); HTRequest_setEnHd(client, HTRequest_enHd(request)); HTList_addObject(http->clients, client); /* ** Create the HTTP output stream for generating the reply ** FROM the client request to the channel */ { HTOutputStream * output = HTNet_getOutput(net, NULL, 0); HTStream * app = HTTPReply_new(client, http,(HTStream*)output); HTRequest_setOutputStream(client, app); HTRequest_setOutputFormat(client, WWW_SOURCE); } http->state = HTTPS_NEED_REQUEST; } break; case HTTPS_NEED_REQUEST: if (type == HTEvent_READ || type == HTEvent_BEGIN) { status = HTHost_read(net->host, net); if (status == HT_WOULD_BLOCK) return HT_OK; else if (status == HT_CLOSED) http->state = HTTPS_OK; else if (status==HT_LOADED || status==HT_PAUSE) { http->state = HTTPS_LOAD_CLIENT; } else http->state = HTTPS_ERROR; } else http->state = HTTPS_ERROR; break; case HTTPS_LOAD_CLIENT: { HTRequest * client = HTList_removeFirstObject(http->clients); HTLoad(client, NO); http->state = HTTPS_BEGIN; break; } case HTTPS_OK: ServerCleanup(request, net, HT_IGNORE); return HT_OK; case HTTPS_ERROR: ServerCleanup(request, net, HT_ERROR); return HT_OK; } } }
int main (int argc, char ** argv) { HTRequest * request = NULL; HTAnchor * src = NULL; HTAnchor * dst = NULL; char * src_str = NULL; char * dst_str = NULL; BOOL status = NO; /* Create a new premptive client */ HTProfile_newNoCacheClient("libwww-PUT", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* And the traces... */ #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own filter to update the history list */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Handle command line args */ if (argc >= 3) { src_str = argv[1]; dst_str = argv[2]; } else { HTPrint("Type the URI of the source and the URI of the destination.\n"); HTPrint("\t%s <src> <dst>\n", argv[0]); HTPrint("For example, %s http://www.w3.org http://myserver/destination.html\n", argv[0]); return -1; } if (src_str && *src_str && dst_str && *dst_str) { /* Make source relative to where we are */ char * cwd = HTGetCurrentDirectoryURL(); char * full_src_str = HTParse(src_str, cwd, PARSE_ALL); HTPrint("Saving %s to %s\n", full_src_str, dst_str); /* Create a request */ request = HTRequest_new(); /* Get an anchor object for the src and dest URIs */ src = HTAnchor_findAddress(full_src_str); dst = HTAnchor_findAddress(dst_str); /* PUT the source to the dest */ status = HTPutDocumentAnchor(HTAnchor_parent(src), dst, request); /* We don't need these anymore */ HT_FREE(cwd); HT_FREE(full_src_str); /* Go into the event loop... */ if (status == YES) HTEventList_loop(request); } return 0; }
int main (int argc, char ** argv) { HTRequest * request = NULL; HTAnchor * anchor = NULL; HTAssocList * formfields = NULL; char * uri = NULL; /* Create a new premptive client */ HTProfile_newNoCacheClient("TestApp", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Get trace messages */ #if 0 HTSetTraceMessageMask("sop"); #endif /* Add our own filter to update the history list */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Set the timeout for long we are going to wait for a response */ HTHost_setEventTimeout(20000); /* Handle command line args */ if (argc >= 2) { int arg; uri = argv[1]; for (arg=2; arg<argc; arg++) { char * string = argv[arg]; /* Create a list to hold the form arguments */ if (!formfields) formfields = HTAssocList_new(); /* Parse the content and add it to the association list */ HTParseFormInput(formfields, string); } } if (uri && formfields) { /* Create a request */ request = HTRequest_new(); /* Set the default output to "asis" */ HTRequest_setOutputFormat(request, WWW_SOURCE); /* Get an anchor object for the URI */ anchor = HTAnchor_findAddress(uri); /* Post the data and get the result in a chunk */ result = HTPostFormAnchorToChunk(formfields, anchor, request); /* Clean up the form fields */ HTAssocList_delete(formfields); /* Go into the event loop... */ HTEventList_loop(request); } else { HTPrint("Type the URI to post to and the arguments for the POST operation. Encode spaces as '+'\n"); HTPrint("\t%s <uri> 'a=1' 'b=+2+' 'c=3 ...'\n", argv[0]); } return 0; }
int main (int argc, char ** argv) { HTRequest * request = NULL; HTParentAnchor * src = NULL; HTAnchor * dst = NULL; char * dst_str = NULL; char * data = NULL; BOOL status = NO; /* Create a new premptive client */ HTProfile_newNoCacheClient("libwww-POST", "1.0"); /* Need our own trace and print functions */ HTPrint_setCallback(printer); HTTrace_setCallback(tracer); /* Add our own filter to update the history list */ HTNet_addAfter(terminate_handler, NULL, NULL, HT_ALL, HT_FILTER_LAST); /* Handle command line args */ if (argc >= 3) { dst_str = argv[1]; data = argv[2]; } else { HTPrint("Type the URI of the destination you want to POST to and the contents that you want to post.\n"); HTPrint("\t%s <destination> <data>\n", argv[0]); HTPrint("For example, %s http://myserver/destination.html \"This is some testdata\"\n", argv[0]); return -1; } if (data && *data && dst_str && *dst_str) { /* Make source relative to where we are */ char * cwd = HTGetCurrentDirectoryURL(); HTPrint("Posting to %s\n", dst_str); /* Create a request */ request = HTRequest_new(); /* Get an anchor object for the destination URI */ dst = HTAnchor_findAddress(dst_str); /* ** Dream up a source anchor (an editor can for example use this). ** After creation we associate the data that we want to post and ** set some metadata about what the data is. More formats can be found ** ../src/HTFormat.html */ src = HTTmpAnchor(NULL); HTAnchor_setDocument(src, data); HTAnchor_setFormat(src, WWW_PLAINTEXT); /* ** If not posting to an HTTP/1.1 server then content length MUST be ** there. If HTTP/1.1 then it doesn't matter as we just use chunked ** encoding under the covers */ HTAnchor_setLength(src, strlen(data)); /* POST the source to the dest */ status = HTPostAnchor(src, dst, request); /* We don't need these anymore */ HT_FREE(cwd); /* Go into the event loop... */ if (status == YES) HTEventList_loop(request); } return 0; }
int main (int argc, char ** argv) { ListenTool * ms = ListenTool_new(); int status = HT_ERROR; int arg = 0; /* ** Initiate libwww. Note that we don't call a profile as these set up ** all the client side specific things. Here we just want a very basic ** server which doesn't do anything but forwarding what it reads to ** stdout. */ HTLibInit(APP_NAME, APP_VERSION); /* Set up default event loop */ HTEventInit(); /* 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); /* Check if we should just provide some help */ if (argc==1) { VersionInfo(argv[0]); Cleanup(ms, 0); } /* Scan command Line for parameters */ for (arg=1; arg<argc ; arg++) { if (*argv[arg] == '-') { /* server port */ if (!strncmp(argv[arg], "-p", 2)) { ms->port = (arg+1 < argc && *argv[arg+1] != '-') ? atoi(argv[++arg]) : DEFAULT_PORT; /* Print version and exit */ } else if (!strcmp(argv[arg], "-version")) { VersionInfo(argv[0]); Cleanup(ms, 0); #ifdef HTDEBUG /* trace flags */ } else if (!strncmp(argv[arg], "-v", 2)) { HTSetTraceMessageMask(argv[arg]+2); #endif } else { VersionInfo(argv[0]); Cleanup(ms, 0); } } else { VersionInfo(argv[0]); Cleanup(ms, 0); } } /* Set up a tool to listen on this port */ if (ms->port >= 0) { /* Register TCP as the transport */ HTTransport_add("tcp", HT_TP_SINGLE, HTReader_new, HTWriter_new); /* Register the "noop" application layer protocol for reading */ HTProtocol_add("noop", "tcp", ms->port, NO, NULL, HTLoadSocket); /* Set up the request */ ms->request = HTRequest_new(); HTRequest_setOutputFormat(ms->request, DEFAULT_FORMAT); HTRequest_setOutputStream(ms->request, HTFWriter_new(ms->request, OUTPUT, YES)); /* Start listening on the socket */ HTPrint("Listening on port %d\n", ms->port); if ((status = HTServeAbsolute("noop://localhost", ms->request)) == NO) { HTPrint("Can't listen on port %d\n", ms->port); Cleanup(ms, -1); } /* Go into the event loop... */ if (status == YES) HTEventList_newLoop(); } Cleanup(ms, 0); return 0; }
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; }