Skip to content

Commit

Permalink
* src/ne_request.c (struct ne_request_s): Add target_uri field.
Browse files Browse the repository at this point in the history
  (ne_get_response_location): Return const, cache the URI in
  req->target_uri.
  (ne_get_response_location): Update accordingly.

* test/request.c (targets): Update accordingly.
  • Loading branch information
notroj committed Aug 19, 2024
1 parent 8c51f9d commit 8fba58e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 19 deletions.
35 changes: 22 additions & 13 deletions src/ne_request.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ struct ne_request_s {
struct interim_handler *interim_handler;

/*** Miscellaneous ***/
ne_uri *target_uri;
unsigned int method_is_head;
unsigned int can_persist;

Expand Down Expand Up @@ -582,16 +583,21 @@ static int get_request_target_uri(ne_request *req, ne_uri *uri)
}
}

ne_uri *ne_get_request_target(ne_request *req)
const ne_uri *ne_get_request_target(ne_request *req)
{
ne_uri *rv = ne_calloc(sizeof *rv);
if (req->target_uri == NULL) {
ne_uri *uri = ne_calloc(sizeof *uri);

if (get_request_target_uri(req, rv)) {
ne_uri_free(rv);
return NULL;
if (get_request_target_uri(req, uri) == 0) {
req->target_uri = uri;
}
else {
ne_uri_free(uri);
ne_free(uri);
}
}

return rv;
return req->target_uri;
}

/* Set the request body length to 'length' */
Expand Down Expand Up @@ -762,13 +768,13 @@ static void free_response_headers(ne_request *req)
ne_uri *ne_get_response_location(ne_request *req, const char *fragment)
{
const char *location;
ne_uri dest, base, *ret = NULL;
ne_uri dest, *ret = NULL;
const ne_uri *base;

location = get_response_header_hv(req, HH_HV_LOCATION, "location");
if (location == NULL)
return NULL;

memset(&base, 0, sizeof base);
memset(&dest, 0, sizeof dest);

/* Location is a URI-reference (RFC9110ẞ10.2.2) relative to the
Expand All @@ -781,22 +787,21 @@ ne_uri *ne_get_response_location(ne_request *req, const char *fragment)
goto fail;
}

if (get_request_target_uri(req, &base)) {
ne_set_error(req->session, _("Could not parse redirect "
"destination URL"));
if ((base = ne_get_request_target(req)) == NULL) {
ne_set_error(req->session, _("Could not parse request "
"target URI"));
goto fail;
}

ret = ne_malloc(sizeof *ret);
ne_uri_resolve(&base, &dest, ret);
ne_uri_resolve(base, &dest, ret);

/* HTTP-specific fragment handling is a MUST in RFC9110ẞ10.2.2: */
if (fragment && !dest.fragment) {
ret->fragment = ne_strdup(fragment);
}

fail:
ne_uri_free(&base);
ne_uri_free(&dest);

return ret;
Expand Down Expand Up @@ -832,6 +837,10 @@ void ne_request_destroy(ne_request *req)

ne_free(req->target);
ne_free(req->method);
if (req->target_uri) {
ne_uri_free(req->target_uri);
ne_free(req->target_uri);
}

for (rdr = req->body_readers; rdr != NULL; rdr = next_rdr) {
next_rdr = rdr->next;
Expand Down
2 changes: 1 addition & 1 deletion src/ne_request.h
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ void ne_print_request_header(ne_request *req, const char *name,
/* Returns the request target URI as a malloc-allocated ne_uri object,
* or NULL on error if the request target cannot be determined. The
* session error string is not changed on error. */
ne_uri *ne_get_request_target(ne_request *req);
const ne_uri *ne_get_request_target(ne_request *req);

/* If the response includes a Location header, this function parses
* and resolves the URI-reference relative to the request target. If
Expand Down
9 changes: 4 additions & 5 deletions test/request.c
Original file line number Diff line number Diff line change
Expand Up @@ -2455,23 +2455,22 @@ static int targets(void)
unsigned n;

for (n = 0; ts[n].scheme != NULL; n++ ) {
const ne_uri *uri, *uri2;
ne_session *sess;
ne_request *req;
ne_uri *uri;
char *actual;

sess = ne_session_create(ts[n].scheme, ts[n].host, ts[n].port);
req = ne_request_create(sess, ts[n].method, ts[n].target);
uri = ne_get_request_target(req);
uri2 = ne_get_request_target(req);
actual = uri ? ne_uri_unparse(uri) : NULL;

ONCMP(ts[n].expected, actual, "request target", "URI");

ONN("caching failed, different rv on second call", uri != uri2);

if (actual) ne_free(actual);
if (uri) {
ne_uri_free(uri);
ne_free(uri);
}

ne_request_destroy(req);
ne_session_destroy(sess);
Expand Down

0 comments on commit 8fba58e

Please sign in to comment.