static gboolean run_check (gpointer user_data) { NMConnectivity *self = NM_CONNECTIVITY (user_data); NMConnectivityPrivate *priv; SoupURI *soup_uri; SoupMessage *msg; g_return_val_if_fail (NM_IS_CONNECTIVITY (self), FALSE); priv = NM_CONNECTIVITY_GET_PRIVATE (self); /* check given url async */ soup_uri = soup_uri_new (priv->uri); if (soup_uri && SOUP_URI_VALID_FOR_HTTP (soup_uri)) { msg = soup_message_new_from_uri ("GET", soup_uri); soup_message_set_flags (msg, SOUP_MESSAGE_NO_REDIRECT); soup_session_queue_message (priv->soup_session, msg, nm_connectivity_check_cb, self); priv->running = TRUE; g_object_notify (G_OBJECT (self), NM_CONNECTIVITY_RUNNING); nm_log_dbg (LOGD_CORE, "Connectivity check with uri '%s' started.", priv->uri); } else nm_log_err (LOGD_CORE, "Invalid uri '%s' for connectivity check.", priv->uri); if (soup_uri) soup_uri_free (soup_uri); return TRUE; /* keep firing */ }
gchar * find_domain(const gchar *s, int flags) { SoupURI *uri; gchar *ret, *p; if (s == NULL) return (NULL); uri = soup_uri_new(s); if (uri == NULL) return (NULL); if (!SOUP_URI_VALID_FOR_HTTP(uri)) { soup_uri_free(uri); return (NULL); } if (flags & XT_WL_TOPLEVEL && !isdigit((unsigned char)uri->host[strlen(uri->host) - 1])) p = tld_get_suffix(uri->host); else p = uri->host; if (flags & XT_WL_TOPLEVEL) ret = g_strdup_printf(".%s", p); else /* assume FQDN */ ret = g_strdup(p); soup_uri_free(uri); return (ret); }
gchar * find_domain(const gchar *s, int toplevel) { SoupURI *uri; gchar *ret, *p; if (s == NULL) return (NULL); uri = soup_uri_new(s); if (uri == NULL || !SOUP_URI_VALID_FOR_HTTP(uri)) { return (NULL); } if (toplevel && !isdigit(uri->host[strlen(uri->host) - 1])) { p = tld_get_suffix(uri->host); } else p = uri->host; ret = g_strdup_printf(".%s", p); soup_uri_free(uri); return (ret); }
/// Create a new request struct with a defined method and URL. This can /// optionally use a pre-allocated base URL. The base URL will not be copied /// or freed, and it is the responsibility of the calling function to handle it rmRequest* rm_request_new(const gchar *method, gchar *url, const SoupURI *baseUrl, GError **error) { rmRequest *req; req = g_malloc(sizeof(rmRequest)); req->method = g_quark_from_string(method); req->headers = NULL; req->bodyType = 0; req->body = NULL; req->bodyLength = 0; req->freeBody = FALSE; req->repeat = 1; if (baseUrl == NULL) { req->url = soup_uri_new(url); } else { req->url = soup_uri_new_with_base((SoupURI *) baseUrl, url); } if (! SOUP_URI_VALID_FOR_HTTP(req->url)) { g_set_error(error, RM_ERROR_REQ, RM_ERROR_REQ_INVALID_URI, "invalid URI provided for request: '%s'. Are you missing a base URL?", url); rm_request_free(req); return NULL; } return req; }
static void set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { NMConnectivity *self = NM_CONNECTIVITY (object); NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self); const char *uri, *response; guint interval; gboolean changed; switch (property_id) { case PROP_URI: uri = g_value_get_string (value); if (uri && !*uri) uri = NULL; changed = g_strcmp0 (uri, priv->uri) != 0; #if WITH_CONCHECK if (uri) { SoupURI *soup_uri = soup_uri_new (uri); if (!soup_uri || !SOUP_URI_VALID_FOR_HTTP (soup_uri)) { _LOGE ("invalid uri '%s' for connectivity check.", uri); uri = NULL; } if (uri && soup_uri && changed && soup_uri_get_scheme(soup_uri) == SOUP_URI_SCHEME_HTTPS) _LOGW ("use of HTTPS for connectivity checking is not reliable and is discouraged (URI: %s)", uri); if (soup_uri) soup_uri_free (soup_uri); } #endif if (changed) { g_free (priv->uri); priv->uri = g_strdup (uri); _reschedule_periodic_checks (self, TRUE); } break; case PROP_INTERVAL: interval = g_value_get_uint (value); if (priv->interval != interval) { priv->interval = interval; _reschedule_periodic_checks (self, TRUE); } break; case PROP_RESPONSE: response = g_value_get_string (value); if (g_strcmp0 (response, priv->response) != 0) { /* a response %NULL means, NM_CONFIG_DEFAULT_CONNECTIVITY_RESPONSE. Any other response * (including "") is accepted. */ g_free (priv->response); priv->response = g_strdup (response); _reschedule_periodic_checks (self, TRUE); } break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } }
static gboolean soup_request_http_check_uri (SoupRequest *request, SoupURI *uri, GError **error) { SoupRequestHTTP *http = SOUP_REQUEST_HTTP (request); if (!SOUP_URI_VALID_FOR_HTTP (uri)) return FALSE; http->priv->msg = soup_message_new_from_uri (SOUP_METHOD_GET, uri); return TRUE; }
gboolean test_url (const gchar *url) { gboolean ret; SoupURI *test_uri; if (url == NULL) return TRUE; ret = TRUE; test_uri = soup_uri_new (url); if (test_uri == NULL) { ret = FALSE; } else { if (SOUP_URI_VALID_FOR_HTTP (test_uri) == FALSE) ret = FALSE; soup_uri_free (test_uri); } return ret; }
static void set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec) { NMConnectivity *self = NM_CONNECTIVITY (object); NMConnectivityPrivate *priv = NM_CONNECTIVITY_GET_PRIVATE (self); switch (property_id) { case PROP_URI: g_free (priv->uri); priv->uri = get_non_empty_string_value (value); #if WITH_CONCHECK if (priv->uri) { SoupURI *uri = soup_uri_new (priv->uri); if (!uri || !SOUP_URI_VALID_FOR_HTTP (uri)) { nm_log_err (LOGD_CONCHECK, "Invalid uri '%s' for connectivity check.", priv->uri); g_free (priv->uri); priv->uri = NULL; } } #endif break; case PROP_INTERVAL: priv->interval = g_value_get_uint (value); break; case PROP_RESPONSE: g_free (priv->response); priv->response = get_non_empty_string_value (value); break; default: G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec); break; } }
static guint parse_request_headers (SoupMessage *msg, char *headers, guint headers_len, SoupEncoding *encoding, gpointer sock) { SoupMessagePrivate *priv = SOUP_MESSAGE_GET_PRIVATE (msg); char *req_method, *req_path, *url; SoupHTTPVersion version; const char *req_host; guint status; SoupURI *uri; status = soup_headers_parse_request (headers, headers_len, msg->request_headers, &req_method, &req_path, &version); if (!SOUP_STATUS_IS_SUCCESSFUL (status)) return status; g_object_set (G_OBJECT (msg), SOUP_MESSAGE_METHOD, req_method, SOUP_MESSAGE_HTTP_VERSION, version, NULL); g_free (req_method); /* Handle request body encoding */ *encoding = soup_message_headers_get_encoding (msg->request_headers); if (*encoding == SOUP_ENCODING_UNRECOGNIZED) { if (soup_message_headers_get_list (msg->request_headers, "Transfer-Encoding")) return SOUP_STATUS_NOT_IMPLEMENTED; else return SOUP_STATUS_BAD_REQUEST; } /* Generate correct context for request */ req_host = soup_message_headers_get_one (msg->request_headers, "Host"); if (req_host && strchr (req_host, '/')) { g_free (req_path); return SOUP_STATUS_BAD_REQUEST; } if (!strcmp (req_path, "*") && req_host) { /* Eg, "OPTIONS * HTTP/1.1" */ url = g_strdup_printf ("%s://%s", soup_socket_is_ssl (sock) ? "https" : "http", req_host); uri = soup_uri_new (url); if (uri) soup_uri_set_path (uri, "*"); g_free (url); } else if (*req_path != '/') { /* Must be an absolute URI */ uri = soup_uri_new (req_path); } else if (req_host) { url = g_strdup_printf ("%s://%s%s", soup_socket_is_ssl (sock) ? "https" : "http", req_host, req_path); uri = soup_uri_new (url); g_free (url); } else if (priv->http_version == SOUP_HTTP_1_0) { /* No Host header, no AbsoluteUri */ SoupAddress *addr = soup_socket_get_local_address (sock); uri = soup_uri_new (NULL); soup_uri_set_scheme (uri, soup_socket_is_ssl (sock) ? SOUP_URI_SCHEME_HTTPS : SOUP_URI_SCHEME_HTTP); soup_uri_set_host (uri, soup_address_get_physical (addr)); soup_uri_set_port (uri, soup_address_get_port (addr)); soup_uri_set_path (uri, req_path); } else uri = NULL; g_free (req_path); if (!SOUP_URI_VALID_FOR_HTTP (uri)) { /* certainly not "a valid host on the server" (RFC2616 5.2.3) * SOUP_URI_VALID_FOR_HTTP also guards against uri == NULL */ if (uri) soup_uri_free (uri); return SOUP_STATUS_BAD_REQUEST; } soup_message_set_uri (msg, uri); soup_uri_free (uri); return SOUP_STATUS_OK; }
/** * soup_uri_new_with_base: * @base: a base URI * @uri_string: the URI * * Parses @uri_string relative to @base. * * Return value: a parsed #SoupURI. **/ SoupURI * soup_uri_new_with_base (SoupURI *base, const char *uri_string) { SoupURI *uri; const char *end, *hash, *colon, *at, *path, *question; const char *p, *hostend; gboolean remove_dot_segments = TRUE; int len; /* First some cleanup steps (which are supposed to all be no-ops, * but...). Skip initial whitespace, strip out internal tabs and * line breaks, and ignore trailing whitespace. */ while (g_ascii_isspace (*uri_string)) uri_string++; len = strcspn (uri_string, "\t\n\r"); if (uri_string[len]) { char *clean = g_malloc (strlen (uri_string) + 1), *d; const char *s; for (s = uri_string, d = clean; *s; s++) { if (*s != '\t' && *s != '\n' && *s != '\r') *d++ = *s; } *d = '\0'; uri = soup_uri_new_with_base (base, clean); g_free (clean); return uri; } end = uri_string + len; while (end > uri_string && g_ascii_isspace (end[-1])) end--; uri = g_slice_new0 (SoupURI); /* Find fragment. */ hash = strchr (uri_string, '#'); if (hash) { uri->fragment = uri_normalized_copy (hash + 1, end - hash + 1, NULL); end = hash; } /* Find scheme: initial [a-z+.-]* substring until ":" */ p = uri_string; while (p < end && (g_ascii_isalnum (*p) || *p == '.' || *p == '+' || *p == '-')) p++; if (p > uri_string && *p == ':') { uri->scheme = soup_uri_parse_scheme (uri_string, p - uri_string); uri_string = p + 1; } if (uri_string == end && !base && !uri->fragment) return uri; /* Check for authority */ if (strncmp (uri_string, "//", 2) == 0) { uri_string += 2; path = uri_string + strcspn (uri_string, "/?#"); if (path > end) path = end; at = strchr (uri_string, '@'); if (at && at < path) { colon = strchr (uri_string, ':'); if (colon && colon < at) { uri->password = uri_decoded_copy (colon + 1, at - colon - 1); } else { uri->password = NULL; colon = at; } uri->user = uri_decoded_copy (uri_string, colon - uri_string); uri_string = at + 1; } else uri->user = uri->password = NULL; /* Find host and port. */ if (*uri_string == '[') { uri_string++; hostend = strchr (uri_string, ']'); if (!hostend || hostend > path) { soup_uri_free (uri); return NULL; } if (*(hostend + 1) == ':') colon = hostend + 1; else colon = NULL; } else { colon = memchr (uri_string, ':', path - uri_string); hostend = colon ? colon : path; } uri->host = uri_decoded_copy (uri_string, hostend - uri_string); if (colon && colon != path - 1) { char *portend; uri->port = strtoul (colon + 1, &portend, 10); if (portend != (char *)path) { soup_uri_free (uri); return NULL; } } uri_string = path; } /* Find query */ question = memchr (uri_string, '?', end - uri_string); if (question) { uri->query = uri_normalized_copy (question + 1, end - (question + 1), NULL); end = question; } if (end != uri_string) { uri->path = uri_normalized_copy (uri_string, end - uri_string, NULL); } /* Apply base URI. This is spelled out in RFC 3986. */ if (base && !uri->scheme && uri->host) uri->scheme = base->scheme; else if (base && !uri->scheme) { uri->scheme = base->scheme; uri->user = g_strdup (base->user); uri->password = g_strdup (base->password); uri->host = g_strdup (base->host); uri->port = base->port; if (!uri->path) { uri->path = g_strdup (base->path); if (!uri->query) uri->query = g_strdup (base->query); remove_dot_segments = FALSE; } else if (*uri->path != '/') { char *newpath, *last; last = strrchr (base->path, '/'); if (last) { newpath = g_strdup_printf ("%.*s%s", (int)(last + 1 - base->path), base->path, uri->path); } else newpath = g_strdup_printf ("/%s", uri->path); g_free (uri->path); uri->path = newpath; } } if (remove_dot_segments && uri->path && *uri->path) { char *p, *q; /* Remove "./" where "." is a complete segment. */ for (p = uri->path + 1; *p; ) { if (*(p - 1) == '/' && *p == '.' && *(p + 1) == '/') memmove (p, p + 2, strlen (p + 2) + 1); else p++; } /* Remove "." at end. */ if (p > uri->path + 2 && *(p - 1) == '.' && *(p - 2) == '/') *(p - 1) = '\0'; /* Remove "<segment>/../" where <segment> != ".." */ for (p = uri->path + 1; *p; ) { if (!strncmp (p, "../", 3)) { p += 3; continue; } q = strchr (p + 1, '/'); if (!q) break; if (strncmp (q, "/../", 4) != 0) { p = q + 1; continue; } memmove (p, q + 4, strlen (q + 4) + 1); p = uri->path + 1; } /* Remove "<segment>/.." at end where <segment> != ".." */ q = strrchr (uri->path, '/'); if (q && !strcmp (q, "/..")) { p = q - 1; while (p > uri->path && *p != '/') p--; if (strncmp (p, "/../", 4) != 0) *(p + 1) = 0; } /* Remove extraneous initial "/.."s */ while (!strncmp (uri->path, "/../", 4)) memmove (uri->path, uri->path + 3, strlen (uri->path) - 2); if (!strcmp (uri->path, "/..")) uri->path[1] = '\0'; } /* HTTP-specific stuff */ if (uri->scheme == SOUP_URI_SCHEME_HTTP || uri->scheme == SOUP_URI_SCHEME_HTTPS) { if (!uri->path) uri->path = g_strdup ("/"); if (!SOUP_URI_VALID_FOR_HTTP (uri)) { soup_uri_free (uri); return NULL; } } if (uri->scheme == SOUP_URI_SCHEME_FTP) { if (!uri->host) { soup_uri_free (uri); return NULL; } } if (!uri->port) uri->port = soup_scheme_default_port (uri->scheme); if (!uri->path) uri->path = g_strdup (""); return uri; }
static gboolean ai_app_validate_image_check (AsImage *im, AsAppValidateHelper *helper) { AsImageAlphaFlags alpha_flags; const gchar *url; gboolean require_correct_aspect_ratio = FALSE; gdouble desired_aspect = 1.777777778; gdouble screenshot_aspect; guint status_code; guint screenshot_height; guint screenshot_width; guint ss_size_height_max = 900; guint ss_size_height_min = 351; guint ss_size_width_max = 1600; guint ss_size_width_min = 624; g_autoptr(GdkPixbuf) pixbuf = NULL; g_autoptr(GInputStream) stream = NULL; g_autoptr(SoupMessage) msg = NULL; g_autoptr(SoupURI) base_uri = NULL; /* make the requirements more strict */ if ((helper->flags & AS_APP_VALIDATE_FLAG_STRICT) > 0) { require_correct_aspect_ratio = TRUE; } /* relax the requirements a bit */ if ((helper->flags & AS_APP_VALIDATE_FLAG_RELAX) > 0) { ss_size_height_max = 1800; ss_size_height_min = 150; ss_size_width_max = 3200; ss_size_width_min = 300; } /* have we got network access */ if ((helper->flags & AS_APP_VALIDATE_FLAG_NO_NETWORK) > 0) return TRUE; /* GET file */ url = as_image_get_url (im); g_debug ("checking %s", url); base_uri = soup_uri_new (url); if (!SOUP_URI_VALID_FOR_HTTP (base_uri)) { ai_app_validate_add (helper, AS_PROBLEM_KIND_URL_NOT_FOUND, "<screenshot> url not valid [%s]", url); return FALSE; } msg = soup_message_new_from_uri (SOUP_METHOD_GET, base_uri); if (msg == NULL) { g_warning ("Failed to setup message"); return FALSE; } /* send sync */ status_code = soup_session_send_message (helper->session, msg); if (SOUP_STATUS_IS_TRANSPORT_ERROR(status_code)) { ai_app_validate_add (helper, AS_PROBLEM_KIND_URL_NOT_FOUND, "<screenshot> failed to connect: %s [%s]", soup_status_get_phrase(status_code), url); return FALSE; } else if (status_code != SOUP_STATUS_OK) { ai_app_validate_add (helper, AS_PROBLEM_KIND_URL_NOT_FOUND, "<screenshot> failed to download (HTTP %d: %s) [%s]", status_code, soup_status_get_phrase(status_code), url); return FALSE; } /* check if it's a zero sized file */ if (msg->response_body->length == 0) { ai_app_validate_add (helper, AS_PROBLEM_KIND_FILE_INVALID, "<screenshot> url is a zero length file [%s]", url); return FALSE; } /* create a buffer with the data */ stream = g_memory_input_stream_new_from_data (msg->response_body->data, (gssize) msg->response_body->length, NULL); if (stream == NULL) { ai_app_validate_add (helper, AS_PROBLEM_KIND_URL_NOT_FOUND, "<screenshot> failed to load data [%s]", url); return FALSE; } /* load the image */ pixbuf = gdk_pixbuf_new_from_stream (stream, NULL, NULL); if (pixbuf == NULL) { ai_app_validate_add (helper, AS_PROBLEM_KIND_FILE_INVALID, "<screenshot> failed to load [%s]", url); return FALSE; } /* check width matches */ screenshot_width = (guint) gdk_pixbuf_get_width (pixbuf); screenshot_height = (guint) gdk_pixbuf_get_height (pixbuf); if (as_image_get_width (im) != 0 && as_image_get_width (im) != screenshot_width) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> width did not match specified [%s]", url); } /* check height matches */ if (as_image_get_height (im) != 0 && as_image_get_height (im) != screenshot_height) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> height did not match specified [%s]", url); } /* check size is reasonable */ if (screenshot_width < ss_size_width_min) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> width too small [%s] minimum is %upx", url, ss_size_width_min); } if (screenshot_height < ss_size_height_min) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> height too small [%s] minimum is %upx", url, ss_size_height_min); } if (screenshot_width > ss_size_width_max) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> width too large [%s] maximum is %upx", url, ss_size_width_max); } if (screenshot_height > ss_size_height_max) { ai_app_validate_add (helper, AS_PROBLEM_KIND_ATTRIBUTE_INVALID, "<screenshot> height too large [%s] maximum is %upx", url, ss_size_height_max); } /* check padding */ as_image_set_pixbuf (im, pixbuf); alpha_flags = as_image_get_alpha_flags (im); if ((alpha_flags & AS_IMAGE_ALPHA_FLAG_TOP) > 0|| (alpha_flags & AS_IMAGE_ALPHA_FLAG_BOTTOM) > 0) { ai_app_validate_add (helper, AS_PROBLEM_KIND_STYLE_INCORRECT, "<image> has vertical padding [%s]", url); } if ((alpha_flags & AS_IMAGE_ALPHA_FLAG_LEFT) > 0|| (alpha_flags & AS_IMAGE_ALPHA_FLAG_RIGHT) > 0) { ai_app_validate_add (helper, AS_PROBLEM_KIND_STYLE_INCORRECT, "<image> has horizontal padding [%s]", url); } /* check aspect ratio */ if (require_correct_aspect_ratio) { screenshot_aspect = (gdouble) screenshot_width / (gdouble) screenshot_height; if (ABS (screenshot_aspect - 1.777777777) > 0.1) { g_debug ("got aspect %.2f, wanted %.2f", screenshot_aspect, desired_aspect); ai_app_validate_add (helper, AS_PROBLEM_KIND_ASPECT_RATIO_INCORRECT, "<screenshot> aspect ratio not 16:9 [%s]", url); } } return TRUE; }
/** * gs_screenshot_image_load_async: **/ void gs_screenshot_image_load_async (GsScreenshotImage *ssimg, GCancellable *cancellable) { AsImage *im = NULL; GsScreenshotImagePrivate *priv; SoupURI *base_uri = NULL; const gchar *url; gint rc; _cleanup_free_ gchar *basename = NULL; _cleanup_free_ gchar *cachedir2 = NULL; _cleanup_free_ gchar *cachedir = NULL; _cleanup_free_ gchar *sizedir = NULL; g_return_if_fail (GS_IS_SCREENSHOT_IMAGE (ssimg)); priv = gs_screenshot_image_get_instance_private (ssimg); g_return_if_fail (AS_IS_SCREENSHOT (priv->screenshot)); g_return_if_fail (priv->width != 0); g_return_if_fail (priv->height != 0); /* load an image according to the scale factor */ priv->scale = gtk_widget_get_scale_factor (GTK_WIDGET (ssimg)); im = as_screenshot_get_image (priv->screenshot, priv->width * priv->scale, priv->height * priv->scale); /* if we've failed to load a HiDPI image, fallback to LoDPI */ if (im == NULL && priv->scale > 1) { priv->scale = 1; im = as_screenshot_get_image (priv->screenshot, priv->width, priv->height); } if (im == NULL) { /* TRANSLATORS: this is when we request a screenshot size that * the generator did not create or the parser did not add */ gs_screenshot_image_set_error (ssimg, _("Screenshot size not found")); return; } url = as_image_get_url (im); basename = g_path_get_basename (url); if (priv->width == G_MAXUINT || priv->height == G_MAXUINT) { sizedir = g_strdup ("unknown"); } else { sizedir = g_strdup_printf ("%ux%u", priv->width * priv->scale, priv->height * priv->scale); } cachedir = g_build_filename (priv->cachedir, "gnome-software", "screenshots", sizedir, NULL); rc = g_mkdir_with_parents (cachedir, 0700); if (rc != 0) { /* TRANSLATORS: this is when we try create the cache directory * but we were out of space or permission was denied */ gs_screenshot_image_set_error (ssimg, _("Could not create cache")); return; } /* does local file already exist */ priv->filename = g_build_filename (cachedir, basename, NULL); if (g_file_test (priv->filename, G_FILE_TEST_EXISTS)) { as_screenshot_show_image (ssimg); return; } /* can we load a blurred smaller version of this straight away */ if (priv->width > AS_IMAGE_THUMBNAIL_WIDTH && priv->height > AS_IMAGE_THUMBNAIL_HEIGHT) { cachedir2 = g_build_filename (priv->cachedir, "gnome-software", "screenshots", "112x63", basename, NULL); if (g_file_test (cachedir2, G_FILE_TEST_EXISTS)) gs_screenshot_image_show_blurred (ssimg, cachedir2); } /* download file */ g_debug ("downloading %s to %s", url, priv->filename); base_uri = soup_uri_new (url); if (base_uri == NULL || !SOUP_URI_VALID_FOR_HTTP (base_uri)) { /* TRANSLATORS: this is when we try to download a screenshot * that was not a valid URL */ gs_screenshot_image_set_error (ssimg, _("Screenshot not valid")); soup_uri_free (base_uri); return; } /* cancel any previous messages */ if (priv->message != NULL) { soup_session_cancel_message (priv->session, priv->message, SOUP_STATUS_CANCELLED); g_clear_object (&priv->message); } priv->message = soup_message_new_from_uri (SOUP_METHOD_GET, base_uri); if (priv->message == NULL) { /* TRANSLATORS: this is when networking is not available */ gs_screenshot_image_set_error (ssimg, _("Screenshot not available")); soup_uri_free (base_uri); return; } /* send async */ soup_session_queue_message (priv->session, g_object_ref (priv->message) /* transfer full */, gs_screenshot_image_complete_cb, g_object_ref (ssimg)); soup_uri_free (base_uri); }
static gint luaH_webview_set_prop(lua_State *L) { size_t len; widget_t *w = luaH_checkudata(L, 1, &widget_class); const gchar *prop = luaL_checklstring(L, 2, &len); GtkWidget *view = g_object_get_data(G_OBJECT(w->widget), "webview"); GObject *ws; property_tmp_values tmp; SoupURI *u; for (guint i = 0; i < LENGTH(properties); i++) { if (g_strcmp0(properties[i].name, prop)) continue; if (!properties[i].writable) { warn("attempt to set read-only property: %s", prop); return 0; } ws = get_settings_object(view, properties[i].scope); switch(properties[i].type) { case BOOL: tmp.b = luaH_checkboolean(L, 3); g_object_set(ws, prop, tmp.b, NULL); return 0; case CHAR: tmp.c = (gchar*) luaL_checklstring(L, 3, &len); g_object_set(ws, prop, tmp.c, NULL); return 0; case INT: tmp.i = (gint) luaL_checknumber(L, 3); g_object_set(ws, prop, tmp.i, NULL); return 0; case FLOAT: tmp.f = (gfloat) luaL_checknumber(L, 3); g_object_set(ws, prop, tmp.f, NULL); return 0; case DOUBLE: tmp.d = (gdouble) luaL_checknumber(L, 3); g_object_set(ws, prop, tmp.d, NULL); return 0; case URI: tmp.c = (gchar*) luaL_checkstring(L, 3); u = soup_uri_new(tmp.c); if (SOUP_URI_VALID_FOR_HTTP(u)) g_object_set(ws, prop, u, NULL); else luaL_error(L, "cannot parse uri: %s", tmp.c); soup_uri_free(u); return 0; default: warn("unknown property type for: %s", properties[i].name); break; } } warn("unknown property: %s", prop); return 0; }
static void do_soup_uri_null_tests (void) { SoupURI *uri, *uri2; char *uri_string; debug_printf (1, "\nsoup_uri_new (NULL)\n"); uri = soup_uri_new (NULL); if (SOUP_URI_IS_VALID (uri) || SOUP_URI_VALID_FOR_HTTP (uri)) { debug_printf (1, " ERROR: soup_uri_new(NULL) returns valid URI?\n"); errors++; } /* This implicitly also verifies that none of these methods g_warn */ if (soup_uri_get_scheme (uri) || soup_uri_get_user (uri) || soup_uri_get_password (uri) || soup_uri_get_host (uri) || soup_uri_get_port (uri) || soup_uri_get_path (uri) || soup_uri_get_query (uri) || soup_uri_get_fragment (uri)) { debug_printf (1, " ERROR: soup_uri_new(NULL) returns non-empty URI?\n"); errors++; } expect_warning = TRUE; uri2 = soup_uri_new_with_base (uri, "/path"); if (uri2 || expect_warning) { debug_printf (1, " ERROR: soup_uri_new_with_base didn't fail on NULL URI?\n"); errors++; expect_warning = FALSE; } expect_warning = TRUE; uri_string = soup_uri_to_string (uri, FALSE); if (expect_warning) { debug_printf (1, " ERROR: soup_uri_to_string didn't fail on NULL URI?\n"); errors++; expect_warning = FALSE; } else if (*uri_string) { debug_printf (1, " ERROR: soup_uri_to_string on NULL URI returned '%s'\n", uri_string); errors++; } g_free (uri_string); soup_uri_set_scheme (uri, SOUP_URI_SCHEME_HTTP); if (SOUP_URI_IS_VALID (uri) || SOUP_URI_VALID_FOR_HTTP (uri)) { debug_printf (1, " ERROR: setting scheme on NULL URI makes it valid?\n"); errors++; } expect_warning = TRUE; uri_string = soup_uri_to_string (uri, FALSE); if (expect_warning) { debug_printf (1, " ERROR: soup_uri_to_string didn't fail on scheme-only URI?\n"); errors++; expect_warning = FALSE; } else if (strcmp (uri_string, "http:") != 0) { debug_printf (1, " ERROR: soup_uri_to_string returned '%s' instead of 'http:'\n", uri_string); errors++; } g_free (uri_string); soup_uri_set_host (uri, "localhost"); if (SOUP_URI_IS_VALID (uri)) { debug_printf (1, " ERROR: setting scheme+host on NULL URI makes it valid?\n"); errors++; } if (SOUP_URI_VALID_FOR_HTTP (uri)) { debug_printf (1, " ERROR: setting scheme+host on NULL URI makes it valid for http?\n"); errors++; } expect_warning = TRUE; uri_string = soup_uri_to_string (uri, FALSE); if (expect_warning) { debug_printf (1, " ERROR: soup_uri_to_string didn't fail on scheme+host URI?\n"); errors++; expect_warning = FALSE; } else if (strcmp (uri_string, "http://localhost/") != 0) { debug_printf (1, " ERROR: soup_uri_to_string with NULL path returned '%s' instead of 'http://localhost/'\n", uri_string); errors++; } g_free (uri_string); expect_warning = TRUE; uri2 = soup_uri_new_with_base (uri, "/path"); if (expect_warning) { debug_printf (1, " ERROR: soup_uri_new_with_base didn't warn on NULL+scheme URI?\n"); errors++; expect_warning = FALSE; } else if (!uri2) { debug_printf (1, " ERROR: soup_uri_new_with_base didn't fix path on NULL+scheme URI\n"); errors++; } if (uri2) { uri_string = soup_uri_to_string (uri2, FALSE); if (!uri_string) { debug_printf (1, " ERROR: soup_uri_to_string failed on uri2?\n"); errors++; } else if (strcmp (uri_string, "http://localhost/path") != 0) { debug_printf (1, " ERROR: soup_uri_to_string returned '%s' instead of 'http://localhost/path'\n", uri_string); errors++; } g_free (uri_string); soup_uri_free (uri2); } expect_warning = TRUE; soup_uri_set_path (uri, NULL); if (expect_warning) { debug_printf (1, " ERROR: setting path to NULL doesn't warn\n"); errors++; expect_warning = FALSE; } if (!uri->path || *uri->path) { debug_printf (1, " ERROR: setting path to NULL != \"\"\n"); errors++; soup_uri_set_path (uri, ""); } uri_string = soup_uri_to_string (uri, FALSE); if (!uri_string) { debug_printf (1, " ERROR: soup_uri_to_string failed on complete URI?\n"); errors++; } else if (strcmp (uri_string, "http://localhost/") != 0) { debug_printf (1, " ERROR: soup_uri_to_string with empty path returned '%s' instead of 'http://localhost/'\n", uri_string); errors++; } g_free (uri_string); if (!SOUP_URI_IS_VALID (uri)) { debug_printf (1, " ERROR: setting scheme+path on NULL URI doesn't make it valid?\n"); errors++; } if (!SOUP_URI_VALID_FOR_HTTP (uri)) { debug_printf (1, " ERROR: setting scheme+host+path on NULL URI doesn't make it valid for http?\n"); errors++; } soup_uri_free (uri); }
static void parse_and_decrypt_signons (const char *signons, gboolean handle_forms) { int version; gchar **lines; int i; guint length; lines = g_strsplit (signons, "\r\n", -1); if (!g_ascii_strncasecmp (lines[0], "#2c", 3)) version = 1; else if (!g_ascii_strncasecmp (lines[0], "#2d", 3)) version = 2; else if (!g_ascii_strncasecmp (lines[0], "#2e", 3)) version = 3; else goto out; /* Skip the never-saved list */ for (i = 1; lines[i] && !g_str_equal (lines[i], "."); i++) { ; } i++; /* * Read saved passwords. The information is stored in blocks * separated by lines that only contain a dot. We find a block by * the separator and parse them one by one. */ length = g_strv_length (lines); while (i < length) { size_t begin = i; size_t end = i + 1; const char *realmBracketBegin = " ("; const char *realmBracketEnd = ")"; SoupURI *uri = NULL; char *realm = NULL; while (lines[end] && !g_str_equal (lines[end], ".")) end++; i = end + 1; /* A block has at least five lines */ if (end - begin < 5) continue; /* The first line is the site URL. * For HTTP authentication logins, the URL may contain http realm, * which will be in bracket: * sitename:8080 (realm) */ if (g_strstr_len (lines[begin], -1, realmBracketBegin)) { char *start_ptr, *end_ptr; char *full_url, *url; glong start, end; /* In this case the scheme may not exist. We assume that the * scheme is HTTP. */ if (!g_strstr_len (lines[begin], -1, "://")) full_url = g_strconcat ("http://", lines[begin], NULL); else full_url = g_strdup (lines[begin]); start_ptr = g_strstr_len (full_url, -1, realmBracketBegin); start = g_utf8_pointer_to_offset (full_url, start_ptr); url = g_utf8_substring (full_url, 0, start); url = g_strstrip (url); uri = soup_uri_new (url); g_free (url); start += strlen (realmBracketBegin); end_ptr = g_strstr_len (full_url, -1, realmBracketEnd) -1; end = g_utf8_pointer_to_offset (full_url, end_ptr); realm = g_utf8_substring (full_url, start, end); g_free (full_url); } else { /* Don't have HTTP realm. It is the URL that the following * password belongs to. */ uri = soup_uri_new (lines[begin]); } if (!SOUP_URI_VALID_FOR_HTTP (uri)) { soup_uri_free (uri); g_free (realm); continue; } ++begin; /* There may be multiple username/password pairs for this site. * In this case, they are saved in one block without a separated * line (contains a dot). */ while (begin + 4 < end) { char *username = NULL; char *password = NULL; char *form_username = NULL; char *form_password = NULL; guint32 item_id; /* The username */ if (handle_forms) { form_username = g_strdup (lines[begin++]); } else { begin++; /* Skip username element */ } username = decrypt (lines[begin++]); /* The password */ /* The element name has a leading '*' */ if (lines[begin][0] == '*') { if (handle_forms) { form_password = g_strdup (lines[begin++]); } else { begin++; /* Skip password element */ } password = decrypt (lines[begin++]); } else { /* Maybe the file is broken, skip to the next block */ g_free (username); break; } /* The action attribute for from the form element. This line * exists in version 2 or above */ if (version >= 2) { if (begin < end) /* Skip it */ ; begin++; /* Version 3 has an extra line for further use */ if (version == 3) begin++; } if (handle_forms && !realm && username && password && !g_str_equal (username, "") && !g_str_equal (form_username, "") && !g_str_equal (form_password, "*")) { char *u = soup_uri_to_string (uri, FALSE); /* We skip the '*' at the beginning of form_password. */ _ephy_profile_utils_store_form_auth_data (u, form_username, form_password+1, username, password); g_free (u); } else if (!handle_forms && realm && username && password && !g_str_equal (username, "") && form_username == NULL && form_password == NULL) { gnome_keyring_set_network_password_sync (NULL, username, realm, uri->host, NULL, uri->scheme, NULL, uri->port, password, &item_id); } g_free (username); g_free (password); g_free (form_username); g_free (form_password); } soup_uri_free (uri); g_free (realm); } out: g_strfreev (lines); }
/** * soup_uri_new_with_base: * @base: a base URI * @uri_string: the URI * * Parses @uri_string relative to @base. * * Return value: a parsed #SoupURI. **/ SoupURI * soup_uri_new_with_base (SoupURI *base, const char *uri_string) { SoupURI *uri; const char *end, *hash, *colon, *at, *path, *question; const char *p, *hostend; gboolean remove_dot_segments = TRUE; uri = g_slice_new0 (SoupURI); /* See RFC 3986 for details. IF YOU CHANGE ANYTHING IN THIS * FUNCTION, RUN tests/uri-parsing AFTERWARDS. */ /* Find fragment. */ end = hash = strchr (uri_string, '#'); if (hash && hash[1]) { uri->fragment = uri_normalized_copy (hash + 1, strlen (hash + 1), NULL, FALSE); if (!uri->fragment) { soup_uri_free (uri); return NULL; } } else end = uri_string + strlen (uri_string); /* Find scheme: initial [a-z+.-]* substring until ":" */ p = uri_string; while (p < end && (g_ascii_isalnum (*p) || *p == '.' || *p == '+' || *p == '-')) p++; if (p > uri_string && *p == ':') { uri->scheme = soup_uri_get_scheme (uri_string, p - uri_string); if (!uri->scheme) { soup_uri_free (uri); return NULL; } uri_string = p + 1; } if (!*uri_string && !base) return uri; /* Check for authority */ if (strncmp (uri_string, "//", 2) == 0) { uri_string += 2; path = uri_string + strcspn (uri_string, "/?#"); at = strchr (uri_string, '@'); if (at && at < path) { colon = strchr (uri_string, ':'); if (colon && colon < at) { uri->password = uri_decoded_copy (colon + 1, at - colon - 1); if (!uri->password) { soup_uri_free (uri); return NULL; } } else { uri->password = NULL; colon = at; } uri->user = uri_decoded_copy (uri_string, colon - uri_string); if (!uri->user) { soup_uri_free (uri); return NULL; } uri_string = at + 1; } else uri->user = uri->password = NULL; /* Find host and port. */ if (*uri_string == '[') { uri_string++; hostend = strchr (uri_string, ']'); if (!hostend || hostend > path) { soup_uri_free (uri); return NULL; } if (*(hostend + 1) == ':') colon = hostend + 1; else colon = NULL; } else { colon = memchr (uri_string, ':', path - uri_string); hostend = colon ? colon : path; } uri->host = uri_decoded_copy (uri_string, hostend - uri_string); if (!uri->host) { soup_uri_free (uri); return NULL; } if (colon && colon != path - 1) { char *portend; uri->port = strtoul (colon + 1, &portend, 10); if (portend != (char *)path) { soup_uri_free (uri); return NULL; } } uri_string = path; } /* Find query */ question = memchr (uri_string, '?', end - uri_string); if (question) { if (question[1]) { uri->query = uri_normalized_copy (question + 1, end - (question + 1), NULL, TRUE); if (!uri->query) { soup_uri_free (uri); return NULL; } } end = question; } if (end != uri_string) { uri->path = uri_normalized_copy (uri_string, end - uri_string, NULL, TRUE); if (!uri->path) { soup_uri_free (uri); return NULL; } } /* Apply base URI. Again, this is spelled out in RFC 3986. */ if (base && !uri->scheme && uri->host) uri->scheme = base->scheme; else if (base && !uri->scheme) { uri->scheme = base->scheme; uri->user = g_strdup (base->user); uri->password = g_strdup (base->password); uri->host = g_strdup (base->host); uri->port = base->port; if (!uri->path) { uri->path = g_strdup (base->path); if (!uri->query) uri->query = g_strdup (base->query); remove_dot_segments = FALSE; } else if (*uri->path != '/') { char *newpath, *last; last = strrchr (base->path, '/'); if (last) { newpath = g_strdup_printf ("%.*s/%s", (int)(last - base->path), base->path, uri->path); } else newpath = g_strdup_printf ("/%s", uri->path); g_free (uri->path); uri->path = newpath; } } if (remove_dot_segments && uri->path && *uri->path) { char *p = uri->path, *q; /* Remove "./" where "." is a complete segment. */ for (p = uri->path + 1; *p; ) { if (*(p - 1) == '/' && *p == '.' && *(p + 1) == '/') memmove (p, p + 2, strlen (p + 2) + 1); else p++; } /* Remove "." at end. */ if (p > uri->path + 2 && *(p - 1) == '.' && *(p - 2) == '/') *(p - 1) = '\0'; /* Remove "<segment>/../" where <segment> != ".." */ for (p = uri->path + 1; *p; ) { if (!strncmp (p, "../", 3)) { p += 3; continue; } q = strchr (p + 1, '/'); if (!q) break; if (strncmp (q, "/../", 4) != 0) { p = q + 1; continue; } memmove (p, q + 4, strlen (q + 4) + 1); p = uri->path + 1; } /* Remove "<segment>/.." at end where <segment> != ".." */ q = strrchr (uri->path, '/'); if (q && !strcmp (q, "/..")) { p = q - 1; while (p > uri->path && *p != '/') p--; if (strncmp (p, "/../", 4) != 0) *(p + 1) = 0; } /* Remove extraneous initial "/.."s */ while (!strncmp (uri->path, "/../", 4)) memmove (uri->path, uri->path + 3, strlen (uri->path) - 2); if (!strcmp (uri->path, "/..")) uri->path[1] = '\0'; } /* HTTP-specific stuff */ if (uri->scheme == SOUP_URI_SCHEME_HTTP || uri->scheme == SOUP_URI_SCHEME_HTTPS) { if (!SOUP_URI_VALID_FOR_HTTP (uri)) { soup_uri_free (uri); return NULL; } if (!uri->path) uri->path = g_strdup ("/"); } if (!uri->port) uri->port = soup_scheme_default_port (uri->scheme); if (!uri->path) uri->path = g_strdup (""); return uri; }
void gs_screenshot_image_load_async (GsScreenshotImage *ssimg, GCancellable *cancellable) { AsImage *im = NULL; const gchar *url; g_autofree gchar *basename = NULL; g_autofree gchar *cache_kind = NULL; g_autofree gchar *cachefn_thumb = NULL; g_autofree gchar *sizedir = NULL; g_autoptr(SoupURI) base_uri = NULL; g_return_if_fail (GS_IS_SCREENSHOT_IMAGE (ssimg)); g_return_if_fail (AS_IS_SCREENSHOT (ssimg->screenshot)); g_return_if_fail (ssimg->width != 0); g_return_if_fail (ssimg->height != 0); /* load an image according to the scale factor */ ssimg->scale = (guint) gtk_widget_get_scale_factor (GTK_WIDGET (ssimg)); im = as_screenshot_get_image (ssimg->screenshot, ssimg->width * ssimg->scale, ssimg->height * ssimg->scale); /* if we've failed to load a HiDPI image, fallback to LoDPI */ if (im == NULL && ssimg->scale > 1) { ssimg->scale = 1; im = as_screenshot_get_image (ssimg->screenshot, ssimg->width, ssimg->height); } if (im == NULL) { /* TRANSLATORS: this is when we request a screenshot size that * the generator did not create or the parser did not add */ gs_screenshot_image_set_error (ssimg, _("Screenshot size not found")); return; } /* check if the URL points to a local file */ url = as_image_get_url (im); if (g_str_has_prefix (url, "file://")) { g_free (ssimg->filename); ssimg->filename = g_strdup (url + 7); if (g_file_test (ssimg->filename, G_FILE_TEST_EXISTS)) { as_screenshot_show_image (ssimg); return; } } basename = gs_screenshot_get_cachefn_for_url (url); if (ssimg->width == G_MAXUINT || ssimg->height == G_MAXUINT) { sizedir = g_strdup ("unknown"); } else { sizedir = g_strdup_printf ("%ux%u", ssimg->width * ssimg->scale, ssimg->height * ssimg->scale); } cache_kind = g_build_filename ("screenshots", sizedir, NULL); g_free (ssimg->filename); ssimg->filename = gs_utils_get_cache_filename (cache_kind, basename, GS_UTILS_CACHE_FLAG_NONE, NULL); if (ssimg->filename == NULL) { /* TRANSLATORS: this is when we try create the cache directory * but we were out of space or permission was denied */ gs_screenshot_image_set_error (ssimg, _("Could not create cache")); return; } /* does local file already exist and has recently been downloaded */ if (g_file_test (ssimg->filename, G_FILE_TEST_EXISTS)) { guint64 age_max; g_autoptr(GFile) file = NULL; /* show the image we have in cache while we're checking for the * new screenshot (which probably won't have changed) */ as_screenshot_show_image (ssimg); /* verify the cache age against the maximum allowed */ age_max = g_settings_get_uint (ssimg->settings, "screenshot-cache-age-maximum"); file = g_file_new_for_path (ssimg->filename); /* image new enough, not re-requesting from server */ if (age_max > 0 && gs_utils_get_file_age (file) < age_max) return; } /* if we're not showing a full-size image, we try loading a blurred * smaller version of it straight away */ if (!ssimg->showing_image && ssimg->width > AS_IMAGE_THUMBNAIL_WIDTH && ssimg->height > AS_IMAGE_THUMBNAIL_HEIGHT) { const gchar *url_thumb; g_autofree gchar *basename_thumb = NULL; g_autofree gchar *cache_kind_thumb = NULL; im = as_screenshot_get_image (ssimg->screenshot, AS_IMAGE_THUMBNAIL_WIDTH * ssimg->scale, AS_IMAGE_THUMBNAIL_HEIGHT * ssimg->scale); url_thumb = as_image_get_url (im); basename_thumb = gs_screenshot_get_cachefn_for_url (url_thumb); cache_kind_thumb = g_build_filename ("screenshots", "112x63", NULL); cachefn_thumb = gs_utils_get_cache_filename (cache_kind_thumb, basename_thumb, GS_UTILS_CACHE_FLAG_NONE, NULL); if (cachefn_thumb == NULL) return; if (g_file_test (cachefn_thumb, G_FILE_TEST_EXISTS)) gs_screenshot_image_show_blurred (ssimg, cachefn_thumb); } /* re-request the cache filename, which might be different as it needs * to be writable this time */ g_free (ssimg->filename); ssimg->filename = gs_utils_get_cache_filename (cache_kind, basename, GS_UTILS_CACHE_FLAG_WRITEABLE, NULL); /* download file */ g_debug ("downloading %s to %s", url, ssimg->filename); base_uri = soup_uri_new (url); if (base_uri == NULL || !SOUP_URI_VALID_FOR_HTTP (base_uri)) { /* TRANSLATORS: this is when we try to download a screenshot * that was not a valid URL */ gs_screenshot_image_set_error (ssimg, _("Screenshot not valid")); return; } /* cancel any previous messages */ if (ssimg->message != NULL) { soup_session_cancel_message (ssimg->session, ssimg->message, SOUP_STATUS_CANCELLED); g_clear_object (&ssimg->message); } ssimg->message = soup_message_new_from_uri (SOUP_METHOD_GET, base_uri); if (ssimg->message == NULL) { /* TRANSLATORS: this is when networking is not available */ gs_screenshot_image_set_error (ssimg, _("Screenshot not available")); return; } /* not all servers support If-Modified-Since, but worst case we just * re-download the entire file again every 30 days */ if (g_file_test (ssimg->filename, G_FILE_TEST_EXISTS)) { g_autoptr(GFile) file = g_file_new_for_path (ssimg->filename); gs_screenshot_soup_msg_set_modified_request (ssimg->message, file); } /* send async */ soup_session_queue_message (ssimg->session, g_object_ref (ssimg->message) /* transfer full */, gs_screenshot_image_complete_cb, g_object_ref (ssimg)); }