/** * soup_date_new_from_now: * @offset_seconds: offset from current time * * Creates a #SoupDate representing a time @offset_seconds after the * current time (or before it, if @offset_seconds is negative). If * offset_seconds is 0, returns the current time. * * If @offset_seconds would indicate a time not expressible as a * #time_t, the return value will be clamped into range. * * Return value: a new #SoupDate **/ SoupDate * soup_date_new_from_now (int offset_seconds) { time_t now = time (NULL); time_t then = now + offset_seconds; if (sizeof (time_t) == 4) { if (offset_seconds < 0 && then > now) return soup_date_new_from_time_t (-G_MAXINT); else if (offset_seconds > 0 && then < now) return soup_date_new_from_time_t (G_MAXINT); } return soup_date_new_from_time_t (then); }
int main (int argc, char **argv) { int i; test_init (argc, argv, NULL); debug_printf (1, "Good dates:\n"); for (i = 0; i < G_N_ELEMENTS (good_dates); i++) check_good (good_dates[i].format, good_dates[i].date); debug_printf (1, "\nOK dates:\n"); for (i = 0; i < G_N_ELEMENTS (ok_dates); i++) check_ok (ok_dates[i], make_date (ok_dates[i])); check_ok (TIME_T_STRING, soup_date_new_from_time_t (TIME_T)); debug_printf (1, "\nBad dates:\n"); for (i = 0; i < G_N_ELEMENTS (bad_dates); i++) check_bad (bad_dates[i], make_date (bad_dates[i])); debug_printf (1, "\nConversions:\n"); for (i = 0; i < G_N_ELEMENTS (conversions); i++) check_conversion (&conversions[i] ); test_cleanup (); return errors != 0; }
gchar * twitter_http_date_from_time_t (time_t time_) { SoupDate *soup_date; gchar *retval; soup_date = soup_date_new_from_time_t (time_); retval = soup_date_to_string (soup_date, SOUP_DATE_HTTP); soup_date_free (soup_date); return retval; }
void setcookie(SoupCookie *c) { int lock; lock = open(cookiefile, 0); flock(lock, LOCK_EX); SoupDate *e; SoupCookieJar *j = soup_cookie_jar_text_new(cookiefile, FALSE); c = soup_cookie_copy(c); if(c->expires == NULL && sessiontime) { e = soup_date_new_from_time_t(time(NULL) + sessiontime); soup_cookie_set_expires(c, e); } soup_cookie_jar_add_cookie(j, c); g_object_unref(j); flock(lock, LOCK_UN); close(lock); }
void changecookie(SoupCookieJar *j, SoupCookie *oc, SoupCookie *c, gpointer p) { SoupDate *e; SoupCookieJar *jar; if(lockcookie) return; if(c && c->expires == NULL) { e = soup_date_new_from_time_t(time(NULL) + sessiontime); c = soup_cookie_copy(c); soup_cookie_set_expires(c, e); } jar = soup_cookie_jar_text_new(cookiefile, FALSE); if(c) soup_cookie_jar_add_cookie(jar, soup_cookie_copy(c)); else soup_cookie_jar_delete_cookie(jar, oc); g_object_unref(jar); }
/** * soup_date_new_from_now: * @offset_seconds: offset from current time * * Creates a #SoupDate representing a time @offset_seconds after the * current time (or before it, if @offset_seconds is negative). If * offset_seconds is 0, returns the current time. * * Return value: a new #SoupDate **/ SoupDate * soup_date_new_from_now (int offset_seconds) { return soup_date_new_from_time_t (time (NULL) + offset_seconds); }
/* Downloads a feed specified in the request structure, returns the downloaded data or NULL in the request structure. If the webserver reports a permanent redirection, the feed url will be modified and the old URL 'll be freed. The request structure will also contain the HTTP status and the last modified string. */ void network_process_request (const updateJobPtr job) { SoupMessage *msg; SoupDate *date; gboolean do_not_track = FALSE; g_assert (NULL != job->request); debug1 (DEBUG_NET, "downloading %s", job->request->source); if (job->request->postdata && (debug_level & DEBUG_VERBOSE) && (debug_level & DEBUG_NET)) debug1 (DEBUG_NET, " postdata=>>>%s<<<", job->request->postdata); /* Prepare the SoupMessage */ msg = soup_message_new (job->request->postdata ? SOUP_METHOD_POST : SOUP_METHOD_GET, job->request->source); if (!msg) { g_warning ("The request for %s could not be parsed!", job->request->source); return; } /* Set the postdata for the request */ if (job->request->postdata) { soup_message_set_request (msg, "application/x-www-form-urlencoded", SOUP_MEMORY_STATIC, /* libsoup won't free the postdata */ job->request->postdata, strlen (job->request->postdata)); } /* Set the If-Modified-Since: header */ if (job->request->updateState && update_state_get_lastmodified (job->request->updateState)) { gchar *datestr; date = soup_date_new_from_time_t (update_state_get_lastmodified (job->request->updateState)); datestr = soup_date_to_string (date, SOUP_DATE_HTTP); soup_message_headers_append (msg->request_headers, "If-Modified-Since", datestr); g_free (datestr); soup_date_free (date); } /* Set the If-None-Match header */ if (job->request->updateState && update_state_get_etag (job->request->updateState)) { soup_message_headers_append(msg->request_headers, "If-None-Match", update_state_get_etag (job->request->updateState)); } /* Set the I-AM header */ if (job->request->updateState && (update_state_get_lastmodified (job->request->updateState) || update_state_get_etag (job->request->updateState))) { soup_message_headers_append(msg->request_headers, "A-IM", "feed"); } /* Support HTTP content negotiation */ soup_message_headers_append(msg->request_headers, "Accept", "application/atom+xml,application/xml;q=0.9,text/xml;q=0.8,*/*;q=0.7"); /* Set the authentication */ if (!job->request->authValue && job->request->options && job->request->options->username && job->request->options->password) { SoupURI *uri = soup_message_get_uri (msg); soup_uri_set_user (uri, job->request->options->username); soup_uri_set_password (uri, job->request->options->password); } if (job->request->authValue) { soup_message_headers_append (msg->request_headers, "Authorization", job->request->authValue); } /* Add requested cookies */ if (job->request->updateState && job->request->updateState->cookies) { soup_message_headers_append (msg->request_headers, "Cookie", job->request->updateState->cookies); soup_message_disable_feature (msg, SOUP_TYPE_COOKIE_JAR); } /* TODO: Right now we send the msg, and if it requires authentication and * we didn't provide one, the petition fails and when the job is processed * it sees it needs authentication and displays a dialog, and if credentials * are entered, it queues a new job with auth credentials. Instead of that, * we should probably handle authentication directly here, connecting the * msg to a callback in case of 401 (see soup_message_add_status_code_handler()) * displaying the dialog ourselves, and requeing the msg if we get credentials */ /* Add Do Not Track header according to settings */ conf_get_bool_value (DO_NOT_TRACK, &do_not_track); if (do_not_track) soup_message_headers_append (msg->request_headers, "DNT", "1"); /* If the feed has "dont use a proxy" selected, use 'session2' which is non-proxy */ if (job->request->options && job->request->options->dontUseProxy) soup_session_queue_message (session2, msg, network_process_callback, job); else soup_session_queue_message (session, msg, network_process_callback, job); }
/* Downloads a feed specified in the request structure, returns the downloaded data or NULL in the request structure. If the the webserver reports a permanent redirection, the feed url will be modified and the old URL 'll be freed. The request structure will also contain the HTTP status and the last modified string. */ void network_process_request (const updateJobPtr const job) { SoupMessage *msg; SoupDate *date; g_assert (NULL != job->request); debug1 (DEBUG_NET, "downloading %s", job->request->source); /* Prepare the SoupMessage */ msg = soup_message_new (job->request->postdata ? SOUP_METHOD_POST : SOUP_METHOD_GET, job->request->source); if (!msg) { g_warning ("The request for %s could not be parsed!", job->request->source); return; } /* Set the postdata for the request */ if (job->request->postdata) { soup_message_set_request (msg, "application/x-www-form-urlencoded", SOUP_MEMORY_STATIC, /* libsoup won't free the postdata */ job->request->postdata, strlen (job->request->postdata)); } /* Set the If-Modified-Since: header */ if (job->request->updateState && job->request->updateState->lastModified) { gchar *datestr; date = soup_date_new_from_time_t (job->request->updateState->lastModified); datestr = soup_date_to_string (date, SOUP_DATE_HTTP); soup_message_headers_append (msg->request_headers, "If-Modified-Since", datestr); g_free (datestr); soup_date_free (date); } /* Set the authentication */ if (!job->request->authValue && job->request->options && job->request->options->username && job->request->options->password) { SoupURI *uri = soup_message_get_uri (msg); soup_uri_set_user (uri, job->request->options->username); soup_uri_set_password (uri, job->request->options->password); } if (job->request->authValue) { soup_message_headers_append (msg->request_headers, "Authorization", job->request->authValue); } /* Add requested cookies */ if (job->request->updateState && job->request->updateState->cookies) { soup_message_headers_append (msg->request_headers, "Cookie", job->request->updateState->cookies); soup_message_disable_feature (msg, SOUP_TYPE_COOKIE_JAR); } /* TODO: Right now we send the msg, and if it requires authentication and * we didn't provide one, the petition fails and when the job is processed * it sees it needs authentication and displays a dialog, and if credentials * are entered, it queues a new job with auth credentials. Instead of that, * we should probably handle authentication directly here, connecting the * msg to a callback in case of 401 (see soup_message_add_status_code_handler()) * displaying the dialog ourselves, and requeing the msg if we get credentials */ /* If the feed has "dont use a proxy" selected, disable the proxy for the msg */ if ((job->request->options && job->request->options->dontUseProxy) || (network_get_proxy_host () == NULL)) soup_message_disable_feature (msg, SOUP_TYPE_PROXY_URI_RESOLVER); soup_session_queue_message (session, msg, network_process_callback, job); }
static SoupCookie* cookie_new_from_table(lua_State *L, gint idx, gchar **error) { SoupCookie *cookie = NULL; SoupDate *date; const gchar *name, *value, *domain, *path; name = value = domain = path = NULL; gboolean secure, http_only; gint expires; /* correct relative index */ if (idx < 0) idx = lua_gettop(L) + idx + 1; /* check for cookie table */ if (!lua_istable(L, idx)) { *error = g_strdup_printf("invalid cookie table, got %s", lua_typename(L, lua_type(L, idx))); return NULL; } #define IS_STRING (lua_isstring(L, -1) || lua_isnumber(L, -1)) #define IS_BOOLEAN (lua_isboolean(L, -1) || lua_isnil(L, -1)) #define IS_NUMBER (lua_isnumber(L, -1)) #define GET_PROP(prop, typname, typexpr, typfunc) \ lua_pushliteral(L, #prop); \ lua_rawget(L, idx); \ if ((typexpr)) { \ prop = typfunc(L, -1); \ lua_pop(L, 1); \ } else { \ *error = g_strdup_printf("invalid cookie." #prop " type, expected " \ #typname ", got %s", lua_typename(L, lua_type(L, -1))); \ return NULL; \ } /* get cookie properties */ GET_PROP(name, string, IS_STRING, lua_tostring) GET_PROP(value, string, IS_STRING, lua_tostring) GET_PROP(domain, string, IS_STRING, lua_tostring) GET_PROP(path, string, IS_STRING, lua_tostring) GET_PROP(secure, boolean, IS_BOOLEAN, lua_toboolean) GET_PROP(http_only, boolean, IS_BOOLEAN, lua_toboolean) GET_PROP(expires, number, IS_NUMBER, lua_tonumber) #undef IS_STRING #undef IS_BOOLEAN #undef IS_NUMBER #undef GET_PROP /* create soup cookie */ if ((cookie = soup_cookie_new(name, value, domain, path, expires))) { soup_cookie_set_secure(cookie, secure); soup_cookie_set_http_only(cookie, http_only); /* set real expiry date from unixtime */ if (expires > 0) { date = soup_date_new_from_time_t((time_t) expires); soup_cookie_set_expires(cookie, date); soup_date_free(date); } return cookie; } /* soup cookie creation failed */ *error = g_strdup_printf("soup cookie creation failed"); return NULL; }