/* * This function generates a CREATE TABLE statement from * `tbl' and writes it to the file specified by `fd'. */ void table_desc_to_sql(int fd, const struct table_desc *tbl) { char *buff; size_t size; long stored; const struct column_desc *col; size = DEF_BUFF_SIZE; buff = alloc_buff(size); stored = sprintf(buff, "CREATE TABLE %s (\n", tbl->t_name); for (col = tbl->c_head; col; col = col->next) { buff = ensure_capacity(buff, &size, (size_t) stored); stored += sprint_column(buff + stored, col); } buff = ensure_capacity(buff, &size, (size_t) stored); if (tbl->t_pkname && strlen(tbl->t_pkname) > 0) { strcpy(buff + stored, "\n);\n\n"); stored += strlen("\n);\n\n"); sprint_named_pk(buff + stored, tbl); } else { stored += sprint_anon_pk(buff + stored, tbl->c_head); strcpy(buff + stored, "\n);\n"); } write_file(fd, buff); free_buff(buff); }
static void sieve_init_priv_(prime_sieve_t * sieve, const lint_t max_num){ lint_t n = max_num; lint_t bits = get_index(n); while (bits == 0){ ++n; bits = get_index(n); } sieve->max_number = max_num; printf("%lu kb needed for prime sieve up to %lu\n",bits/(8*1024),max_num); sieve->bitbuff = alloc_buff(bits,SIEVE_BUFF_PART_BYTES,1); n=5; for ( ; n<=max_num ; ++n){ lint_t idx = get_index(n); if ((idx > 0 || n==5) && (get_bit(sieve->bitbuff,idx) != 0)){ lint_t mp = n*2; while (mp <= max_num){ idx = get_index(mp); if ((idx > 0) && (get_bit(sieve->bitbuff,idx) != 0)){ clear_bit(sieve->bitbuff,idx); } mp += n; } } } }
/* ** Name: void el3_rx_complete(dpeth_t * dep); ** Function: Upon receiving a packet, provides status checks ** and if packet is OK copies it to local buffer. */ static void el3_rx_complete(dpeth_t * dep) { short int RxStatus; int pktsize; buff_t *rxptr; RxStatus = inw_el3(dep, REG_RxStatus); pktsize = RxStatus & RXS_Length; /* Mask off packet length */ if (RxStatus & RXS_Error) { /* First checks for receiving errors */ RxStatus &= RXS_ErrType; switch (RxStatus) { /* Bad packet (see error type) */ case RXS_Dribble: case RXS_Oversize: case RXS_Runt: dep->de_stat.ets_recvErr += 1; break; case RXS_Overrun: dep->de_stat.ets_OVW += 1; break; case RXS_Framing: dep->de_stat.ets_frameAll += 1; break; case RXS_CRC: dep->de_stat.ets_CRCerr += 1; break; } } else if ((rxptr = alloc_buff(dep, pktsize + sizeof(buff_t))) == NULL) { /* Memory not available. Drop packet */ dep->de_stat.ets_fifoOver += 1; } else { /* Good packet. Read it from FIFO */ insb(dep->de_data_port, SELF, rxptr->buffer, pktsize); rxptr->next = NULL; rxptr->size = pktsize; lock(); /* Queue packet to receive queue */ if (dep->de_recvq_head == NULL) dep->de_recvq_head = rxptr; else dep->de_recvq_tail->next = rxptr; dep->de_recvq_tail = rxptr; unlock(); /* Reply to pending Receive requests, if any */ el3_recv(dep, TRUE, pktsize); } /* Discard top packet from queue */ outw_el3(dep, REG_CmdStatus, CMD_RxDiscard); return; }
static void test_buff(){ buff_t * b = alloc_buff(1573,16,0); lint_t i=0; for ( ; i<b->bit_count ; ++i){ lint_t k=0; set_bit(b,i); assert( 1 == get_bit(b,i) ); for ( ; k<b->bit_count ; ++k){ if (k != i){ assert( 0 == get_bit(b,k) ); } } clear_bit(b,i); } free_buff(b); (void)print_buff; }
t_clientGUI *alloc_clientGUI(SOCKET sd) { t_clientGUI *ret; if (!(ret = malloc(sizeof(*ret)))) return (NULL); if (!(ret->buff_write = alloc_buff())) return (free_va_arg(1, ret)); if (!(ret->listWrite = create_list(ANSWER))) return (free_va_arg(2, ret->buff_write, ret)); ret->buff_read = NULL; ret->socket = sd; ret->error = false; ret->write = false; ret->writeConn = NULL; ret->buff = NULL; return (ret); }
/* ** Name: void init_buff(dpeth_t *dep, buff_t **tx_buff) ** Function: Initalizes driver data structures. */ void init_buff(dpeth_t *dep, buff_t **tx_buff) { /* Initializes buffer pool */ if (allocptr == NULL) { m_hdr_t *rx = (m_hdr_t *) tx_rx_buff; rx->next = allocptr = rx; rx->size = 0; rx += 1; rx->next = NULL; rx->size = (sizeof(tx_rx_buff) / sizeof(m_hdr_t)) - 1; free_buff(dep, rx + 1); dep->de_recvq_tail = dep->de_recvq_head = NULL; if (tx_buff != NULL) { *tx_buff = alloc_buff(dep, ETH_MAX_PACK_SIZE + sizeof(buff_t)); (*tx_buff)->size = 0; } } return; /* Done */ }
// Function to add a new texture of size Width*Height, with fake Texture ID "ID". Return the ID or -1 if failed. int AddStreamed(int width, int height, unsigned int ID) { //printf("AddStreamed(%i, %i, %u)\n", width, height, ID); if (!gl_streaming) return -1; static int i =0; int j=0; while (j<10) { int k = (i+j)%10; if (tex_free[k]) { if (alloc_buff(k, width, height)) { stream_cache[k].active = 1; stream_cache[k].last = frame_number; stream_cache[k].texID = ID; i = (i+j+1)%10; return k; } else { return -1; // Probably useless to try again and again } } j++; } return -1; }
/* ** Name: void el1_interrupt(dpeth_t *dep) ** Function: Interrupt handler. Acknwledges transmit interrupts ** or unloads receive buffer to memory queue. */ static void el1_interrupt(dpeth_t * dep) { u16_t csr, isr; int pktsize; buff_t *rxptr; csr = inb_el1(dep, EL1_CSR); if ((csr & ECSR_XMIT) && (dep->de_flags & DEF_XMIT_BUSY)) { /* Got a transmit interrupt */ isr = inb_el1(dep, EL1_XMIT); if ((isr & (EXSR_16JAM | EXSR_UNDER | EXSR_JAM)) || !(isr & EXSR_IDLE)) { DEBUG(printf("3c501: got xmit interrupt (ASR=0x%02X XSR=0x%02X)\n", csr, isr)); if (isr & EXSR_JAM) { /* Sending, packet got a collision */ dep->de_stat.ets_collision += 1; /* Put pointer back to beginning of packet */ outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS); outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - TxBuff->size)); /* And retrigger transmission */ outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_XMIT); return; } else if ((isr & EXSR_16JAM) || !(isr & EXSR_IDLE)) { dep->de_stat.ets_sendErr += 1; } else if (isr & EXSR_UNDER) { dep->de_stat.ets_fifoUnder += 1; } DEBUG(printf("3c501: got xmit interrupt (0x%02X)\n", isr)); el1_reset(dep); } else { /** if (inw_el1(dep, EL1_XMITPTR) == EL1_BFRSIZ) **/ /* Packet transmitted successfully */ dep->de_stat.ets_packetT += 1; dep->bytes_Tx += (long) (TxBuff->size); free_buff(dep, TxBuff); dep->de_flags &= NOT(DEF_XMIT_BUSY); if ((dep->de_flags & DEF_SENDING) && dep->de_xmitq_head) { /* Pending transmit request available in queue */ el1_send(dep, TRUE, 0); if (dep->de_flags & (DEF_XMIT_BUSY | DEF_ACK_SEND)) return; } } } else if ((csr & (ECSR_RECV | ECSR_XMTBSY)) == (ECSR_RECV | ECSR_XMTBSY)) { /* Got a receive interrupt */ isr = inb_el1(dep, EL1_RECV); pktsize = inw_el1(dep, EL1_RECVPTR); if ((isr & ERSR_RERROR) || (isr & ERSR_STALE)) { DEBUG(printf("Rx0 (ASR=0x%02X RSR=0x%02X size=%d)\n", csr, isr, pktsize)); dep->de_stat.ets_recvErr += 1; } else if (pktsize < ETH_MIN_PACK_SIZE || pktsize > ETH_MAX_PACK_SIZE) { DEBUG(printf("Rx1 (ASR=0x%02X RSR=0x%02X size=%d)\n", csr, isr, pktsize)); dep->de_stat.ets_recvErr += 1; } else if ((rxptr = alloc_buff(dep, pktsize + sizeof(buff_t))) == NULL) { /* Memory not available. Drop packet */ dep->de_stat.ets_fifoOver += 1; } else if (isr & (ERSR_GOOD | ERSR_ANY)) { /* Got a good packet. Read it from buffer */ outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS); outw_el1(dep, EL1_XMITPTR, 0); insb(dep->de_data_port, SELF, rxptr->buffer, pktsize); rxptr->next = NULL; rxptr->size = pktsize; dep->de_stat.ets_packetR += 1; dep->bytes_Rx += (long) pktsize; lock(); /* Queue packet to receive queue */ if (dep->de_recvq_head == NULL) dep->de_recvq_head = rxptr; else dep->de_recvq_tail->next = rxptr; dep->de_recvq_tail = rxptr; unlock(); /* Reply to pending Receive requests, if any */ el1_recv(dep, TRUE, 0); } } else { /* Nasty condition, should never happen */ DEBUG( printf("3c501: got interrupt with status 0x%02X\n" " de_flags=0x%04X XSR=0x%02X RSR=0x%02X \n" " xmit buffer = 0x%4X recv buffer = 0x%4X\n", csr, dep->de_flags, inb_el1(dep, EL1_RECV), inb_el1(dep, EL1_XMIT), inw_el1(dep, EL1_XMITPTR), inw_el1(dep, EL1_RECVPTR)) ); el1_reset(dep); } /* Move into receive mode */ outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_RECV); outw_el1(dep, EL1_RECVPTR, 0); /* Be sure that interrupts are cleared */ inb_el1(dep, EL1_RECV); inb_el1(dep, EL1_XMIT); return; }
/* ** Name: void el1_send(dpeth_t *dep, int from_int, int pktsize) ** Function: Send function. Called from main to transit a packet or ** from interrupt handler when a new packet was queued. */ static void el1_send(dpeth_t * dep, int from_int, int pktsize) { buff_t *txbuff; clock_t now; if (from_int == FALSE) { if ((txbuff = alloc_buff(dep, pktsize + sizeof(buff_t))) != NULL) { /* Fill transmit buffer from user area */ txbuff->next = NULL; txbuff->size = pktsize; txbuff->client = dep->de_client; user2mem(dep, txbuff); } else panic("out of memory for Tx"); } else if ((txbuff = dep->de_xmitq_head) != NULL) { /* Get first packet in queue */ lock(); if (dep->de_xmitq_tail == dep->de_xmitq_head) dep->de_xmitq_head = dep->de_xmitq_tail = NULL; else dep->de_xmitq_head = txbuff->next; unlock(); pktsize = txbuff->size; } else panic("should not be sending "); if ((dep->de_flags & DEF_XMIT_BUSY)) { if (from_int) panic("should not be sending "); getticks(&now); if ((now - dep->de_xmit_start) > 4) { /* Transmitter timed out */ DEBUG(printf("3c501: transmitter timed out ... \n")); dep->de_stat.ets_sendErr += 1; dep->de_flags &= NOT(DEF_XMIT_BUSY); el1_reset(dep); } /* Queue packet */ lock(); /* Queue packet to receive queue */ if (dep->de_xmitq_head == NULL) dep->de_xmitq_head = txbuff; else dep->de_xmitq_tail->next = txbuff; dep->de_xmitq_tail = txbuff; unlock(); } else { /* Save for retransmission */ TxBuff = txbuff; dep->de_flags |= (DEF_XMIT_BUSY | DEF_ACK_SEND); /* Setup board for packet loading */ lock(); /* Buffer to processor */ outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_SYS); inb_el1(dep, EL1_RECV); /* Clears any spurious interrupt */ inb_el1(dep, EL1_XMIT); outw_el1(dep, EL1_RECVPTR, 0); /* Clears RX packet area */ /* Loads packet */ outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - pktsize)); outsb(dep->de_data_port, SELF, txbuff->buffer, pktsize); /* Starts transmitter */ outw_el1(dep, EL1_XMITPTR, (EL1_BFRSIZ - pktsize)); outb_el1(dep, EL1_CSR, ECSR_RIDE | ECSR_XMIT); /* There it goes... */ unlock(); getticks(&dep->de_xmit_start); dep->de_flags &= NOT(DEF_SENDING); } return; }
static char* build_direct_request(char *meth, struct url *url, char *headers, struct request *rq, int flags) { int rlen, authorization_done = FALSE; char *answer = NULL, *fav=NULL, *httpv, *host=NULL; struct buff *tmpbuff; struct av *av; int host_len=0; int via_inserted = FALSE; // if (!TEST(flags, DONT_CHANGE_HTTPVER) ) { // httpv = "HTTP/1.1"; // if ( TEST(rq->flags, RQ_HAS_HOST) ) // host = rq->url.host; // } else { httpv = url->httpv; if ( !TEST(rq->flags, RQ_HAS_HOST) ) host = rq->url.host; // } tmpbuff = alloc_buff(CHUNK_SIZE); if ( !tmpbuff ) return(NULL); rlen = strlen(meth) + 1/*sp*/ + strlen(url->path) + 1/*sp*/ + strlen(httpv) + 2/* \r\n */; if(TEST(rq->flags,RQ_USE_PROXY)) { rlen=rlen+7+strlen(rq->url.host); if(rq->url.port!=80) rlen=rlen+7; } answer = (char *)xmalloc(ROUND(rlen+1,CHUNK_SIZE), "build_direct_request(): 1"); /* here answer is actually *request* buffer */ if ( !answer ) goto fail; memset(answer,0,rlen+1); if(TEST(rq->flags,RQ_USE_PROXY)) { if(rq->url.port!=80) snprintf(answer,rlen,"%s http://%s:%d%s %s\r\n",meth,rq->url.host,rq->url.port,rq->url.path,httpv); else snprintf(answer,rlen,"%s http://%s%s %s\r\n",meth,rq->url.host,rq->url.path,httpv); } else { sprintf(answer, "%s %s %s\r\n", meth, rq->url.path, httpv); } // printf("host=%s.path=%s.\n",rq->url.host,rq->url.path); // printf("answer=%s.\n",answer); if ( attach_data(answer, strlen(answer), tmpbuff) ) goto fail; if ( headers ) { /* attach what was requested */ if ( attach_data(headers, strlen(headers), tmpbuff) ) goto fail; } host_len=strlen(rq->url.host)+10; host=(char *)malloc(host_len); memset(host,0,host_len); if(rq->url.port!=80) snprintf(host,host_len-1,"%s:%d",rq->url.host,rq->url.port); else snprintf(host,host_len-1,"%s",rq->url.host); if(fav=format_av_pair("Host:",host)) { if(attach_data(fav,strlen(fav),tmpbuff)) goto fail; xfree(fav); fav=NULL; } free(host); av = rq->av_pairs; while ( av ) { if ( is_attr(av, "Proxy-Connection:") ) goto do_not_insert; if ( is_attr(av, "Proxy-Authorization:") ) /* hop-by-hop */ goto do_not_insert; if ( is_attr(av, "Connection:") ) goto do_not_insert; if( is_attr(av,"Host:")) goto do_not_insert; if ( is_attr(av, "Via:") && conf.insert_via && !via_inserted ) { /* attach my Via: */ if ( (fav = format_av_pair(av->attr, av->val)) != 0 ) { char *buf; buf = strchr(fav, '\r'); if ( buf ) *buf = 0; if ( !buf ) { buf = strchr(fav, '\n'); if ( buf ) *buf = 0; } int buf_len=strlen(fav)+2+18+7+10+strlen(VER_ID)+2+1; buf =(char *)malloc(buf_len); if ( !buf ) goto fail; memset(buf,0,buf_len); snprintf(buf,buf_len-1,"%s,%d.%d %s:%d (kingate/%s)\r\n",fav,rq->http_major,rq->http_minor,rq->client->get_server_name() ,conf.port[HTTP], VER_ID); if ( attach_data(buf, strlen(buf), tmpbuff) ) { xfree(buf); goto fail; } via_inserted = TRUE; xfree(buf); xfree(fav); fav = NULL; } goto do_not_insert; } if ( (fav=format_av_pair(av->attr, av->val)) != 0 ) { if ( attach_data(fav, strlen(fav), tmpbuff) ) goto fail; xfree(fav); fav = NULL; } do_not_insert: av = av->next; } if ( (fav = format_av_pair("Connection:", "close")) != 0 ) { if ( attach_data(fav, strlen(fav), tmpbuff) ) goto fail; xfree(fav); fav = NULL; } if(conf.x_forwarded_for) { if( (fav=format_av_pair("X-Forwarded-For:",(char *)rq->server->get_client_name()))!=0) { if ( attach_data(fav, strlen(fav), tmpbuff) ) goto fail; xfree(fav); fav = NULL; } } if (conf.insert_via && !via_inserted ) { char *buf; int buf_len=4+1+18+7+10+strlen(VER_ID)+2+1; buf =(char *) malloc(buf_len); if ( !buf ) goto fail; memset(buf,0,buf_len); snprintf(buf,buf_len-1,"Via: %d.%d %s:%d (kingate/%s)\r\n",rq->http_major,rq->http_minor, rq->client->get_server_name(),conf.port[HTTP], VER_ID); if ( attach_data(buf, strlen(buf), tmpbuff) ) { xfree(buf); goto fail; } xfree(buf); xfree(fav); fav = NULL; } /* CRLF */ if ( attach_data("\r\n", 2, tmpbuff) ) goto fail; if ( attach_data("", 1, tmpbuff) ) goto fail; IF_FREE(answer); answer = tmpbuff->data; tmpbuff->data = NULL; free_chain(tmpbuff); //printf("%s",answer); return answer; fail: IF_FREE(fav); IF_FREE(host); if (tmpbuff) free_chain(tmpbuff); IF_FREE(answer); return NULL; }
int send_data_from_obj(struct request *rq, struct mem_obj *obj) { struct buff * tmp=NULL;//obj->container; struct av *header=obj->headers; struct buff *hdrs_to_send=alloc_buff(CHUNK_SIZE); /* most headers fit this (?) */ // rq->server->set_time(60); char tmp_str[100]; int start=0,send_len=obj->content_length; pthread_mutex_lock(&obj->lock); if(!swap_in_obj(obj) ) { pthread_mutex_unlock(&obj->lock); dead_obj(obj); goto error; } pthread_mutex_unlock(&obj->lock); tmp=obj->container; if(rq->range_to==-1) rq->range_to=obj->content_length-1; if ( TEST(rq->flags, RQ_HAVE_RANGE)) { if( // && (obj->state == OBJ_READY) // && !content_chunked(obj) ((rq->range_from >= 0 && rq->range_from<obj->content_length-1) && (rq->range_to == -1||rq->range_to>rq->range_from) )) { attach_av_pair_to_buff("HTTP/1.1","206 Partial Content",hdrs_to_send); memset(tmp_str,0,sizeof(tmp_str)); snprintf(tmp_str,sizeof(tmp_str)-1,"%d-%d/%d",rq->range_from,rq->range_to,obj->content_length); /* if(rq->range_to!=-1) snprintf(tmp_str+strlen(tmp_str),sizeof(tmp_str)-1-strlen(tmp_str),"%d",MIN(rq->range_to,obj->content_length-1)); snprintf(tmp_str+strlen(tmp_str),sizeof(tmp_str)-1-strlen(tmp_str),"/%d",obj->content_length); */ attach_av_pair_to_buff("Content-Range:",tmp_str,hdrs_to_send); header=header->next; start=rq->range_from; if(rq->range_to>=0) send_len=MIN(rq->range_to+1,obj->content_length)-rq->range_from; else send_len=obj->content_length-rq->range_from; } else if(rq->range_from>obj->content_length-1) { attach_av_pair_to_buff("HTTP/1.1","416 Requested Range Not Satisfiable",hdrs_to_send); memset(tmp_str,0,sizeof(tmp_str)); snprintf(tmp_str,sizeof(tmp_str)-1,"*/%d",obj->content_length); attach_av_pair_to_buff("Content-Range:",tmp_str,hdrs_to_send); start=-1; header=header->next; } } while(header) { // printf("attr=%s\n",header->attr); if(strncasecmp(header->attr,"Connection:",11)==0) { if(!TEST(rq->flags,RQ_HAS_KEEP_CONNECTION)) { attach_av_pair_to_buff("Connection:","close",hdrs_to_send); } else { attach_av_pair_to_buff("Connection:","keep-alive",hdrs_to_send); } goto next_head; } if(strncasecmp(header->attr,"Server:",7)==0) { memset(tmp_str,0,sizeof(tmp_str)); snprintf(tmp_str,sizeof(tmp_str)-1,"kingate/%s (cached)",VER_ID); attach_av_pair_to_buff("Server:",tmp_str,hdrs_to_send); goto next_head; } attach_av_pair_to_buff(header->attr, header->val, hdrs_to_send); next_head: header = header->next; } attach_av_pair_to_buff("", "", hdrs_to_send); if(rq->server->send(hdrs_to_send->data,hdrs_to_send->used)<=0) goto done; // printf("start=%d,send_len=%d.\n",start,send_len); if(start==-1) goto done; while(tmp!=NULL) { if(tmp->used<start) { start-=tmp->used; goto next_buffer; } if(send_len<=0) break; if(rq->server->send(tmp->data+start,MIN(tmp->used,send_len))<=0) goto error; start=0; send_len-=tmp->used; next_buffer: tmp=tmp->next; } done: if(hdrs_to_send) free_container(hdrs_to_send); return SEND_FROM_MEM_OK; error: if(hdrs_to_send) free_container(hdrs_to_send); return CONNECT_ERR; }
int check_server_headers(struct server_answ *a, struct mem_obj *obj, const char *b,int len, struct request *rq) { char *start, *beg, *end, *p; char holder, its_here = 0, off = 2; char *p1 = NULL; a->http_result=0; assert(a); assert(obj); assert(b); assert(rq); sscanf(b,"%*s %d",&a->http_result); // if ( !b || !b->data ) return(0); beg =(char *) b; end = (char *)b + len; if ( end - beg > MAX_DOC_HDR_SIZE ) { /* Header is too large */ return(1); } go: if ( a->state & GOT_HDR ) return(0); start = beg + a->checked; if ( !a->checked ) { p = (char *)memchr(beg, '\n', end-beg); holder = '\n'; if ( !p ) { p = (char *)memchr(beg, '\r', end-beg); holder = '\r'; } if ( !p ) return(0); if ( *p == '\n' ) { if ( *(p-1) == '\r' ) { p1 = p-1; *p1 = 0; } } *p = 0; a->checked = strlen(start); /* this is HTTP XXX yyy "header", which we will never rewrite */ analyze_header(start, a); if ( add_header_av(start, obj) ) { *p = holder; if ( p1 ) { *p1 = '\r'; p1=NULL; } return(-1); } *p = holder; if ( p1 ) { *p1 = '\r'; p1=NULL; } goto go; } if ( (end - start >= 2) && !memcmp(start, "\n\n", 2) ) { its_here = 1; off = 2; } if ( (end - start >= 3) && !memcmp(start, "\r\n\n", 3) ) { its_here = 1; off = 3; } if ( (end - start >= 3) && !memcmp(start, "\n\r\n", 3) ) { its_here = 1; off = 3; } if ( (end - start >= 4) && !memcmp(start, "\r\n\r\n", 4) ) { its_here = 1; off = 4; } if ( its_here ) { struct buff *body; int all_siz; obj->insertion_point = start-beg; obj->tail_length = off; a->state |= GOT_HDR ; obj->httpv_major = a->httpv_major; obj->httpv_minor = a->httpv_minor; obj->content_length = a->content_len; // b->used = ( start + off ) - beg; /* trunc first buf to header siz */ /* if requested don't cache documents without "Last-Modified" */ /* allocate data storage */ if ( a->content_len ) { if ( a->content_len > maxresident ) { /* - This object will not be stored, we will receive it in - small parts, in syncronous mode - allocate as much as we need now... */ all_siz = ROUND_CHUNKS(end-start-off); /* - mark object as 'not for store' and 'don't expand container' */ a->flags |= (ANSW_NO_STORE | ANSW_SHORT_CONTAINER); } else {/* obj is not too large */ all_siz = MIN(a->content_len, 8192); } } else { /* no Content-Len: */ char *transfer_encoding = NULL; all_siz = ROUND_CHUNKS(end-start-off); transfer_encoding = attr_value(obj->headers, "Transfer-Encoding"); if ((transfer_encoding && !strncasecmp("chunked", transfer_encoding, 7)) ) { a->flags |= (ANSW_NO_STORE | ANSW_SHORT_CONTAINER); } } body = alloc_buff(all_siz); if ( !body ) { return(-1); } // b->next = body; obj->container=obj->hot_buff = body; attach_data(start+off, end-start-off, obj->hot_buff); return(0); } p = start; while( (p < end) && ( *p == '\r' || *p == '\n' ) ) p++; if ( p < end && *p ) { char *t = (char *)memchr(p, '\n', end-p); char *tmp, *saved_tmp; holder = '\n'; if ( !t ) { t = (char *)memchr(p, '\r', end-p); holder = '\r'; } if ( !t ) return(0); if ( *t == '\n' ) { if ( *(t-1) == '\r' ) { p1 = t-1; *p1 = 0; } } *t = 0; saved_tmp = tmp = strdup(p); if ( !tmp ) return(-1); // do_redir_rewrite_header(&tmp, rq, NULL); analyze_header(tmp, a); if ( add_header_av(tmp, obj) ) { free(tmp); return(-1); } if ( saved_tmp != tmp ) { /* header was changed */ if ( obj ) SET(obj->flags, ANSW_HDR_CHANGED); } free(tmp); *t = holder; if ( p1 ) { *p1 = '\r'; t=p1; p1 = NULL; } a->checked = t - beg; goto go; } return(0); }
static int load_head(struct request *rq,struct mem_obj *obj,head_info &m_head) { //char *answer = NULL; struct server_answ answ_state; struct pollarg pollarg[2]; struct av *header = NULL; int r=0,downgrade_flags=0; char *answer= (char *)malloc(ANSW_SIZE+1); char *buf=answer; int len=ANSW_SIZE; int head_status=CONNECT_ERR; struct buff *hdrs_to_send=NULL; // rq->client->set_time(0); // rq->server->set_time(0); if ( !answer ) goto error; memset(&answ_state,0, sizeof(answ_state)); pollarg[0].fd = rq->client->get_socket(); pollarg[0].request = FD_POLL_RD; pollarg[1].fd = rq->server->get_socket(); pollarg[1].request = FD_POLL_RD; forever() { if((r = poll_descriptors(2, &pollarg[0], conf.time_out[HTTP]*1000))<=0) { klog(DEBUG_LOG,"poll descriptors is error in file %s line %d\n",__FILE__,__LINE__); goto error; } if(IS_READABLE(&pollarg[1]) || IS_HUPED(&pollarg[1])) { klog(DEBUG_LOG,"prev head value=%d,client close the connection\n",head_status); //head_status=CLIENT_HAVE_DATA; goto error; } if(IS_READABLE(&pollarg[0])) r = rq->client->recv_t(buf,len,0); else { klog(DEBUG_LOG,"unknow error is happen\n"); goto error; } if ( r <= 0 ) { klog(DEBUG_LOG,"recv server data error\n"); goto error; } // buf[r]=0; // printf("%s",buf); buf+=r; len-=r; if ( !(answ_state.state & GOT_HDR) ) { obj->size += r; if ( check_server_headers(&answ_state, obj, answer,r, rq) ) { head_status=HEAD_UNKNOW; rq->flags|=RESULT_CLOSE; // printf("%s:%d\n",__FILE__,__LINE__); goto send_data; } } if ( answ_state.state & GOT_HDR ) { obj->flags|= answ_state.flags; obj->times = answ_state.times; // printf("obj->times.last_modified=%d\n",obj->times.last_modified); if ( !obj->times.date ) obj->times.date = global_sec_timer; // check_new_object_expiration(rq, obj); obj->status_code = answ_state.status_code; if ( obj->status_code != STATUS_OK && obj->status_code!=STATUS_NOT_MODIFIED) { // printf("status is not ok\n"); // printf("obj->status_code=%d.\n",obj->status_code); rq->flags|=RESULT_CLOSE; head_status=HEAD_NOT_OK; goto send_data; } // obj->flags |= FLAG_DEAD; if (TEST(obj->flags , ANSW_NO_STORE) ) { //printf("answer is no cache\n"); head_status=HEAD_OK_NO_STORE; rq->flags|=RESULT_CLOSE; goto send_data; } // obj->flags |= ANSW_NO_CACHE; // downgrade_flags = downgrade(rq, obj); // my_xlog(OOPS_LOG_HTTP|OOPS_LOG_DBG, "fill_mem_obj(): Downgrade flags: %x\n", downgrade_flags); hdrs_to_send = alloc_buff(CHUNK_SIZE); /* most headers fit this (?) */ if ( !hdrs_to_send ) goto error; header = obj->headers; if ( !header ) { rq->flags|=RESULT_CLOSE; goto send_data ; } if ( TEST(obj->flags, ANSW_SHORT_CONTAINER) ) { //printf("answer is short contaner\n"); head_status=HEAD_OK_NO_STORE; rq->flags|=RESULT_CLOSE; goto send_data; } /* first must be "HTTP/1.x 200 ..." */ /* if ( TEST(downgrade_flags, DOWNGRADE_ANSWER) ) { attach_av_pair_to_buff("HTTP/1.0", header->val, hdrs_to_send); header = header->next; } */ while(header) { if(strncasecmp(header->attr,"Set-Cookie:",7)!=0) { //don't cache cookie if(strncasecmp(header->attr,"Connection:",11)==0) { if(!TEST(rq->flags,RQ_HAS_KEEP_CONNECTION)) { attach_av_pair_to_buff("Connection:","close",hdrs_to_send); } else { attach_av_pair_to_buff("Connection:","keep-alive",hdrs_to_send); } goto next_header; } } else { // printf("server has set-cookie\n"); head_status=HEAD_OK_NO_STORE; rq->flags|=RESULT_CLOSE; goto send_data; } attach_av_pair_to_buff(header->attr, header->val, hdrs_to_send); next_header: header = header->next; } attach_av_pair_to_buff("", "", hdrs_to_send); // obj->container=hdrs_to_send; // hdrs_to_send->next=obj->hot_buff; if(obj->status_code==STATUS_NOT_MODIFIED) { head_status=HEAD_NOT_MODIFIED; m_head.head=hdrs_to_send->data; m_head.len=hdrs_to_send->used; hdrs_to_send->data=NULL; // free_container(hdrs_to_send); goto done; } // obj->container=obj->hot_buff; if(rq->server->send(hdrs_to_send->data,hdrs_to_send->used)<0) { // free_container(hdrs_to_send); goto error; } // free_container(hdrs_to_send); // printf("%s\n",hdrs_to_send->data); head_status=HEAD_OK; goto done; } if(len<1) { // printf("head size is too large\n"); head_status=HEAD_UNKNOW; rq->flags|=RESULT_CLOSE; goto send_data;//head size is too large } } error://get head failed obj->flags |= FLAG_DEAD; if(hdrs_to_send) free_container(hdrs_to_send); IF_FREE(answer); return head_status; done://get head ok; if(hdrs_to_send) free_container(hdrs_to_send); free(answer); return head_status; send_data://得到头失败,但要转发数据 if(hdrs_to_send) free_container(hdrs_to_send); m_head.head=answer; m_head.len=ANSW_SIZE-len; answer[m_head.len]=0; return head_status; }
int gather_results(int *result[]) { int fd = 0; /* file descriptior */ int minOne = 0; /* minimum method 1 */ int numTests = 0; /* number of tests in file */ int sampNum = 0; /* ammount of samples */ int prevVal = 0; /* previous sample values */ int curVal = 0; /* current sample value */ int bfPl = 0; /* place from inBuff */ int i = 0; int j = 0; ssize_t retBytes = 0; /* byte value returned from a read or write */ char inBuff[IN_BUF_] = {'\0'}; /* input buffer */ char numStr[TM_BUF_] = {'\0'}; /* String to be converted to an int from buf */ fd = open("smallpractice", O_RDWR); if(fd == -1){ errExit("gather_results: Opening file failed");} /* fill the input buffer */ alloc_buff(inBuff); /* get number of tests */ getBufLine(inBuff, numStr, bfPl); numTests = getInt(numStr, 0, "numTests"); result[0] = (int*) malloc(sizeof(int)*numTests); result[1] = (int*) malloc(sizeof(int)*numTests); /* allocate results index 0 and 1. */ for(i = 0; i < numTests; ++i) { minOne = 0; /*minTwo = 0;*/ /* get input from #of samples */ getBufLine(inBuff, numStr, bfPl); sampNum = getInt(numStr, 0, "sampNum"); /* pull first sample from file */ getBufLine(inBuff, numStr, bfPl); prevVal = getInt(numStr, 0, "prevVal"); --sampNum; /* prevVal holds the first sample */ for(j = 0; j < sampNum; ++j) { getBufLine(inBuff, numStr, bfPl); curVal = getInt(numStr, 0, "curVal"); /* get the mins */ minOne += method_one(prevVal, curVal); /* minTwo += method_two() */ prevVal = curVal; } if(sampNum == 0) { minOne = prevVal; /*results[1][i] = prevVal;*/ } /* place result in correct place */ result[0][i] = minOne; /* result[1][i] = minTwo; */ } if(close(fd) == -1){ errExit("gather_results: close() failure");} return 1; }