Пример #1
0
int http_parse_request(int fd, struct HttpRequest* request){
  char buf[MAX_LINE];
  int line_length;

  request->client_fd = fd;
  
  rio_t rio;
  rio_init(&rio, fd);

  //TODO: need to check result, try to telnet and input nothing
  line_length = rio_readline(&rio, buf, MAX_LINE);

  printf("%s", buf);
  
  char format[16];
  sprintf(format, "%%%lus %%%lus %%%lus", sizeof(request->method) - 1, sizeof(request->uri) - 1, sizeof(request->version) - 1);
  sscanf(buf, format, request->method, request->uri, request->version);

  while((line_length = rio_readline(&rio, buf, MAX_LINE)) > 0){
    printf(" %s", buf);
    if(strncmp(buf, "\r\n", line_length) == 0){
      break;
    }
    if(strncmp(buf, "\n", line_length) == 0){
      printf("got unexpected new line\n");
      break;
    }
  }

  return 0;
}
Пример #2
0
void serve_static(int fd, char *filename, int filesize)
{
    int srcfd;
    char filetype[MAXLINE], buf[MAXLINE], *body;


    /* 发送 Response headers 到客户端 */
    get_file_type(filename, filetype);
    sprintf(buf, "HTTP/1.0 200 OK \r\n");
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Server: toyws\r\n");
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Content-type: %s\r\n", filetype);
    rio_writen(fd, buf, strlen(buf));
    sprintf(buf, "Content-length: %d\r\n\r\n", filesize);
    rio_writen(fd, buf, strlen(buf));

    /* 发送Response body */
    srcfd = open(filename, 'r');

    body = (char *)malloc(filesize);
    memset(body, filesize, 0);
    /* 读取文件内容到body */
    while (rio_readline(srcfd, buf, MAXLINE)) {
        sprintf(body, "%s%s",body, buf);
    }    
    close(srcfd);
    rio_writen(fd, body, filesize);
    free(body);
}
Пример #3
0
void read_request_headers(int fd)
{
    char buf[MAXLINE];

    while(strcmp(buf,"\r\n")) {
        rio_readline(fd, buf, MAXLINE);
        //printf("%s", buf);
    }
    return;
}
Пример #4
0
Файл: io.c Проект: Lw-Cui/proxy
int main(int argc, char **argv) {
	int fd;
	char buf[MAX];

	if (argc > 1) {
		fd = open("foo.txt", O_RDONLY, 0);
		while (rio_readline(fd, buf, MAX))
			printf("%s", buf);
	}
    return 0;
}
Пример #5
0
int read_post_data(int fd)
{
    char buf[MAXLINE];
    char key[MAXLINE], value[MAXLINE];
    int i,split;
    u_int64_t content_length = 0;    
    
    while(strcmp(buf,"\r\n")) {
        split = -1;
        bzero(key, sizeof(key));
        bzero(value, sizeof(value));
        rio_readline(fd, buf, MAXLINE);

        if (!strcmp(buf,"\r\n")) {
            break;
        }
        
        for (i=0; i < strlen(buf); i++) {
            if (buf[i] == ' '){
                continue;    
            }
            else if (buf[i] == ':'){
                split = i;
                break;
            }            
        }
        
        if (split) {
            strncpy(key, buf, split);
            strncpy(value, buf+split+1, strlen(buf) - split-1);
            printf("Key : %s \t Value : %s \n", key, value);

            /* Content-Type: application/x-www-form-urlencoded */
            if (!strncasecmp(key,"Content-Type",MAXLINE)){
#ifdef DEBUG
                printf("Content-Type in header\n");
#endif
            }
            /* Content-Length */
            else if (!strncasecmp(key,"Content-Length",MAXLINE)){
#ifdef DEBUG
                printf("Content-Length in header\n");
                content_length = atol(value);
                printf("Content-Length:%d\n");
#endif
            }
            
        }
    }
    //rio_readline(fd, buf, MAXLINE);
    //printf("request body: %s\n", buf);

    return 1;
}
Пример #6
0
int main()
{
    int n;
    rio_t rio;
    char res[10000];
    rio_init(&rio, STDIN_FILENO, 5);
    rio.rio_ptr = (char *) malloc(sizeof(char) * 5);

    //n = rio_readline(&rio, res, 20);
    //printf("get %d", n);

    while((n = rio_readline(&rio, res, 20))!= 0)
    {
        sendn(STDOUT_FILENO, res, n);
        printf("\n");
    }
}
Пример #7
0
int main(int argc, char const *argv[])
{
	int fd = open("test.txt", O_RDONLY);
	if (fd == -1)
		ERR_EXIT("open test.txt");
	rio_t rio;
	rio_init(&rio, fd);

	char buf[1024] = "0";

	while (rio_readline(&rio, buf, sizeof buf)) {
		printf("%s", buf);
	}

	close(fd);

	return 0;
}
Пример #8
0
void on_read(struct ev_loop *loop, struct ev_io *watcher, int revents) 
{
    struct ev_io *w_request = (struct ev_io *) malloc(sizeof(struct ev_io));
    int fd = watcher-> fd;
    char buf[MAXLINE];
    Request request;
    
    /*读取 Rquest Line 和Headers*/
    rio_readline(fd, buf, MAXLINE);
    
    RequestLine *request_line = &(request.request_line);
    //printf("%s", buf);
    sscanf(buf, "%s %s %s\n", request_line->method, request_line->uri, request_line->version);

#ifdef DEBUG
    printf("uri: %s\t method: %s\n", request_line->uri, request_line->method);
#endif
    ev_io_stop(loop, watcher);
    ev_io_init(&request.io, on_write, fd, EV_WRITE);
    ev_io_start(loop, &request.io);
}