PTR_WEIBO_ENTITY show_single_weibo_byid(const char* access_token, const char* weibo_id) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "ss", access_token, weibo_id); char s[20] = {}; cJSON* root = NULL; PTR_WEIBO_ENTITY weibo = NULL; PTR_HTTP_REQUEST request = alloc_http_request(2, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "access_token"; request->params[0].value = access_token; request->params[1].name = "id"; request->params[1].value = weibo_id; response = https_get(WEIBO_SHOW_WEIBO_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return NULL; } weibo = create_weibo_entity_from_json(root); free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return weibo; }
PTR_WEIBO_ENTITY show_multiple_weibo_byids(const char* access_token, char** weibo_ids, uint32_t count) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "spd", access_token, weibo_ids, count); char s[2048] = {}; uint32_t i = 0; uint32_t cnt = count>MAX_BATCH_SIZE?MAX_BATCH_SIZE:count; cJSON* root = NULL; cJSON* statuses = NULL; PTR_WEIBO_ENTITY list_head = NULL; PTR_WEIBO_ENTITY weibo = NULL; PTR_HTTP_REQUEST request = alloc_http_request(2, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; memset(s, 0, sizeof(char)*sizeof(s)); sprintf(s, "%s", weibo_ids[0]); for (i=1; i<cnt; i++) { sprintf(s, "%s,%s", s, weibo_ids[i]); } s[strlen(s)] = '\0'; request->params[0].name = "access_token"; request->params[0].value = access_token; request->params[1].name = "ids"; request->params[1].value = s; response = https_get(WEIBO_SHOW_WEIBO_BATCH_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return NULL; } statuses = cJSON_GetObjectItem(root, "statuses"); cnt = cJSON_GetArraySize(statuses); for (i=0; i<cnt; i++) { weibo = create_weibo_entity_from_json(cJSON_GetArrayItem(statuses, i)); weibo->next = list_head; weibo->prev = NULL; if (list_head != NULL) { list_head->prev = weibo; } list_head = weibo; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return list_head; }
char* fetch_access_token(const char* code, char* token) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "sp", code, token); cJSON* root = NULL; PTR_HTTP_REQUEST request = alloc_http_request(5, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "client_id"; request->params[0].value = APP_KEY; request->params[1].name = "client_secret"; request->params[1].value = APP_SECRET; request->params[2].name = "grant_type"; request->params[2].value = "authorization_code"; request->params[3].name = "code"; request->params[3].value = code; request->params[4].name = "redirect_uri"; request->params[4].value = APP_AUTH_REDIRECT_URL; response = https_post(APP_FETCH_TOKEN_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); sprintf(token, "%s", cJSON_GetObjectItem(root, "access_token")->valuestring); free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return token; }
BOOL get_access_token_info(const char* access_token) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "s", access_token); cJSON* root = NULL; PTR_HTTP_REQUEST request = alloc_http_request(1, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "access_token"; request->params[0].value = access_token; response = https_post(APP_GET_TOKEN_INFO_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return False; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return False; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return True; }
PTR_WEIBO_ENTITY get_user_timeline(const char* access_token, const char* uid, int page) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "sd", access_token, page); char s[20] = {}; int i=0, cnt = 0; cJSON* root = NULL; cJSON* statuses = NULL; PTR_WEIBO_ENTITY list_head = NULL; PTR_WEIBO_ENTITY weibo = NULL; PTR_HTTP_REQUEST request = alloc_http_request(3, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "access_token"; request->params[0].value = access_token; request->params[1].name = "page"; snprintf(s, 20, "%d", page); request->params[1].value = s; request->params[2].name = "uid"; request->params[2].value = uid; response = https_get(WEIBO_GET_USER_TIMELINE_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return NULL; } statuses = cJSON_GetObjectItem(root, "statuses"); cnt = cJSON_GetArraySize(statuses); for (i=0; i<cnt; i++) { weibo = create_weibo_entity_from_json(cJSON_GetArrayItem(statuses, i)); weibo->next = list_head; weibo->prev = NULL; if (list_head != NULL) { list_head->prev = weibo; } list_head = weibo; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return list_head; }
PTR_WEIBO_ENTITY get_user_timeline_byname(const char* access_token, const char* name, int page) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "ssd", access_token, name, page); char s[20] = {}; char *weibo_id = NULL; int i = 0, cnt = 0; cJSON* root = NULL; PTR_WEIBO_ENTITY list_head = NULL; PTR_WEIBO_ENTITY weibo = NULL; PTR_HTTP_REQUEST request = alloc_http_request(4, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "access_token"; request->params[0].value = access_token; request->params[1].name = "name"; request->params[1].value = name; snprintf(s, 20, "%d", page); request->params[2].name="page"; request->params[2].value=s; request->params[3].name="count"; request->params[3].value="5"; /* hard code as 5 for api invocation limit */ response = https_get(WEIBO_GET_USER_TIMELINE_IDS_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return NULL; } cnt = cJSON_GetArraySize(cJSON_GetObjectItem(root, "statuses")); for (i=0; i<cnt; i++) { weibo_id = (cJSON_GetArrayItem(cJSON_GetObjectItem(root, "statuses"), i))->valuestring; weibo = show_single_weibo_byid(access_token, weibo_id); weibo->next = list_head; weibo->prev = NULL; if (list_head != NULL) { list_head->prev = weibo; } list_head = weibo; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return list_head; }
/*------------------------------------------------------------------------ * Callback invoked once the connection has been established to the HTTP server, * or on connection failure. * * @param user_data The MXit session object * @param source The file-descriptor associated with the connection * @param error_message Message explaining why the connection failed */ static void mxit_cb_http_connect( gpointer user_data, gint source, const gchar* error_message ) { struct http_request* req = (struct http_request*) user_data; purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_http_connect\n" ); /* source is the file descriptor of the new connection */ if ( source < 0 ) { purple_debug_info( MXIT_PLUGIN_ID, "mxit_cb_http_connect failed: %s\n", error_message ); purple_connection_error( req->session->con, _( "Unable to connect to the MXit HTTP server. Please check your server settings." ) ); return; } /* we now have an open and active TCP connection to the mxit server */ req->session->fd = source; /* reset the receive buffer */ req->session->rx_state = RX_STATE_RLEN; req->session->rx_lbuf[0] = '\0'; req->session->rx_i = 0; req->session->rx_res = 0; /* start listening on the open connection for messages from the server (reference: "libpurple/eventloop.h") */ req->session->http_handler = purple_input_add( req->session->fd, PURPLE_INPUT_READ, mxit_cb_http_read, req->session ); /* actually send the request to the HTTP server */ mxit_http_raw_write( req->session->fd, req->data, req->datalen ); /* free up resources */ free_http_request( req ); req = NULL; }
void http_stream_on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) { size_t parsed; http_request_context *context = (http_request_context *)tcp->data; if (nread >= 0) { parsed = http_parser_execute(&context->parser, &parser_settings, buf.base, nread); if (parsed < nread) { //uv_close((uv_handle_t*) &client->handle, http_stream_on_close); } } else { uv_err_t err = uv_last_error(uv_loop); if (err.code != UV_EOF) { //UVERR(err, "read"); if (context->request != NULL) { free_http_request(context->request); } } uv_close((uv_handle_t*) &context->stream, http_stream_on_close); } free(buf.base); }
PTR_WEIBO_ENTITY get_user_timeline_byids(const char* access_token, const char* uid, int page) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "ssd", access_token, uid, page); char s[20] = {}; char **weibo_ids = NULL; int i = 0, cnt = 0; cJSON* root = NULL; PTR_WEIBO_ENTITY list_head = NULL; PTR_WEIBO_ENTITY weibo = NULL; PTR_HTTP_REQUEST request = alloc_http_request(3, 0, 0, 0); PTR_HTTP_RESPONSE response = NULL; request->params[0].name = "access_token"; request->params[0].value = access_token; request->params[1].name = "uid"; request->params[1].value = uid; snprintf(s, 20, "%d", page); request->params[2].name="page"; request->params[2].value=s; response = https_get(WEIBO_GET_USER_TIMELINE_IDS_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return NULL; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return NULL; } cnt = cJSON_GetArraySize(cJSON_GetObjectItem(root, "statuses")); weibo_ids = (char**) malloc(sizeof(char*)*cnt); for (i=0; i<cnt; i++) { weibo_ids[i] = (cJSON_GetArrayItem(cJSON_GetObjectItem(root, "statuses"), i))->valuestring; } list_head = show_multiple_weibo_byids(access_token, weibo_ids, cnt); free_http_request(request); free_http_response(response); free(weibo_ids); cJSON_Delete(root); debug_log_exit(FINE, func_name); return list_head; }
void free_http_connection(http_connection* connection) { if (connection->request != NULL) { free_http_request(connection->request); } free(connection); INCREMENT_STAT(stat_connections_destroyed_total); }
void free_http_connection(http_connection* connection) { if (connection->request) { free_http_request(connection->request); } http_request_buffer_destroy(connection->buffer); free(connection); INCREMENT_STAT(stat_connections_destroyed_total); }
BOOL repost_weibo(const char* access_token, const char* text, const char* weibo_id) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "sss", access_token, text, weibo_id); cJSON* root = NULL; PTR_HTTP_REQUEST request = alloc_http_request(0, 0, 0, (100+strlen(text))*sizeof(char)); PTR_HTTP_RESPONSE response = NULL; /* request->form[0].name = "access_token"; request->form[0].value = access_token; request->form[0].type = STRING; request->form[1].name = "status"; request->form[1].value = text; request->form[1].type = STRING; request->form[2].name = "id"; request->form[2].value = weibo_id; request->form[2].type = STRING; */ sprintf((char*)(request->body), "access_token=%s&id=%s&status=%s", access_token, weibo_id, text); request->body_length = strlen((char*)(request->body)); response = https_post(WEIBO_REPOST_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return False; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return False; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return True; }
static void free_http_get_check(void *data) { http_checker_t *http_get_chk = CHECKER_DATA(data); http_t *http = HTTP_ARG(http_get_chk); request_t *req = HTTP_REQ(http); free_list(&http_get_chk->url); free_http_request(req); FREE(http); FREE_PTR(http_get_chk); FREE_PTR(CHECKER_CO(data)); FREE(data); }
BOOL create_weibo_pic(const char* access_token, const char* text, const char* pic_name) { const char* func_name = __func__; debug_log_enter(FINE, func_name, "ss", access_token, text); cJSON* root = NULL; PTR_HTTP_REQUEST request = alloc_http_request(0, 0, 3, 0); PTR_HTTP_RESPONSE response = NULL; request->form[0].name = "access_token"; request->form[0].value = access_token; request->form[0].type = STRING; request->form[1].name = "status"; request->form[1].value = text; request->form[1].type = STRING; request->form[2].name = "pic"; request->form[2].value = pic_name; request->form[2].type = FILENAME; response = https_post(WEIBO_CREATE_URL, request); if (response->status_code != 200) { free_http_request(request); free_http_response(response); return False; } root = cJSON_Parse((char*)(response->body)); if (check_api_error(root)) { free_http_request(request); free_http_response(response); cJSON_Delete(root); return False; } free_http_request(request); free_http_response(response); cJSON_Delete(root); debug_log_exit(FINE, func_name); return True; }
char * trust_url(struct http_request *http, struct client_state *csp) { struct file_list *fl; struct block_spec *b; struct url_spec url[1], **tl, *t; char *p, *h; char *hostport, *path, *referrer; struct http_request rhttp[1]; int n; if(((fl = csp->tlist) == NULL) || ((b = fl->f) == NULL)) { return(NULL); } *url = dsplit(http->host); /* if splitting the domain fails, punt */ if(url->dbuf == NULL) return(NULL); memset(rhttp, '\0', sizeof(*rhttp)); for(b = b->next; b ; b = b->next) { if((b->url->port == 0) || (b->url->port == http->port)) { if((b->url->domain[0] == '\0') || (domaincmp(b->url, url) == 0)) { if((b->url->path == NULL) || #ifdef REGEX (regexec(b->url->preg, http->path, 0, NULL, 0) == 0) #else (strncmp(b->url->path, http->path, b->url->pathlen) == 0) #endif ) { freez(url->dbuf); freez(url->dvec); if(b->reject == 0) return(NULL); hostport = URL(http->hostport); path = URL(http->path); if(csp->referrer) { referrer = URL(csp->referrer); } else { referrer = URL("undefined"); } n = strlen(CTRUST); n += strlen(hostport); n += strlen(path); n += strlen(referrer); p = malloc(n); sprintf(p, CTRUST, hostport, path, referrer); freez(hostport); freez(path); freez(referrer); return(p); } } } } freez(url->dbuf); freez(url->dvec); if((csp->referrer == NULL)|| (strlen(csp->referrer) <= 9)) { /* no referrer was supplied */ goto trust_url_not_trusted; } /* forge a URL from the referrer so we can use * convert_url() to parse it into its components. */ p = NULL; p = strsav(p, "GET "); p = strsav(p, csp->referrer + 9); /* skip over "Referer: " */ p = strsav(p, " HTTP/1.0"); parse_http_request(p, rhttp, csp); if(rhttp->cmd == NULL) { freez(p); goto trust_url_not_trusted; } freez(p); *url = dsplit(rhttp->host); /* if splitting the domain fails, punt */ if(url->dbuf == NULL) goto trust_url_not_trusted; for(tl = trust_list; (t = *tl) ; tl++) { if((t->port == 0) || (t->port == rhttp->port)) { if((t->domain[0] == '\0') || domaincmp(t, url) == 0) { if((t->path == NULL) || #ifdef REGEX (regexec(t->preg, rhttp->path, 0, NULL, 0) == 0) #else (strncmp(t->path, rhttp->path, t->pathlen) == 0) #endif ) { /* if the URL's referrer is from a trusted referrer, then * add the target spec to the trustfile as an unblocked * domain and return NULL (which means it's OK). */ FILE *fp; freez(url->dbuf); freez(url->dvec); if((fp = fopen(trustfile, "a"))) { h = NULL; h = strsav(h, "~"); h = strsav(h, http->hostport); p = http->path; if((*p++ == '/') && (*p++ == '~')) { /* since this path points into a user's home space * be sure to include this spec in the trustfile. */ if((p = strchr(p, '/'))) { *p = '\0'; h = strsav(h, http->path); h = strsav(h, "/"); } } free_http_request(rhttp); fprintf(fp, "%s\n", h); freez(h); fclose(fp); } return(NULL); } } } } trust_url_not_trusted: free_http_request(rhttp); hostport = URL(http->hostport); path = URL(http->path); if(csp->referrer) { referrer = URL(csp->referrer); } else { referrer = URL("undefined"); } n = strlen(CTRUST); n += strlen(hostport); n += strlen(path); n += strlen(referrer); p = malloc(n); sprintf(p, CTRUST, hostport, path, referrer); freez(hostport); freez(path); freez(referrer); return(p); }
/********************************************************************* * * Function : sweep * * Description : Basically a mark and sweep garbage collector, it is run * (by the parent thread) every once in a while to reclaim memory. * * It uses a mark and sweep strategy: * 1) mark all files as inactive * * 2) check with each client: * if it is active, mark its files as active * if it is inactive, free its resources * * 3) free the resources of all of the files that * are still marked as inactive (and are obsolete). * * N.B. files that are not obsolete don't have an unloader defined. * * Parameters : None * * Returns : N/A * *********************************************************************/ void sweep(void) { struct file_list *fl, *nfl; struct client_state *csp, *last_active; int i; /* clear all of the file's active flags */ for ( fl = files->next; NULL != fl; fl = fl->next ) { fl->active = 0; } last_active = clients; csp = clients->next; while (NULL != csp) { if (csp->flags & CSP_FLAG_ACTIVE) { /* Mark this client's files as active */ /* * Always have a configuration file. * (Also note the slightly non-standard extra * indirection here.) */ csp->config->config_file_list->active = 1; /* * Actions files */ for (i = 0; i < MAX_AF_FILES; i++) { if (csp->actions_list[i]) { csp->actions_list[i]->active = 1; } } /* * Filter files */ for (i = 0; i < MAX_AF_FILES; i++) { if (csp->rlist[i]) { csp->rlist[i]->active = 1; } } /* * Trust file */ #ifdef FEATURE_TRUST if (csp->tlist) { csp->tlist->active = 1; } #endif /* def FEATURE_TRUST */ last_active = csp; csp = csp->next; } else /* * This client is not active. Free its resources. */ { last_active->next = csp->next; freez(csp->ip_addr_str); freez(csp->iob->buf); freez(csp->error_message); if (csp->action->flags & ACTION_FORWARD_OVERRIDE && NULL != csp->fwd) { unload_forward_spec(csp->fwd); } free_http_request(csp->http); destroy_list(csp->headers); destroy_list(csp->tags); free_current_action(csp->action); #ifdef FEATURE_STATISTICS urls_read++; if (csp->flags & CSP_FLAG_REJECTED) { urls_rejected++; } #endif /* def FEATURE_STATISTICS */ freez(csp); csp = last_active->next; } } nfl = files; fl = files->next; while (fl != NULL) { if ( ( 0 == fl->active ) && ( NULL != fl->unloader ) ) { nfl->next = fl->next; (fl->unloader)(fl->f); freez(fl->filename); freez(fl); fl = nfl->next; } else { nfl = fl; fl = fl->next; } } }