Ejemplo n.º 1
0
int main(int argc, char** argv) {

    char *eth, *ip1, *ip2;

    char ip[IP_LENGTH];
    char filename[FILENAME_LENGTH];
    char buffer[CLOSE_READ_BUFFER_SIZE];
 
    if (argc < 2) {
        printf("No url found\nUsage: %s url\n", argv[0]);
        return 1;
    }

    eth = getenv("ETH");
    if (!eth) {
        fprintf(stdout, "The ETH environment variable must be set!\n");
        return 1;
    }

    ip1 = getenv("IP1");
    ip2 = getenv("IP2");
    if (!ip1 || !ip2) {
        fprintf(stdout, "The IP1 and IP2 environment variables must be set!\n");
        return 1;
    }

    if (tcp_socket() != 0) {
        fprintf(stdout, "HTTPC: Opening socket failed\n");
        return 1;
    }

    /* parse url into ip and filename */
    if (!parse_url(argv[1], ip, IP_LENGTH, filename, FILENAME_LENGTH)) {
        printf("Invalid url\n");
        return 1;
    }

    /* connect to http server */
    if (tcp_connect(inet_aton(ip), SERVER_PORT)) {
        printf("Request failed: could not connect to server\n");
        return 0;
    }

    /* do request */
    if (!do_request(ip, filename)) {
        tcp_close();
        return 1;
    }

    /* handle response */
    if (!handle_response(ip, filename)) {
        tcp_close();
        return 1;
    }

    /* close connection */
    if (tcp_close() != 0) {
        printf("Closing connection failed\n");
        return 1;
    }

    signal(SIGALRM, alarm_handler);
    alarm(TIME_OUT);
    while (tcp_read(buffer, RESPONSE_BUFFER_SIZE) > 0) {}
    alarm(0);

    return 0;

}
Ejemplo n.º 2
0
/*
 * do_local_work
 *
 * Any processing errors are placed in the 'rq'
 * structure to be reported back to the kernel.
 * It may be pointless for this function to
 * return an int.
 *
 * Returns: 0 on success, -EXXX on failure
 */
static int do_local_work(void *data)
{
	int r;
	struct clog_request *rq;
	struct dm_ulog_request *u_rq = NULL;

	r = kernel_recv(&rq);
	if (r)
		return r;

	if (!rq)
		return 0;

	u_rq = &rq->u_rq;
	LOG_DBG("[%s]  Request from kernel received: [%s/%u]",
		SHORT_UUID(u_rq->uuid), RQ_TYPE(u_rq->request_type),
		u_rq->seq);
	switch (u_rq->request_type) {
	case DM_ULOG_CTR:
	case DM_ULOG_DTR:
	case DM_ULOG_GET_REGION_SIZE:
	case DM_ULOG_IN_SYNC:
	case DM_ULOG_GET_SYNC_COUNT:
	case DM_ULOG_STATUS_INFO:
	case DM_ULOG_STATUS_TABLE:
	case DM_ULOG_PRESUSPEND:
		/* We do not specify ourselves as server here */
		r = do_request(rq, 0);
		if (r)
			LOG_DBG("Returning failed request to kernel [%s]",
				RQ_TYPE(u_rq->request_type));
		r = kernel_send(u_rq);
		if (r)
			LOG_ERROR("Failed to respond to kernel [%s]",
				  RQ_TYPE(u_rq->request_type));
			
		break;
	case DM_ULOG_RESUME:
		/*
		 * Resume is a special case that requires a local
		 * component to join the CPG, and a cluster component
		 * to handle the request.
		 */
		r = local_resume(u_rq);
		if (r) {
			LOG_DBG("Returning failed request to kernel [%s]",
				RQ_TYPE(u_rq->request_type));
			r = kernel_send(u_rq);
			if (r)
				LOG_ERROR("Failed to respond to kernel [%s]",
					  RQ_TYPE(u_rq->request_type));
			break;
		}
		/* ELSE, fall through */
	case DM_ULOG_IS_CLEAN:
	case DM_ULOG_FLUSH:
	case DM_ULOG_MARK_REGION:
	case DM_ULOG_GET_RESYNC_WORK:
	case DM_ULOG_SET_REGION_SYNC:
	case DM_ULOG_IS_REMOTE_RECOVERING:
	case DM_ULOG_POSTSUSPEND:
		r = cluster_send(rq);
		if (r) {
			u_rq->data_size = 0;
			u_rq->error = r;
			kernel_send(u_rq);
		}

		break;
	case DM_ULOG_CLEAR_REGION:
		r = kernel_ack(u_rq->seq, 0);

		r = cluster_send(rq);
		if (r) {
			/*
			 * FIXME: store error for delivery on flush
			 *        This would allow us to optimize MARK_REGION
			 *        too.
			 */
		}

		break;
	default:
		LOG_ERROR("Invalid log request received (%u), ignoring.",
			  u_rq->request_type);

		return 0;
	}

	if (r && !u_rq->error)
		u_rq->error = r;

	return r;
}
Ejemplo n.º 3
0
void SocksClient::processOutgoing(const QByteArray &block)
{
#ifdef PROX_DEBUG
    // show hex
    fprintf(stderr, "SocksClient: client recv { ");
    for(int n = 0; n < (int)block.size(); ++n)
        fprintf(stderr, "%02X ", (unsigned char)block[n]);
    fprintf(stderr, " } \n");
#endif
    d->recvBuf += block;

    if(d->step == StepVersion) {
        SPSS_VERSION s;
        int r = sps_get_version(d->recvBuf, &s);
        if(r == -1) {
            resetConnection(true);
            setError(ErrProxyNeg);
            return;
        }
        else if(r == 1) {
            if(s.version != 0x05 || s.method == 0xff) {
#ifdef PROX_DEBUG
                fprintf(stderr, "SocksClient: Method selection failed\n");
#endif
                resetConnection(true);
                setError(ErrProxyNeg);
                return;
            }

            QString str;
            if(s.method == 0x00) {
                str = "None";
                d->authMethod = AuthNone;
            }
            else if(s.method == 0x02) {
                str = "Username/Password";
                d->authMethod = AuthUsername;
            }
            else {
#ifdef PROX_DEBUG
                fprintf(stderr, "SocksClient: Server wants to use unknown method '%02x'\n", s.method);
#endif
                resetConnection(true);
                setError(ErrProxyNeg);
                return;
            }

            if(d->authMethod == AuthNone) {
                // no auth, go straight to the request
                do_request();
            }
            else if(d->authMethod == AuthUsername) {
                d->step = StepAuth;
#ifdef PROX_DEBUG
                fprintf(stderr, "SocksClient: Authenticating [Username] ...\n");
#endif
                writeData(spc_set_authUsername(d->user.toLatin1(), d->pass.toLatin1()));
            }
        }
    }
    if(d->step == StepAuth) {
        if(d->authMethod == AuthUsername) {
            SPSS_AUTHUSERNAME s;
            int r = sps_get_authUsername(d->recvBuf, &s);
            if(r == -1) {
                resetConnection(true);
                setError(ErrProxyNeg);
                return;
            }
            else if(r == 1) {
                if(s.version != 0x01) {
                    resetConnection(true);
                    setError(ErrProxyNeg);
                    return;
                }
                if(!s.success) {
                    resetConnection(true);
                    setError(ErrProxyAuth);
                    return;
                }

                do_request();
            }
        }
    }
    else if(d->step == StepRequest) {
        SPS_CONNREQ s;
        int r = sp_get_request(d->recvBuf, &s);
        if(r == -1) {
            resetConnection(true);
            setError(ErrProxyNeg);
            return;
        }
        else if(r == 1) {
            if(s.cmd != RET_SUCCESS) {
#ifdef PROX_DEBUG
                fprintf(stderr, "SocksClient: client << Error >> [%02x]\n", s.cmd);
#endif
                resetConnection(true);
                if(s.cmd == RET_UNREACHABLE)
                    setError(ErrHostNotFound);
                else if(s.cmd == RET_CONNREFUSED)
                    setError(ErrConnectionRefused);
                else
                    setError(ErrProxyNeg);
                return;
            }

#ifdef PROX_DEBUG
            fprintf(stderr, "SocksClient: client << Success >>\n");
#endif
            if(d->udp) {
                if(s.address_type == 0x03)
                    d->udpAddr = s.host;
                else
                    d->udpAddr = s.addr.toString();
                d->udpPort = s.port;
            }

            setOpenMode(QIODevice::ReadWrite);

            QPointer<QObject> self = this;
            setOpenMode(QIODevice::ReadWrite);
            emit connected();
            if(!self)
                return;

            if(!d->recvBuf.isEmpty()) {
                appendRead(d->recvBuf);
                d->recvBuf.resize(0);
                readyRead();
            }
        }
    }
}
Ejemplo n.º 4
0
int main(int argc, char* argv[])
{
	int i;
	char c;
	int flag = 0;
	if (!(ptr_auxMem = fopen(AUXILIARY_MEMORY, "r+")))
	{
		do_error(ERROR_FILE_OPEN_FAILED);
		exit(1);
	}
	do_init();
	do_print_info();
	ptr_memAccReq = (Ptr_MemoryAccessRequest) malloc(sizeof(MemoryAccessRequest));
	/* 在循环中模拟访存请求与处理过程 */
	while (TRUE)
	{
loop4:		do_request();
loop:	do_response();
		printf("按Y打印页表,按其他键不打印.........\n");
loop1:	if ((c = getchar()) == 'y' || c == 'Y')
			do_print_info();
		while (c != '\n')
			c = getchar();
		if(flag == 1){
			flag = 0;
			goto loop1;
		}
loop2:		printf("按X退出程序,按I手动输入请求,按P打印实存,按R打印辅存,按T转换模式,按其他键继续...\n");
		if ((c = getchar()) == 'x' || c == 'X')
			break;
		if (c == 'i' || c == 'I'){
			flag = do_input_request();
			if(flag == 1)
				goto loop;
		}
		if (c == 'p' || c == 'P'){
			printf("实存\t对应值\t实存\t对应值\t实存\t对应值\t实存\t对应值\n");
			for(i = 0; i < ACTUAL_MEMORY_SIZE; i++){
				printf("%d \t%02X\t", i, actMem[i]);
				if(i % 4 == 3)
					printf("\n");
			}	
			goto loop1;
		}
		if (c == 'r' || c == 'R'){
			ptr_auxMem = fopen(AUXILIARY_MEMORY, "r+");
			while(fscanf(ptr_auxMem, "%c", &c) != EOF)
				printf("%c",c);
			printf("\n");
			goto loop1;		
		}
		if (c == 't' || c == 'T'){
			goto loop3;		
		}
		while (c != '\n'){
			c = getchar();
		}
		
	}
	
	/* 在循环中模拟访存请求与处理过程 */
loop3:	while (TRUE)
	{
		umask(0);
		printf("进入读写FIFO模式\n");
		//do_request();
		FILE *fp;
		char *buf[100];
		mkfifo(FIFO,S_IFIFO|0666);
		/*if (mkfifo(FIFO,S_IFIFO|0666) < 0){
			printf("what the f**k\n");
			continue;
		}*/	
		fp=fopen(FIFO,"r");
		fgets(buf,sizeof(buf),fp);
		
		deal_request(buf);
		do_response();
		printf("按Y打印页表,按其他键不打印...\n");
		if ((c = getchar()) == 'y' || c == 'Y')
			do_print_info();
		while (c != '\n')
			c = getchar();
		printf("按X退出程序,按T转换模式,按其他键继续...\n");
		if ((c = getchar()) == 'x' || c == 'X')
			break;
		if(c == 't' || c == 'T'){
			flag = 1;
			printf("退出读写FIFO模式\n");
			goto loop4;		
		}
		while (c != '\n')
			c = getchar();
		//sleep(5000);
	}

	if (fclose(ptr_auxMem) == EOF)
	{
		do_error(ERROR_FILE_CLOSE_FAILED);
		exit(1);
	}
	return (0);
}
Ejemplo n.º 5
0
static int ft245r_paged_write_flash(PROGRAMMER * pgm, AVRPART * p, AVRMEM * m,
                                    int page_size, int addr, int n_bytes) {
    unsigned int    i,j;
    int addr_save,buf_pos,do_page_write,req_count;
    unsigned char buf[FT245R_FRAGMENT_SIZE+1+128];

    req_count = 0;
    for (i=0; i<n_bytes; ) {
        addr_save = addr;
        buf_pos = 0;
        do_page_write = 0;
        for (j=0; j< FT245R_FRAGMENT_SIZE/8/FT245R_CYCLES/4; j++) {
            buf_pos += set_data(pgm, buf+buf_pos, (addr & 1)?0x48:0x40 );
            buf_pos += set_data(pgm, buf+buf_pos, (addr >> 9) & 0xff );
            buf_pos += set_data(pgm, buf+buf_pos, (addr >> 1) & 0xff );
            buf_pos += set_data(pgm, buf+buf_pos, m->buf[addr]);
            addr ++;
            i++;
            if ( (m->paged) &&
                    (((i % m->page_size) == 0) || (i == n_bytes))) {
                do_page_write = 1;
                break;
            }
        }
#if defined(USE_INLINE_WRITE_PAGE)
        if (do_page_write) {
            int addr_wk = addr_save - (addr_save % m->page_size);
            /* If this device has a "load extended address" command, issue it. */
            if (m->op[AVR_OP_LOAD_EXT_ADDR]) {
                unsigned char cmd[4];
                OPCODE *lext = m->op[AVR_OP_LOAD_EXT_ADDR];

                memset(cmd, 0, 4);
                avr_set_bits(lext, cmd);
                avr_set_addr(lext, cmd, addr_wk/2);
                buf_pos += set_data(pgm, buf+buf_pos, cmd[0]);
                buf_pos += set_data(pgm, buf+buf_pos, cmd[1]);
                buf_pos += set_data(pgm, buf+buf_pos, cmd[2]);
                buf_pos += set_data(pgm, buf+buf_pos, cmd[3]);
            }
            buf_pos += set_data(pgm, buf+buf_pos, 0x4C); /* Issue Page Write */
            buf_pos += set_data(pgm, buf+buf_pos,(addr_wk >> 9) & 0xff);
            buf_pos += set_data(pgm, buf+buf_pos,(addr_wk >> 1) & 0xff);
            buf_pos += set_data(pgm, buf+buf_pos, 0);
        }
#endif
        if (i >= n_bytes) {
            ft245r_out = SET_BITS_0(ft245r_out,pgm,PIN_AVR_SCK,0); // sck down
            buf[buf_pos++] = ft245r_out;
        }
        ft245r_send(pgm, buf, buf_pos);
        put_request(addr_save, buf_pos, 0);
        //ft245r_sync(pgm);
#if 0
        avrdude_message(MSG_INFO, "send addr 0x%04x bufsize %d [%02x %02x] page_write %d\n",
                addr_save,buf_pos,
                extract_data_out(pgm, buf , (0*4 + 3) ),
                extract_data_out(pgm, buf , (1*4 + 3) ),
                do_page_write);
#endif
        req_count++;
        if (req_count > REQ_OUTSTANDINGS)
            do_request(pgm, m);
        if (do_page_write) {
#if defined(USE_INLINE_WRITE_PAGE)
            while (do_request(pgm, m))
                ;
            usleep(m->max_write_delay);
#else
            int addr_wk = addr_save - (addr_save % m->page_size);
            int rc;
            while (do_request(pgm, m))
                ;
            rc = avr_write_page(pgm, p, m, addr_wk);
            if (rc != 0) {
                return -2;
            }
#endif
            req_count = 0;
        }
    }
    while (do_request(pgm, m))
        ;
    return i;
}
Ejemplo n.º 6
0
bool
BNetworkDevice::Exists() const
{
	ifreq request;
	return do_request(request, Name(), SIOCGIFINDEX) == B_OK;
}
Ejemplo n.º 7
0
static void do_read(EpmdVars *g,Connection *s)
{
  int val, pack_size;

  if (s->open == EPMD_FALSE)
    {
      dbg_printf(g,0,"read on unknown socket");
      return;
    }

  /* Check if we already got the whole packet but we keep the
     connection alive to find out when a node is terminated. We then
     want to check for a close */

  if (s->keep == EPMD_TRUE)
    {
      val = read(s->fd, s->buf, INBUF_SIZE);

      if (val == 0)
	{
	  node_unreg_sock(g,s->fd);
	  epmd_conn_close(g,s);
	}
      else if (val < 0)
	{
	    dbg_tty_printf(g,1,"error on ALIVE socket %d (%d; errno=0x%x)",
			   s->fd, val, errno);
	  node_unreg_sock(g,s->fd);
	  epmd_conn_close(g,s);
	}
      else
	{
	  dbg_tty_printf(g,1,"got more than expected on ALIVE socket %d (%d)",
		 s->fd,val);
	  dbg_print_buf(g,s->buf,val);

	  node_unreg_sock(g,s->fd);
	  epmd_conn_close(g,s);
	}
      return;
    }

  /* If unknown size we request the whole buffer - what we got - 1
     We subtract 1 because we will add a "\0" in "do_request()".
     This is not needed for R3A or higher versions of Erlang,
     because the '\0' is included in the request,
     but is kept for backwards compatibility to allow R2D to use
     this epmd. */

  pack_size = s->want ? s->want : INBUF_SIZE - 1;
  val = read(s->fd, s->buf + s->got, pack_size - s->got);

  if (val == 0)
    {
      /* A close when we haven't got all data */
      dbg_printf(g,0,"got partial packet only on file descriptor %d (%d)",
		 s->fd,s->got);
      epmd_conn_close(g,s);
      return;
    }

  if (val < 0)
    {
      dbg_perror(g,"error in read");
      epmd_conn_close(g,s);
      return;
    }

  dbg_print_buf(g,s->buf,val);

  s->got += val;

  if ((s->want == 0) && (s->got >= 2))
    {
      /* The two byte header that specify the length of the packet
	 doesn't count the header as part of the packet so we add 2
	 to "s->want" to make us talk about all bytes we get. */

      s->want = get_int16(s->buf) + 2;

      if ((s->want < 3) || (s->want >= INBUF_SIZE))
	{
	  dbg_printf(g,0,"invalid packet size (%d)",s->want - 2);
	  epmd_conn_close(g,s);
	  return;
	}

      if (s->got > s->want)
	{
	  dbg_printf(g,0,"got %d bytes in packet, expected %d",
		     s->got - 2, s->want - 2);
	  epmd_conn_close(g,s);
	  return;
	}
    }
  
  s->mod_time = current_time(g); /* Note activity */
  
  if (s->want == s->got)
    {
      /* Do action and close up */
      /* Skip header bytes */

      do_request(g, s->fd, s, s->buf + 2, s->got - 2);

      if (!s->keep)
	epmd_conn_close(g,s);		/* Normal close */
    }
}
Ejemplo n.º 8
0
int main(int argc, char **args)
{
    int httpd,client;
	struct sockaddr_in client_addr;
	socklen_t len = sizeof(client_addr);
	int epfd,nfds, i, fd, n, nread;
    struct epoll_event ev,events[MAX_CONNECT];
    char buf[2048];
    int ret = -1;
    
    signal(SIGPIPE, SIG_IGN);
    //set_log_file("./log.txt");
    
    ret = chroot("web");
    if (ret)
    {
        perror("chroot err");
		//return -1;    
    }
    
    on_exit(httpd_destory, (void *)&httpd);
    bzero(&client_addr,sizeof client_addr);
    	
	if (0 > (httpd = mnet_tcp_server(HTTP_PORT)))
	{
		printf("start up server err\n");
		return -1;
	}
       
    epfd = epoll_create(MAX_CONNECT);
    if (epfd == -1)
    {
        printf("err when epoll create\n");
        return -1;
    }
   
    ev.data.fd = httpd;
    ev.events = EPOLLIN|EPOLLET;//监听读状态同时设置ET模式
    epoll_ctl(epfd, EPOLL_CTL_ADD, httpd, &ev);//注册epoll事件
    while(1)
    {
        nfds = epoll_wait(epfd, events, MAX_CONNECT, -1);
        if (nfds == -1)
        {
            printf("failed when epoll wait\n");
            continue;
        }
                
        for (i = 0; i < nfds; i++)
        {
            fd = events[i].data.fd;
            if (fd == httpd)
            {
                while ((client = accept(httpd, (struct sockaddr *) &client_addr, (size_t *)&len)) > 0) 
                {
                    if (mnet_setnoblock(client))
                    {
                        mlog_err("failed when set client nonlock");
                        continue;
                    }
                    
                    ev.events = EPOLLIN | EPOLLET;
                    ev.data.fd = client;
                    
                    if (epoll_ctl(epfd, EPOLL_CTL_ADD, client, &ev) == -1)
                    {
                        perror("epoll add client err");
                        continue;
                    }
                    
                    mlog_info("accept fd : %d", client);
                }
                
                if (errno != EAGAIN && errno != ECONNABORTED 
                                && errno != EPROTO && errno != EINTR) 
                                    perror("accept");
                
                continue;
            }
            
            if (events[i].events & EPOLLIN) 
            {                            
                n = 0;
                while ((nread = read(fd, buf + n, BUFSIZ-1)) > 0) //ET下可以读就一直读
                {
                    n += nread;
                }
                
                if (n == 0)
                {
                    mlog_err("client: %d request over", fd);
                    close(fd);
                    continue;
                }
                
                buf[n] = 0;        
                if (nread == -1 && errno != EAGAIN) 
                {
                    perror("read error");
                }
                        
                mlog_info("read fd : %d, req: %s", fd, buf);

                //recv_dump(fd);              
                
                    #if 0    
                        ev.data.fd = fd;
                        ev.events = events[i].events | EPOLLOUT; //MOD OUT 
                        if (epoll_ctl(epfd, EPOLL_CTL_MOD, fd, &ev) == -1) {
                                perror("epoll_ctl: mod");
                        }
                   #else
                       do_request(request_parse(buf, len), fd); 
                   #endif     
            }          
        }
    }
    
fail_label:
	close(httpd);
	return 0;
}
Ejemplo n.º 9
0
Archivo: gweb.c Proyecto: rzr/connman
guint g_web_request_post(GWeb *web, const char *url,
				const char *type, GWebInputFunc input,
				GWebResultFunc func, gpointer user_data)
{
	return do_request(web, url, type, input, -1, 0, func, NULL, user_data);
}
Ejemplo n.º 10
0
Archivo: gweb.c Proyecto: rzr/connman
guint g_web_request_get(GWeb *web, const char *url, GWebResultFunc func,
		GWebRouteFunc route, gpointer user_data)
{
	return do_request(web, url, NULL, NULL, -1, 0, func, route, user_data);
}
Ejemplo n.º 11
0
static void
do_basics_test (SoupURI *base_uri)
{
	SoupSession *session;
	SoupCache *cache;
	char *cache_dir;
	char *body1, *body2, *body3, *body4, *body5, *cmp;

	debug_printf (1, "Cache basics\n");

	cache_dir = g_dir_make_tmp ("cache-test-XXXXXX", NULL);
	debug_printf (2, "  Caching to %s\n", cache_dir);
	cache = soup_cache_new (cache_dir, SOUP_CACHE_SINGLE_USER);
	session = soup_test_session_new (SOUP_TYPE_SESSION_ASYNC,
					 SOUP_SESSION_USE_THREAD_CONTEXT, TRUE,
					 SOUP_SESSION_ADD_FEATURE, cache,
					 NULL);
	g_signal_connect (session, "request-started",
			  G_CALLBACK (request_started), NULL);

	debug_printf (2, "  Initial requests\n");
	body1 = do_request (session, base_uri, "GET", "/1",
			    "Test-Set-Expires", "Fri, 01 Jan 2100 00:00:00 GMT",
			    NULL);
	body2 = do_request (session, base_uri, "GET", "/2",
			    "Test-Set-Last-Modified", "Fri, 01 Jan 2010 00:00:00 GMT",
			    NULL);
	body3 = do_request (session, base_uri, "GET", "/3",
			    "Test-Set-Last-Modified", "Fri, 01 Jan 2010 00:00:00 GMT",
			    "Test-Set-Cache-Control", "must-revalidate",
			    NULL);
	body4 = do_request (session, base_uri, "GET", "/4",
			    "Test-Set-ETag", "\"abcdefg\"",
			    "Test-Set-Cache-Control", "must-revalidate",
			    NULL);
	body5 = do_request (session, base_uri, "GET", "/5",
			    "Test-Set-Cache-Control", "no-cache",
			    NULL);


	/* Resource with future Expires should have been cached */
	debug_printf (1, "  Fresh cached resource\n");
	cmp = do_request (session, base_uri, "GET", "/1",
			  NULL);
	if (last_request_hit_network) {
		debug_printf (1, "    Request for /1 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body1, cmp) != 0) {
		debug_printf (1, "    Cached response (%s) did not match original (%s)\n",
			      cmp, body1);
		errors++;
	}
	g_free (cmp);


	/* Resource with long-ago Last-Modified should have been cached */
	debug_printf (1, "  Heuristically-fresh cached resource\n");
	cmp = do_request (session, base_uri, "GET", "/2",
			  NULL);
	if (last_request_hit_network) {
		debug_printf (1, "    Request for /2 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body2, cmp) != 0) {
		debug_printf (1, "    Cached response (%s) did not match original (%s)\n",
			      cmp, body2);
		errors++;
	}
	g_free (cmp);


	/* Adding a query string should bypass the cache but not invalidate it */
	debug_printf (1, "  Fresh cached resource with a query\n");
	cmp = do_request (session, base_uri, "GET", "/1?attr=value",
			  NULL);
	if (!last_request_hit_network) {
		debug_printf (1, "    Request for /1?attr=value filled from cache!\n");
		errors++;
	}
	g_free (cmp);
	debug_printf (2, "  Second request\n");
	cmp = do_request (session, base_uri, "GET", "/1",
			  NULL);
	if (last_request_hit_network) {
		debug_printf (1, "    Second request for /1 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body1, cmp) != 0) {
		debug_printf (1, "    Cached response (%s) did not match original (%s)\n",
			      cmp, body1);
		errors++;
	}
	g_free (cmp);


	/* Last-Modified + must-revalidate causes a conditional request */
	debug_printf (1, "  Unchanged must-revalidate resource w/ Last-Modified\n");
	cmp = do_request (session, base_uri, "GET", "/3",
			  "Test-Set-Last-Modified", "Fri, 01 Jan 2010 00:00:00 GMT",
			  "Test-Set-Cache-Control", "must-revalidate",
			  NULL);
	if (!last_request_validated) {
		debug_printf (1, "    Request for /3 not validated!\n");
		errors++;
	}
	if (last_request_hit_network) {
		debug_printf (1, "    Request for /3 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body3, cmp) != 0) {
		debug_printf (1, "    Cached response (%s) did not match original (%s)\n",
			      cmp, body3);
		errors++;
	}
	g_free (cmp);


	/* Validation failure should update cache */
	debug_printf (1, "  Changed must-revalidate resource w/ Last-Modified\n");
	cmp = do_request (session, base_uri, "GET", "/3",
			  "Test-Set-Last-Modified", "Sat, 02 Jan 2010 00:00:00 GMT",
			  "Test-Set-Cache-Control", "must-revalidate",
			  NULL);
	if (!last_request_validated) {
		debug_printf (1, "    Request for /3 not validated!\n");
		errors++;
	}
	if (!last_request_hit_network) {
		debug_printf (1, "    Request for /3 filled from cache!\n");
		errors++;
	}
	if (strcmp (body3, cmp) == 0) {
		debug_printf (1, "    Request for /3 returned cached response\n");
		errors++;
	}
	g_free (cmp);

#if 0 /* This doesn't work... is the test wrong or is SoupCache? */
	debug_printf (2, "  Second request\n");
	cmp = do_request (session, base_uri, "GET", "/3",
			  "Test-Set-Last-Modified", "Sat, 02 Jan 2010 00:00:00 GMT",
			  "Test-Set-Cache-Control", "must-revalidate",
			  NULL);
	if (!last_request_validated) {
		debug_printf (1, "    Second request for /3 not validated!\n");
		errors++;
	}
	if (last_request_hit_network) {
		debug_printf (1, "    Second request for /3 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body3, cmp) == 0) {
		debug_printf (1, "    Replacement body for /3 not cached!\n");
		errors++;
	}
	g_free (cmp);
#endif


	/* ETag + must-revalidate causes a conditional request */
	debug_printf (1, "  Unchanged must-revalidate resource w/ ETag\n");
	cmp = do_request (session, base_uri, "GET", "/4",
			  "Test-Set-ETag", "\"abcdefg\"",
			  NULL);
	if (!last_request_validated) {
		debug_printf (1, "    Request for /4 not validated!\n");
		errors++;
	}
	if (last_request_hit_network) {
		debug_printf (1, "    Request for /4 not filled from cache!\n");
		errors++;
	}
	if (strcmp (body4, cmp) != 0) {
		debug_printf (1, "    Cached response (%s) did not match original (%s)\n",
			      cmp, body4);
		errors++;
	}
	g_free (cmp);


	/* Cache-Control: no-cache prevents caching */
	debug_printf (1, "  Uncacheable resource\n");
	cmp = do_request (session, base_uri, "GET", "/5",
			  "Test-Set-Cache-Control", "no-cache",
			  NULL);
	if (!last_request_hit_network) {
		debug_printf (1, "    Request for /5 filled from cache!\n");
		errors++;
	}
	if (strcmp (body5, cmp) != 0) {
		debug_printf (1, "    Re-retrieved response (%s) did not match original (%s)\n",
			      cmp, body5);
		errors++;
	}
	g_free (cmp);


	/* PUT to a URI invalidates the cache entry */
	debug_printf (1, "  Invalidating and re-requesting a cached resource\n");
	cmp = do_request (session, base_uri, "PUT", "/1",
			  NULL);
	if (!last_request_hit_network) {
		debug_printf (1, "    PUT filled from cache!\n");
		errors++;
	}
	g_free (cmp);
	cmp = do_request (session, base_uri, "GET", "/1",
			  NULL);
	if (!last_request_hit_network) {
		debug_printf (1, "    PUT failed to invalidate cache entry!\n");
		errors++;
	}
	g_free (cmp);


	soup_test_session_abort_unref (session);
	g_object_unref (cache);

	g_free (cache_dir);
	g_free (body1);
	g_free (body2);
	g_free (body3);
	g_free (body4);
	g_free (body5);
}
Ejemplo n.º 12
0
/* 产生访存请求 */
int readrequest(Ptr_MemoryAccessRequest ptr_memAccReq)
{
	printf("请输入访存请求(输入x结束程序):\n");
	char c;
	unsigned long addr;
	BYTE value;
	int i,num;
	c=getchar();//读取请求类型
	//printf("#c的值是%c#\n",c);
	if(c!='s'&&c!='x'&&c!='y'){//如果c不是s或x则读取请求地址
		scanf(" %lu",&addr);//读取请求地址
		ptr_memAccReq->command=0;//不是命令	
	}
	switch(c){
		case 'r':
			ptr_memAccReq->reqType = REQUEST_READ;
			ptr_memAccReq->virAddr = addr;
			scanf(" %u",&(ptr_memAccReq->pid));
			printf("产生请求:\n地址:%lu\t类型:读取\tPID:%u\n", ptr_memAccReq->virAddr,ptr_memAccReq->pid);
            break;
        case 'w':
			ptr_memAccReq->reqType = REQUEST_WRITE;
			ptr_memAccReq->virAddr = addr;
            scanf(" %c",&value);//读入要写入的值
            ptr_memAccReq->value = value;
            scanf(" %u",&(ptr_memAccReq->pid));
            printf("产生请求:\n地址:%lu\t类型:写入\t值:%02X\tPID:%u\n", ptr_memAccReq->virAddr, ptr_memAccReq->value,ptr_memAccReq->pid);
            break;
        case 'e':
        	ptr_memAccReq->reqType = REQUEST_EXECUTE;
        	ptr_memAccReq->virAddr = addr;
        	scanf(" %u",&(ptr_memAccReq->pid));
            printf("产生请求:\n地址:%lu\t类型:执行\tPID:%u\n", ptr_memAccReq->virAddr,ptr_memAccReq->pid);
            break;
        case 's'://随机产生请求
        	ptr_memAccReq->command=0;//不是命令
        	scanf(" %d",&num);
        	printf("将产生%d条随机请求\n",num);
        	for(i=1;i<=num;i++){
        		do_request(ptr_memAccReq);
        		writereq(ptr_memAccReq);	
        	}
        	printf("%d条随机请求产生完毕\n",num);
        	break;
        case 'x':
        	ptr_memAccReq->command=-2;
        	printf("#发送退出程序命令#");
        	break;
        case 'y'://发送y命令
        	ptr_memAccReq->command=-1;
        	printf("#发送打印页表命令#");
        	break;
        default:
        	printf("请求格式有误,请重新输入\n");
    }
    if(c=='x'||c=='y'){//将命令写入fifo文件
		writereq(ptr_memAccReq);
	}
 	if(c=='r'||c=='w'||c=='e'){//将单条请求写入fifo文件
    	writereq(ptr_memAccReq);
    }
    if(c=='x')
    	return -1;//退出本程序
    while(c=getchar()!='\n')//越过行尾回车
    	;
    return 0;
    
}