static bool http_request_init(http_request_t *req) { url_t * url = url_parse(req->url); char * uri = url_get_uri(url); req->uri = uri ? strdup(uri) : strdup("/"); if(uri) { free(uri); } http_conn_t * conn = http_conn_new(url->host, url->port ? url->port : 80); assert(conn != NULL); if(!conn) { return false; } req->conn = conn; url_free(url); sstring_init(&req->header, 200); sstring_init(&req->res_header, 512); sstring_init(&req->response, 2048); req->ht_headers = hash_table_new(20, free); req->error = NULL; return true; }
bool http_request_preform(http_request_t * req) { sstring_t ss; sstring_init(&ss, 100); char buffer[200] = {0}; build_request_header(req, &ss); if (!http_conn_connect(req->conn)) { req->error = req->conn->error; return false; } // trigger STATE_OPENED. change_state(req, STATE_OPENED); //printf("request header: %s len:%d\n", pss->ptr, pss->len); int n = write(req->conn->connfd, ss.ptr, ss.len); //fetch header and stored in hash table. http_request_fetch_header(req); if(req->method != METHOD_HEAD) { change_state(req, STATE_LOADING); size_t complete = 0; size_t total = 0; char * content_length = http_request_get_header(req, "Content-Length"); if(content_length) { total = atoi(content_length); } if(req->handlers.on_loadstart) { req->handlers.on_loadstart(req->handlers.loadstart_data); } if(req->handlers.on_progress) { req->handlers.on_progress(complete, total ,req->handlers.progress_data); } while((n = read(req->conn->connfd, buffer, 100))) { if(n < 0 ) { //error handle there break; }else { sstring_appendl(&req->response, buffer, n); complete += n; if(req->handlers.on_progress) { req->handlers.on_progress(complete, total, req->handlers.progress_data); } } } if(req->handlers.on_load) { req->handlers.on_load(buffer, req->handlers.load_data); } } change_state(req, STATE_DONE); sstring_destroy(&ss); return true; }
static int _parse_url(shttp_connection_internal_t* conn) { struct http_parser_url parse_url; int rc; cstring_t data; data.str = conn_incomming(conn).url.str; data.len = conn_incomming(conn).url.len; rc = http_parser_parse_url(conn_incomming(conn).url.str, conn_incomming(conn).url.len, (HTTP_CONNECT == conn->parser.method)? 1:0, &parse_url); if(0 != rc) { ERR("parse_url: style error\n"); return -1; } #define http_uri conn_request(conn).uri sstring_init(http_uri.full, data.str, data.len); sstring_init(http_uri.schema, data.str + parse_url.field_data[UF_SCHEMA].off, parse_url.field_data[UF_SCHEMA].len); sstring_init(http_uri.host, data.str + parse_url.field_data[UF_HOST].off, parse_url.field_data[UF_HOST].len); sstring_init(http_uri.port_s, data.str + parse_url.field_data[UF_PORT].off, parse_url.field_data[UF_PORT].len); http_uri.port_i = parse_url.port; sstring_init(http_uri.path, data.str + parse_url.field_data[UF_PATH].off, parse_url.field_data[UF_PATH].len); sstring_init(http_uri.query, data.str + parse_url.field_data[UF_QUERY].off, parse_url.field_data[UF_QUERY].len); sstring_init(http_uri.fragment, data.str + parse_url.field_data[UF_FRAGMENT].off, parse_url.field_data[UF_FRAGMENT].len); sstring_init(http_uri.user_info, data.str + parse_url.field_data[UF_USERINFO].off, parse_url.field_data[UF_USERINFO].len); return 0; //return incomming->callbacks->on_message_begin(incomming->conn, // incomming->parser.http_major, // incomming->parser.http_minor, // incomming->parser.method, // &uri); }
int leapsecs_read(void) { char fbuf[1024]; struct sstring sstr; struct stat sb; struct tai tai; struct tai *mem = 0; unsigned long size; unsigned long ind; int fd = -1; /* build path to leapsecs.dat */ sstring_init(&sstr, fbuf, sizeof (fbuf)); sstring_cats(&sstr, leapsecs_dir); if (sstr.len + sizeof("/leapsecs.dat") >= 1024) { errno = error_nametoolong; return -1; } sstring_cats(&sstr, "/leapsecs.dat"); sstring_0(&sstr); /* open leapsecs.dat */ fd = open_ro(sstr.s); if (fd == -1) { if (errno != error_noent) return -1; leapsecs_free(); return 0; } if (fstat(fd, &sb) == -1) goto FAIL; /* map and unpack packed tai structures */ mem = alloc(sb.st_size); if (!mem) goto FAIL; size = read(fd, mem, sb.st_size); if (size != (unsigned long) sb.st_size) { dealloc(mem); goto FAIL; } size = sb.st_size / sizeof(struct tai); for (ind = 0; ind < size; ++ind) { tai_unpack((unsigned char *) &mem[ind], &tai); mem[ind] = tai; } leapsecs_free(); leapsecs_list = (struct tai *) mem; leapsecs_size = size; if (fd != -1) close(fd); return 0; FAIL: if (fd != -1) close(fd); return -1; }
char * url_get_uri(url_t * url) { sstring_t ss ; sstring_init(&ss, 100); if(url->path) { sstring_append(&ss, url->path); } if(url->query) { sstring_fappend(&ss, "?%s", url->query); } if(url->fragment) { sstring_fappend(&ss, "#%s", url->fragment); } return ss.ptr; }
void dir_walk_init(struct dir_walk *dw) { bin_zero(dw, sizeof(struct dir_walk)); sstring_init(&dw->path, dw->pbuf, sizeof(dw->pbuf)); }