/** * Process fetch headers for a download context. * Extracts MIME type, total length, and creates gui_download_window * * \param ctx Context to process * \return NSERROR_OK on success, appropriate error otherwise */ static nserror download_context_process_headers(download_context *ctx) { const char *http_header; char *mime_type; http_parameter *params; unsigned long length; nserror error; /* Retrieve and parse Content-Type */ http_header = llcache_handle_get_header(ctx->llcache, "Content-Type"); if (http_header == NULL) http_header = "text/plain"; error = http_parse_content_type(http_header, &mime_type, ¶ms); if (error != NSERROR_OK) return error; /* Don't care about parameters */ http_parameter_list_destroy(params); /* Retrieve and parse Content-Length */ http_header = llcache_handle_get_header(ctx->llcache, "Content-Length"); if (http_header == NULL) length = 0; else length = strtoul(http_header, NULL, 10); ctx->mime_type = mime_type; ctx->total_length = length; /* Create the frontend window */ ctx->window = gui_download_window_create(ctx, ctx->parent); if (ctx->window == NULL) { free(ctx->mime_type); ctx->mime_type = NULL; return NSERROR_NOMEM; } return NSERROR_OK; }
/** * Process fetch headers for a download context. * Extracts MIME type, total length, and creates gui_download_window * * \param ctx Context to process * \return NSERROR_OK on success, appropriate error otherwise */ static nserror download_context_process_headers(download_context *ctx) { const char *http_header; http_content_type *content_type; unsigned long length; nserror error; /* Retrieve and parse Content-Type */ http_header = llcache_handle_get_header(ctx->llcache, "Content-Type"); if (http_header == NULL) http_header = "text/plain"; error = http_parse_content_type(http_header, &content_type); if (error != NSERROR_OK) return error; /* Retrieve and parse Content-Length */ http_header = llcache_handle_get_header(ctx->llcache, "Content-Length"); if (http_header == NULL) length = 0; else length = strtoul(http_header, NULL, 10); /* Retrieve and parse Content-Disposition */ http_header = llcache_handle_get_header(ctx->llcache, "Content-Disposition"); if (http_header != NULL) { lwc_string *filename; lwc_string *filename_value; http_content_disposition *disposition; if (lwc_intern_string("filename", SLEN("filename"), &filename) != lwc_error_ok) { http_content_type_destroy(content_type); return NSERROR_NOMEM; } error = http_parse_content_disposition(http_header, &disposition); if (error != NSERROR_OK) { lwc_string_unref(filename); http_content_type_destroy(content_type); return error; } error = http_parameter_list_find_item(disposition->parameters, filename, &filename_value); if (error == NSERROR_OK) { ctx->filename = download_parse_filename( lwc_string_data(filename_value)); lwc_string_unref(filename_value); } http_content_disposition_destroy(disposition); lwc_string_unref(filename); } ctx->mime_type = lwc_string_ref(content_type->media_type); ctx->total_length = length; if (ctx->filename == NULL) { ctx->filename = download_default_filename( nsurl_access( llcache_handle_get_url(ctx->llcache))); } http_content_type_destroy(content_type); if (ctx->filename == NULL) { lwc_string_unref(ctx->mime_type); ctx->mime_type = NULL; return NSERROR_NOMEM; } /* Create the frontend window */ ctx->window = gui_download_window_create(ctx, ctx->parent); if (ctx->window == NULL) { free(ctx->filename); ctx->filename = NULL; lwc_string_unref(ctx->mime_type); ctx->mime_type = NULL; return NSERROR_NOMEM; } return NSERROR_OK; }