int call_callback(struct fd_state *state, const char *path, char *sparams, char *scookies) { assert(state && path); struct callback_elem *elem = callback_head; while (elem) { if (strcmp(path, elem->path) == 0) { struct lh_response response; response.header[0] = 0; response.header_len = 0; response.body[0] = 0; response.body_len = 0; struct lh_kv_elem *params = parse_url_params(sparams); struct lh_kv_elem *cookies = parse_cookies(scookies); elem->callback(params, cookies, &response); free_kvs(params); free_kvs(cookies); write_response_header(state, "application/json", response.body_len, response.header); if (response.header_len > 0) write_to_buf(state, response.header, response.header_len); if (response.body_len > 0) write_to_buf(state, response.body, response.body_len); return 1; } elem = elem->next; } return 0; }
static void process_altavista_cookie(const char *cookie, char **new_cookie) { CookieNodeType *cl; ustring ucookie = USTRING_INITIALIZER; *new_cookie = NULL; cl = parse_cookies(cookie); if(!cl) { nag("Error parsing cookies\n"); goto cleanup; } delete_cookie(&cl, "AV_ALL"); if(set_cookie_value(&cl, " AV_PG", "1")) { nag("Error setting new cookie value\n"); goto cleanup; } if(rebuild_cookie(cl, &ucookie)) { nag("Error rebuilding cookie\n"); goto cleanup; } *new_cookie = Strdup(ucookie.string); cleanup: delete_cookie_list(cl); reset_ustring(&ucookie); }
void init_req_header(req_header * req, char * header){ int len = strlen(header); req->body = http_parse_body(header, len); req->method = http_parse_method((char *) header); req->uri = http_parse_uri(header); req->path = http_parse_path(http_parse_uri(header)); cookie * cookies = parse_cookies(header); req->cookies = cookies; req->connection = http_parse_header_field(header, len, "Connection"); }
bool request::prepare() { char const *query=cgetenv("QUERY_STRING"); if(!parse_form_urlencoded(query,query + strlen(query),get_)) { get_.clear(); } parse_cookies(); content_type_ = cppcms::http::content_type(conn_->cgetenv("CONTENT_TYPE")); return true; }
void init_req_header(req_header * req, char * header){ char * cookie_header = malloc(strlen(header)); strcpy(cookie_header, header); int len = strlen(header); req->body = http_parse_body(header, len); req->method = http_parse_method((char *) header); req->uri = http_parse_uri(header); req->path = http_parse_path(http_parse_uri(header)); req->connection = http_parse_header_field( header, len, "Connection"); req->cache_control = http_parse_header_field(header, len, "Cache-Control"); cookie * cookies = parse_cookies(cookie_header, &(req->num_cookies)); req->cookies = cookies; }
void TLib_Cgi_Init(void) { if (iTLib_Cgi_Initialized != 0) { return; } list_create(&stTLib_Cgi_Entries); list_create(&stTLib_Cookie_Entries); iTLib_Cgi_Initialized = 1; read_cgi_input(&stTLib_Cgi_Entries); parse_cookies(&stTLib_Cookie_Entries); }
static void process_msn_cookie(const char *cookie, char **new_cookie) { CookieNodeType *cl; char *cookie_val; char *new_cookie_val = NULL; char *new2_cookie_val = NULL; cl = parse_cookies(cookie); if(!cl) { nag("Error parsing cookies\n"); } else { cookie_val = get_cookie_value(cl, "SRCHHPGUSR"); if(!cookie_val) { nag("No SRCHHPUSR cookie, cookie is %s\n", cookie); } else { new_cookie_val = find_and_replace(cookie_val, "&ADLT=OFF&", "&ADLT=STRICT&"); if(!new_cookie_val) { nag("Error replacing string\n"); } else { new2_cookie_val = find_and_replace(new_cookie_val, "&ADLT=DEMOTE&", "&ADLT=STRICT&"); Free(new_cookie_val); if(!new2_cookie_val) { nag("Error replacing string\n"); } else { if(set_cookie_value(&cl, "SRCHHPGUSR", new2_cookie_val)) { nag("Error setting new cookie value\n"); } else { ustring ucookie = USTRING_INITIALIZER; if(rebuild_cookie(cl, &ucookie)) { nag("Error rebuilding cookie\n"); } else { *new_cookie = Strdup(ucookie.string); } reset_ustring(&ucookie); } Free(new2_cookie_val); } } } } delete_cookie_list(cl); }
static void gaym_weblogin_step4(GaimUtilFetchUrlData *url_data, gpointer data, const gchar *text, gsize len, const gchar* err) { GaimUrlSession *session = (GaimUrlSession *) data; parse_cookies(text, session, len); gaim_debug_misc("gaym", "Step 4: session: %x\n", session); if (session && GAIM_CONNECTION_IS_VALID(session->account->gc)) { // The fourth step is to parse a rand=# value out of the message // text from // The previous step. // We then connect to messenger/applet.html char url[512]; int nonce; //char *buf = g_strdup_printf(_("Signon: %s"), // (session->account->username)); //gaim_connection_update_progress(session->account->gc, buf, 3, 6); sscanf(text, "?rand=%d", &nonce); snprintf(url, 512, "http://www.gay.com/messenger/applet.html?rand=%d", nonce); session->hasFormData = TRUE; gaim_debug_misc("weblogin","About to build url\n"); gchar* request=gaym_build_session_request(url, session); gaim_debug_misc("weblogin","Requesting: %s\n",request); gaim_util_fetch_url_request(url, FALSE, NULL, TRUE, request, TRUE, gaym_weblogin_step5, session); gaim_debug_misc("weblogin","applet fetched"); } else { gaim_debug_misc("gaym", "Connection was cancelled before step4\n"); gaim_debug_misc("gaym", "session: %x\n", session); gaym_session_destroy(session); // g_free(gaym->session); } }
static void process_alltheweb_cookie(const char *cookie, char **new_cookie) { CookieNodeType *cl; char *cookie_val; char *new_cookie_val = NULL; *new_cookie = NULL; cl = parse_cookies(cookie); if(!cl) { nag("Error parsing cookies\n"); } else { cookie_val = get_cookie_value(cl, "PREF"); if(!cookie_val) { nag("No PREF cookie, cookie is %s\n", cookie); } else { new_cookie_val = find_and_replace(cookie_val, ":no=off", ""); if(!new_cookie_val) { nag("Error replacing string\n"); } else { if(set_cookie_value(&cl, "PREF", new_cookie_val)) { nag("Error setting new cookie value\n"); } else { ustring ucookie = USTRING_INITIALIZER; if(rebuild_cookie(cl, &ucookie)) { nag("Error rebuilding cookie\n"); } else { *new_cookie = Strdup(ucookie.string); } reset_ustring(&ucookie); } Free(new_cookie_val); } } } delete_cookie_list(cl); }
// This is a modified version of gaim's url_fetched_cb function. It has // been // modified to pass cookies during requests. The cookies are set in // user_data, // cast as a GaimUrlSession item. Any cookies in the response are added to // // // // // this // structure, as well. static void session_fetched_cb(gpointer url_data, gint sock, GaimInputCondition cond) { GaimFetchUrlData *gfud = url_data; char data; gboolean got_eof = FALSE; if (sock == -1) { gfud->callback(gfud->user_data, NULL, 0); destroy_fetch_url_data(gfud); return; } if (!gfud->sentreq) { char buf[2048]; if (gfud->session->cookies == NULL) gfud->session->cookies = g_strdup(""); if (gfud->user_agent) { /* Host header is not forbidden in HTTP/1.0 requests, and HTTP/1.1 clients must know how to handle the "chunked" transfer encoding. Gaim doesn't know how to handle "chunked", so should always send the Host header regardless, to get around some observed problems */ g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/%s\r\n" "User-Agent: %s\r\n" "Host: %s\r\n" "Cookie: %s\r\n", // (1) // // // // // (see // above) (gfud->full ? "" : "/"), (gfud->full ? gfud->url : gfud->website.page), (gfud->http11 ? "1.1" : "1.0"), gfud->user_agent, gfud->website.address, gfud->session->cookies); } else { g_snprintf(buf, sizeof(buf), "GET %s%s HTTP/%s\r\n" "Host: %s\r\n" "Accept-Encoding: identity\r\n" "Cookie: %s\r\n", // (1) // // // // // See // above (gfud->full ? "" : "/"), (gfud->full ? gfud->url : gfud->website.page), (gfud->http11 ? "1.1" : "1.0"), gfud->website.address, gfud->session->cookies); } if (gfud->session->hasFormData) strcat(buf, "Content-Type: application/x-www-form-urlencoded\r\n\r\n"); else strcat(buf, "\r\n"); gaim_debug_misc("gaim_url_fetch", "Request: %s\n", buf); write(sock, buf, strlen(buf)); fcntl(sock, F_SETFL, O_NONBLOCK); gfud->sentreq = TRUE; gfud->inpa = gaim_input_add(sock, GAIM_INPUT_READ, session_fetched_cb, url_data); gfud->data_len = 4096; gfud->webdata = g_malloc(gfud->data_len); return; } if (read(sock, &data, 1) > 0 || errno == EWOULDBLOCK) { if (errno == EWOULDBLOCK) { errno = 0; return; } gfud->len++; if (gfud->len == gfud->data_len + 1) { gfud->data_len += (gfud->data_len) / 2; gfud->webdata = g_realloc(gfud->webdata, gfud->data_len); } gfud->webdata[gfud->len - 1] = data; if (!gfud->startsaving) { if (data == '\r') return; if (data == '\n') { if (gfud->newline) { size_t content_len; gfud->startsaving = TRUE; gaim_debug_misc("gaim_url_fetch", "Response headers: '%*.*s'\n", gfud->len, gfud->len, gfud->webdata); // JBL 10-16-2004: Put cookies into session gaim_debug(GAIM_DEBUG_MISC, "gaym", "Parsing cookies..."); parse_cookies(gfud->webdata, gfud->session, gfud->len); gaim_debug(GAIM_DEBUG_MISC, "gaym", "Found cookies: %s\n", (gfud->session->cookies)); gaim_debug_misc("gaim_url_fetch", "Parsing of cookies successful\n"); /* See if we can find a redirect. */ if (parse_redirect (gfud->webdata, gfud->len, sock, gfud)) return; /* No redirect. See if we can find a content length. */ content_len = parse_content_len(gfud->webdata, gfud->len); if (content_len == 0) { /* We'll stick with an initial 8192 */ content_len = 8192; } else { gfud->has_explicit_data_len = TRUE; } /* Out with the old... */ gfud->len = 0; g_free(gfud->webdata); gfud->webdata = NULL; /* In with the new. */ gfud->data_len = content_len; gfud->webdata = g_try_malloc(gfud->data_len); if (gfud->webdata == NULL) { gaim_debug_error("gaim_url_fetch", "Failed to allocate %u bytes: %s\n", gfud->data_len, strerror(errno)); gaim_input_remove(gfud->inpa); close(sock); gfud->callback(gfud->user_data, NULL, 0); destroy_fetch_url_data(gfud); } } else gfud->newline = TRUE; return; } gfud->newline = FALSE; } else if (gfud->has_explicit_data_len && gfud->len == gfud->data_len) { got_eof = TRUE; } } else if (errno != ETIMEDOUT) { got_eof = TRUE; } else { gaim_input_remove(gfud->inpa); close(sock); gfud->callback(gfud->user_data, NULL, 0); destroy_fetch_url_data(gfud); } if (got_eof) { gfud->webdata = g_realloc(gfud->webdata, gfud->len + 1); gfud->webdata[gfud->len] = 0; /* gaim_debug_misc("gaim_url_fetch", "Received: '%s'\n", gfud->webdata); */ gaim_input_remove(gfud->inpa); close(sock); gfud->callback(gfud->user_data, gfud->webdata, gfud->len); destroy_fetch_url_data(gfud); } }
static void *ast_httpd_helper_thread(void *data) { char buf[4096]; char cookie[4096]; char timebuf[256]; struct ast_http_server_instance *ser = data; struct ast_variable *vars = NULL; char *uri, *c, *title=NULL; int status = 200, contentlength = 0; time_t t; unsigned int static_content = 0; if (fgets(buf, sizeof(buf), ser->f)) { /* Skip method */ uri = buf; while(*uri && (*uri > 32)) uri++; if (*uri) { *uri = '\0'; uri++; } /* Skip white space */ while (*uri && (*uri < 33)) uri++; if (*uri) { c = uri; while (*c && (*c > 32)) c++; if (*c) { *c = '\0'; } } while (fgets(cookie, sizeof(cookie), ser->f)) { /* Trim trailing characters */ while(!ast_strlen_zero(cookie) && (cookie[strlen(cookie) - 1] < 33)) { cookie[strlen(cookie) - 1] = '\0'; } if (ast_strlen_zero(cookie)) break; if (!strncasecmp(cookie, "Cookie: ", 8)) { vars = parse_cookies(cookie); } } if (*uri) { if (!strcasecmp(buf, "get")) c = handle_uri(&ser->requestor, uri, &status, &title, &contentlength, &vars, &static_content); else c = ast_http_error(501, "Not Implemented", NULL, "Attempt to use unimplemented / unsupported method");\ } else c = ast_http_error(400, "Bad Request", NULL, "Invalid Request"); /* If they aren't mopped up already, clean up the cookies */ if (vars) ast_variables_destroy(vars); if (!c) c = ast_http_error(500, "Internal Error", NULL, "Internal Server Error"); if (c) { time(&t); strftime(timebuf, sizeof(timebuf), "%a, %d %b %Y %H:%M:%S GMT", gmtime(&t)); ast_cli(ser->fd, "HTTP/1.1 %d %s\r\n", status, title ? title : "OK"); ast_cli(ser->fd, "Server: Asterisk/%s\r\n", ASTERISK_VERSION); ast_cli(ser->fd, "Date: %s\r\n", timebuf); ast_cli(ser->fd, "Connection: close\r\n"); if (!static_content) ast_cli(ser->fd, "Cache-Control: no-cache, no-store\r\n"); /* We set the no-cache headers only for dynamic content. * If you want to make sure the static file you requested is not from cache, * append a random variable to your GET request. Ex: 'something.html?r=109987734' */ if (contentlength) { char *tmp; tmp = strstr(c, "\r\n\r\n"); if (tmp) { ast_cli(ser->fd, "Content-length: %d\r\n", contentlength); if (write(ser->fd, c, (tmp + 4 - c)) < 0) { ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); } if (write(ser->fd, tmp + 4, contentlength) < 0) { ast_log(LOG_WARNING, "write() failed: %s\n", strerror(errno)); } } } else ast_cli(ser->fd, "%s", c); free(c); } if (title) free(title); } fclose(ser->f); free(ser); ast_atomic_fetchadd_int(&session_count, -1); return NULL; }
parse(server::request const& request) : headers(parse_headers(request)), cookies(parse_cookies(this->headers)), parameters(parse_parameters(request)), session_id(parse_session_id(this->cookies)) {}
request::request(const int id, const std::string& r) { m_error = 0; m_error_text = ""; m_method = ""; m_path = ""; m_host = ""; m_identifier = ""; m_if_modified_since = 0; m_port = 80; m_request_lines = sammy::utils::split_string(r, '\n'); if(m_request_lines.size() == 0) { m_error = 1; m_error_text = "Request data empty."; return; } parse_header(); for(unsigned int i = 1; i < m_request_lines.size(); ++i) { // Find the line type of the request, to route it to the correct parse function const auto& current_line = m_request_lines.at(i); const auto colon_pos = current_line.find(": "); if(colon_pos == std::string::npos) { continue; } const std::string line_type = current_line.substr(0, colon_pos); const std::string line_data = current_line.substr(colon_pos + 2); if(line_type == "Host") { parse_host(line_data); continue; } if(line_type == "Cookie") { parse_cookies(line_data); continue; } if(line_type == "Referer") { parse_referer(line_data); continue; } if(line_type == "If-Modified-Since") { parse_if_modified_since(line_data); continue; } } parse_post_data(); if(m_method.size() == 0 || m_path.size() == 0 || m_host.size() == 0) { m_error = 1; m_error_text = "Method, path or host empty."; return; } m_identifier = sammy::utils::sha256(std::to_string(id)); }