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; }
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"); } }
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; }