Skip to content

Commit

Permalink
ppb_url_loader: stop using file-backed NPN_PostURL/NPN_PostURLNotify
Browse files Browse the repository at this point in the history
In bug 1352572 Firefox removed file support from NPN_PostURL and
NPN_PostURLNotify. It's now required to construct entire request body
in continuous chunk of memory.
  • Loading branch information
i-rinat committed Dec 10, 2017
1 parent 246d712 commit 4a29ab5
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 36 deletions.
51 changes: 19 additions & 32 deletions src/ppb_url_loader.c
Original file line number Diff line number Diff line change
Expand Up @@ -138,41 +138,33 @@ url_loader_open_ptac(void *user_data)

if (p->method == PP_METHOD_POST) {
// POST request
char *tmpfname;
int fd;
FILE *fp = NULL;
int need_newline = 0;

tmpfname = g_strdup_printf("/tmp/FreshPostBodyXXXXXX"); // TODO: make configurable

fd = mkstemp(tmpfname);
if (fd < 0) {
int need_newline = 0;
GString *buf = g_string_new(NULL);
if (!buf) {
p->retval = NPERR_GENERIC_ERROR;
goto err;
}

fp = fdopen(fd, "wb+");
if (!fp) {
close(fd);
p->retval = NPERR_GENERIC_ERROR;
goto err;
goto quit;
}

if (p->request_headers) {
fprintf(fp, "%s\n", p->request_headers);
g_string_append_printf(buf, "%s\r\n", p->request_headers);
need_newline = 1;
}

if (p->custom_referrer_url) {
// the header name should be "referer", that's how it's spelled in HTTP spec
fprintf(fp, "Referer: %s\n", p->custom_referrer_url);
g_string_append_printf(buf, "Referer: %s\r\n", p->custom_referrer_url);
need_newline = 1;
}

if (p->custom_content_transfer_encoding) {
fprintf(fp, "Content-Transfer-Encoding: %s\n", p->custom_content_transfer_encoding);
g_string_append_printf(buf, "Content-Transfer-Encoding: %s\r\n",
p->custom_content_transfer_encoding);
need_newline = 1;
}

if (p->custom_user_agent) {
fprintf(fp, "User-Agent: %s\n", p->custom_user_agent);
g_string_append_printf(buf, "User-Agent: %s\r\n", p->custom_user_agent);
need_newline = 1;
}

Expand All @@ -185,38 +177,33 @@ url_loader_open_ptac(void *user_data)
}

if (post_len > 0) {
fprintf(fp, "Content-Length: %"PRIu64"\n", (uint64_t)post_len);
g_string_append_printf(buf, "Content-Length: %"PRIu64"\r\n", (uint64_t)post_len);
need_newline = 1;
}
}

if (need_newline)
fprintf(fp, "\n");
g_string_append(buf, "\r\n");

if (p->post_data) {
for (guint k = 0; k < p->post_data->len; k ++)
post_data_write_to_fp(p->post_data, k, fp);
post_data_write_to_gstring(p->post_data, k, buf);
}

fclose(fp); // flush all unwritten data and close the file
fp = NULL; // avoid calling fclose() twice

if (p->target) {
p->retval = npn.posturl(pp_i->npp, p->url, p->target, strlen(tmpfname), tmpfname, true);
p->retval = npn.posturl(pp_i->npp, p->url, p->target, buf->len, buf->str, false);
if (p->retval != NPERR_NO_ERROR)
trace_error("%s, NPN_PostURL returned %d\n", __func__, p->retval);

} else {
p->retval = npn.posturlnotify(pp_i->npp, p->url, NULL, strlen(tmpfname), tmpfname, true,
p->retval = npn.posturlnotify(pp_i->npp, p->url, NULL, buf->len, buf->str, false,
(void*)(size_t)p->loader);
if (p->retval != NPERR_NO_ERROR)
trace_error("%s, NPN_PostURLNotify returned %d\n", __func__, p->retval);
}
err:
if (fp)
fclose(fp);
unlink(tmpfname);
g_free(tmpfname);
g_string_free(buf, TRUE);

} else {
// GET request
if (p->target) {
Expand Down
6 changes: 3 additions & 3 deletions src/ppb_url_request_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@ post_data_free(GArray *post_data)
}

void
post_data_write_to_fp(GArray *post_data, guint idx, FILE *fp)
post_data_write_to_gstring(GArray *post_data, guint idx, GString *s)
{
char buf[128 * 1024];
struct post_data_item_s *pdi = &g_array_index(post_data, struct post_data_item_s, idx);
Expand All @@ -378,14 +378,14 @@ post_data_write_to_fp(GArray *post_data, guint idx, FILE *fp)
ssize_t read_bytes = RETRY_ON_EINTR(read(fd, buf, MIN(to_write, sizeof(buf))));
if (read_bytes == -1)
goto err;
fwrite(buf, 1, (size_t)read_bytes, fp);
g_string_append_len(s, buf, (size_t)read_bytes);
to_write -= read_bytes;
}
err:
if (fd >= 0)
close(fd);
} else {
fwrite(pdi->data, 1, pdi->len, fp);
g_string_append_len(s, pdi->data, pdi->len);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/ppb_url_request_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ size_t
post_data_get_all_item_length(GArray *post_data);

void
post_data_write_to_fp(GArray *post_data, guint idx, FILE *fp);
post_data_write_to_gstring(GArray *post_data, guint idx, GString *buf);

PP_Resource
ppb_url_request_info_create(PP_Instance instance);
Expand Down

0 comments on commit 4a29ab5

Please sign in to comment.