📄 htreqman.c
字号:
} return NO;}PUBLIC HTList * HTRequest_before (HTRequest * me, BOOL *override){ if (me) { *override = me->befores_local; return me->befores; } return NULL;}PUBLIC BOOL HTRequest_addAfter (HTRequest * me, HTNetAfter * filter, const char * tmplate, void * param, int status, HTFilterOrder order, BOOL override){ if (me) { me->afters_local = override; if (filter) { if (!me->afters) me->afters = HTList_new(); return HTNetCall_addAfter(me->afters, filter, tmplate, param, status, order); } return YES; /* It's OK to register a NULL filter */ } return NO;}PUBLIC BOOL HTRequest_deleteAfter (HTRequest * me, HTNetAfter * filter){ return (me && me->afters) ? HTNetCall_deleteAfter(me->afters, filter) : NO;}PUBLIC BOOL HTRequest_deleteAfterStatus (HTRequest * me, int status){ return (me && me->afters) ? HTNetCall_deleteAfterStatus(me->afters, status) : NO;}PUBLIC BOOL HTRequest_deleteAfterAll (HTRequest * me){ if (me && me->afters) { HTNetCall_deleteAfterAll(me->afters); me->afters = NULL; me->afters_local = NO; return YES; } return NO;}PUBLIC HTList * HTRequest_after (HTRequest * me, BOOL *override){ if (me) { *override = me->afters_local; return me->afters; } return NULL;}/*** Call back function for context swapping*/PUBLIC void HTRequest_setCallback (HTRequest * me, HTRequestCallback *cbf){ if (me) me->callback = cbf;}PUBLIC HTRequestCallback *HTRequest_callback (HTRequest * me){ return me ? me->callback : NULL;}/*** Context pointer to be used in context call back function*/PUBLIC void HTRequest_setContext (HTRequest * me, void *context){ if (me) me->context = context;}PUBLIC void *HTRequest_context (HTRequest * me){ return me ? me->context : NULL;}/*** Has output stream been connected to the channel? If not then we** must free it explicitly when deleting the request object*/PUBLIC void HTRequest_setOutputConnected (HTRequest * me, BOOL mode){ if (me) me->connected = mode;}PUBLIC BOOL HTRequest_outputConnected (HTRequest * me){ return me ? me->connected : NO;}/*** Bytes read in this request*/PUBLIC long HTRequest_bodyRead(HTRequest * me){ return me ? HTNet_bytesRead(me->net) - HTNet_headerBytesRead(me->net) : -1;}/*** Bytes written in this request*/PUBLIC long HTRequest_bodyWritten(HTRequest * me){ return me ? HTNet_bytesWritten(me->net) - HTNet_headerBytesWritten(me->net) : -1;}/*** Total Bytes read in this request*/PUBLIC long HTRequest_bytesRead (HTRequest * me){ return me ? HTNet_bytesRead(me->net) : -1;}/*** Bytes written in this request*/PUBLIC long HTRequest_bytesWritten (HTRequest * me){ return me ? HTNet_bytesWritten(me->net) : -1;}/*** Handle the max forward header value*/PUBLIC BOOL HTRequest_setMaxForwards (HTRequest * me, int maxforwards){ if (me && maxforwards >= 0) { me->max_forwards = maxforwards; HTRequest_addRqHd(me, HT_C_MAX_FORWARDS); /* Turn it on */ return YES; } return NO;}PUBLIC int HTRequest_maxForwards (HTRequest * me){ return me ? me->max_forwards : -1;}/*** Source request*/PUBLIC BOOL HTRequest_setSource (HTRequest * me, HTRequest * source){ if (me) { me->source = source; return YES; } return NO;}PUBLIC HTRequest * HTRequest_source (HTRequest * me){ return (me ? me->source : NULL);}PUBLIC BOOL HTRequest_isPostWeb (HTRequest * me){ return (me ? me->source != NULL: NO);}/*** POST Call back function for sending data to the destination*/PUBLIC void HTRequest_setPostCallback (HTRequest * me, HTPostCallback *cbf){ if (me) me->PostCallback = cbf;}PUBLIC HTPostCallback * HTRequest_postCallback (HTRequest * me){ return me ? me->PostCallback : NULL;}/*** Entity Anchor*/PUBLIC BOOL HTRequest_setEntityAnchor (HTRequest * me, HTParentAnchor * anchor){ if (me) { me->source_anchor = anchor; return YES; } return NO;}PUBLIC HTParentAnchor * HTRequest_entityAnchor (HTRequest * me){ return me ? me->source_anchor ? me->source_anchor : me->anchor : NULL;}/* ------------------------------------------------------------------------- *//* POST WEB METHODS *//* ------------------------------------------------------------------------- *//*** Add a destination request to this source request structure so that we** build the internal request representation of the POST web** Returns YES if OK, else NO*/PUBLIC BOOL HTRequest_addDestination (HTRequest * src, HTRequest * dest){ if (src && dest) { dest->source = src->source = src; if (!src->mainDestination) { src->mainDestination = dest; src->destRequests = 1; HTTRACE(CORE_TRACE, "POSTWeb..... Adding dest %p to src %p\n" _ dest _ src); return YES; } else { if (!src->destinations) src->destinations = HTList_new(); if (HTList_addObject(src->destinations, (void *) dest)==YES) { src->destRequests++; HTTRACE(CORE_TRACE, "POSTWeb..... Adding dest %p to src %p\n" _ dest _ src); return YES; } } } return NO;}/*** Remove a destination request from this source request structure** Remember only to delete the internal request objects as the other** comes from the application!** Returns YES if OK, else NO*/PUBLIC BOOL HTRequest_removeDestination (HTRequest * dest){ BOOL found=NO; if (dest && dest->source) { HTRequest *src = dest->source; if (src->mainDestination == dest) { dest->source = NULL; src->mainDestination = NULL; src->destRequests--; found = YES; } else if (src->destinations) { if (HTList_removeObject(src->destinations, (void *) dest)) { src->destRequests--; found = YES; } } if (found) { if (dest->internal) HTRequest_delete(dest); HTTRACE(CORE_TRACE, "POSTWeb..... Deleting dest %p from src %p\n" _ dest _ src); } if (src->destRequests <= 0) { HTTRACE(CORE_TRACE, "POSTWeb..... terminated\n"); if (src->internal) HTRequest_delete(src); } } return found;}/*** Check to see whether all destinations are ready. If so then enable the** source as ready for reading.** Returns YES if all dests are ready, NO otherwise*/PUBLIC BOOL HTRequest_destinationsReady (HTRequest * me){ HTRequest * source = me ? me->source : NULL; if (source) { if (source->destStreams == source->destRequests) { HTNet * net = source->net; HTTRACE(CORE_TRACE, "POSTWeb..... All destinations are ready!\n"); if (net) /* Might already have finished */ HTEvent_register(HTNet_socket(net), HTEvent_READ, &net->event); return YES; } } return NO;}/*** Find the source request object and make the link between the ** source output stream and the destination input stream. There can be** a conversion between the two streams!** Returns YES if link is made, NO otherwise*/PUBLIC BOOL HTRequest_linkDestination (HTRequest *dest){ if (dest && dest->input_stream && dest->source && dest!=dest->source) { HTRequest *source = dest->source; HTStream *pipe = HTStreamStack(source->output_format, dest->input_format, dest->input_stream, dest, YES); /* Check if we are the only one - else spawn off T streams */ /* @@@ We don't do this yet @@@ */ /* Now set up output stream of the source */ if (source->output_stream) (*source->output_stream->isa->_free)(source->output_stream); source->output_stream = pipe ? pipe : dest->input_stream; HTTRACE(CORE_TRACE, "POSTWeb..... Linking dest %p to src %p\n" _ dest _ source); if (++source->destStreams == source->destRequests) { HTNet *net = source->net; HTTRACE(CORE_TRACE, "POSTWeb..... All destinations ready!\n"); if (net) /* Might already have finished */ HTEvent_register(HTNet_socket(net), HTEvent_READ, &net->event); return YES; } } return NO;}/*** Remove a feed stream to a destination request from this source** request structure. When all feeds are removed the request tree is** ready to take down and the operation can be terminated.** Returns YES if removed, else NO*/PUBLIC BOOL HTRequest_unlinkDestination (HTRequest *dest){ BOOL found = NO; if (dest && dest->source && dest != dest->source) { HTRequest *src = dest->source; if (src->mainDestination == dest) { src->output_stream = NULL; if (dest->input_stream) (*dest->input_stream->isa->_free)(dest->input_stream); found = YES; } else if (src->destinations) { /* LOOK THROUGH THE LIST AND FIND THE RIGHT ONE */ } if (found) { src->destStreams--; HTTRACE(CORE_TRACE, "POSTWeb..... Unlinking dest %p from src %p\n" _ dest _ src); return YES; } } return NO;}/*** Removes all request structures in this PostWeb.*/PUBLIC BOOL HTRequest_removePostWeb (HTRequest *me){ if (me && me->source) { HTRequest *source = me->source; /* Kill main destination */ if (source->mainDestination) HTRequest_removeDestination(source->mainDestination); /* Kill all other destinations */ if (source->destinations) { HTList *cur = source->destinations; HTRequest *pres; while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) HTRequest_removeDestination(pres); } /* Remove source request */ HTRequest_removeDestination(source); return YES; } return NO;}/*** Kills all threads in a POST WEB connected to this request but** NOT this request itself. We also keep the request structures.** Some requests might be preemptive, for example a SMTP request (when** that has been implemented). However, this will be handled internally** in the load function.*/PUBLIC BOOL HTRequest_killPostWeb (HTRequest *me){ if (me && me->source) { HTRequest *source = me->source; HTTRACE(CORE_TRACE, "POSTWeb..... Killing\n"); /* ** Kill source. The stream tree is now freed so we have to build ** that again. This is done in HTRequest_linkDestination() */ if (me != source) { HTNet_kill(source->net); source->output_stream = NULL; } /* Kill all other destinations */ if (source->destinations) { HTList *cur = source->destinations; HTRequest *pres; while ((pres = (HTRequest *) HTList_nextObject(cur)) != NULL) if (me != pres) HTNet_kill(pres->net); } /* Kill main destination */ if (source->mainDestination && me != source->mainDestination) HTNet_kill(source->mainDestination->net); return YES; } return NO;}PUBLIC int HTRequest_forceFlush (HTRequest * request){ HTHost * host = HTNet_host(request->net); if (host == NULL) return HT_ERROR; return HTHost_forceFlush(host);}/* --------------------------------------------------------------------------*//* Document Loader *//* --------------------------------------------------------------------------*//* Request a resource** ------------------** This is an internal routine, which has an address AND a matching** anchor. (The public routines are called with one OR the other.)** Returns:** YES if request has been registered (success)** NO an error occured*/PUBLIC BOOL HTLoad (HTRequest * me, BOOL recursive){ if (!me || !me->anchor) { HTTRACE(CORE_TRACE, "Load Start.. Bad argument\n"); return NO; } /* Make sure that we don't carry over any old physical address */ if (!recursive) HTAnchor_clearPhysical(me->anchor); /* Set the default method if not already done */ if (me->method == METHOD_INVALID) me->method = METHOD_GET; /* Should we keep the error stack or not? */ if (!recursive && me->error_stack) { HTError_deleteAll(me->error_stack); me->error_stack = NULL; } /* Delete any old Response Object */ if (me->response) { HTResponse_delete(me->response); me->response = NULL; } /* ** We set the start point of handling a request to here. ** This time will be used by the cache */ HTRequest_setDate(me, time(NULL)); /* Now start the Net Manager */ return HTNet_newClient(me);}PUBLIC BOOL HTServe (HTRequest * me, BOOL recursive){ if (!me || !me->anchor) { HTTRACE(CORE_TRACE, "Serve Start. Bad argument\n"); return NO; } /* Make sure that we don't carry over any old physical address */ if (!recursive) HTAnchor_clearPhysical(me->anchor); /* Should we keep the error stack or not? */ if (!recursive && me->error_stack) { HTError_deleteAll(me->error_stack); me->error_stack = NULL; } /* Delete any old Response Object */ if (me->response) { HTResponse_delete(me->response); me->response = NULL; } /* Now start the Net Manager */ return HTNet_newServer(me);}/* --------------------------------------------------------------------------*//* Message Body *//* --------------------------------------------------------------------------*//*** This function sets the request's message body*/PUBLIC BOOL HTRequest_setMessageBody (HTRequest * request, const char * body) {#ifdef HT_EXT if (request && body && *body){ StrAllocCopy (request->messageBody,body); return YES; }#endif /* HT_EXT */ return NO; }/*** This function deletes the message body, freeing the string and** setting it to NULL.*/PUBLIC BOOL HTRequest_deleteMessageBody (HTRequest * request) {#ifdef HT_EXT if (request && request->messageBody) { HT_FREE (request->messageBody); request->messageBody = NULL; return YES; } #endif /* HT_EXT */ return NO;}/*** This function creates a copy of the message body*/PUBLIC char * HTRequest_messageBody (HTRequest * request) { char * bodycopy = NULL; #ifdef HT_EXT if (request && request->messageBody && *(request->messageBody)) StrAllocCopy(bodycopy,request->messageBody); #endif /* HT_EXT */ return bodycopy;}/*** This function sets the length of the body. This length will be** used to set Content-Length header.** Note: length should be greater than 0, and the Content-Length** header will be created only if there is a message Body.*/PUBLIC BOOL HTRequest_setMessageBodyLength (HTRequest * request, long int length) {#ifdef HT_EXT if (request && length > 0) { request->messageBodyLength = length; return YES; }#endif /* HT_EXT */ return NO;}/*** This function returns the message body length,** or -1 if it is not set.*/PUBLIC long int HTRequest_messageBodyLength (HTRequest * request) {#ifdef HT_EXT return (request && (request->messageBody && request->messageBodyLength))? request->messageBodyLength:-1;#else return -1;#endif}/*** This function sets the format of the message body to be used** in the Content-Type header.** Note: the Content-Type header will be created only if there is a** message body set.*/PUBLIC BOOL HTRequest_setMessageBodyFormat (HTRequest * request, HTFormat format) { #ifdef HT_EXT if (request && format) { request->messageBodyFormat = format; return YES; }#endif /*HT_EXT*/ return NO;}/*** This function returns the format of the message body, or** NULL if it is not set.*/PUBLIC HTFormat HTRequest_messageBodyFormat (HTRequest * request) {#ifdef HT_EXT if (request && request->messageBodyFormat) return request->messageBodyFormat; else #endif /*HT_EXT*/ return NULL;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -