Skip to content

Commit

Permalink
request redirection
Browse files Browse the repository at this point in the history
  • Loading branch information
janbar committed Nov 25, 2023
1 parent b8fb7fd commit 7fba0bd
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 0 deletions.
5 changes: 5 additions & 0 deletions noson/src/private/uriparser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ void URIParser::URIScan(char *uri, URI_t *parts)
*p = '\0';
parts->fragment = ++p;
}
if ((p = strchr(uri, '?')) != NULL)
{
*p = '\0';
parts->params = ++p;
}
if ((p = strchr(uri, ' ')) != NULL)
*p = '\0';

Expand Down
2 changes: 2 additions & 0 deletions noson/src/private/uriparser.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ namespace NSROOT
bool IsRelative() const { return m_parts.relative ? true : false; }
const char *Path() const { return IsRelative() ? m_parts.relative : m_parts.absolute; }
const char *Fragment() const { return m_parts.fragment; }
const char *Params() const { return m_parts.params; }

private:
// prevent copy
Expand All @@ -58,6 +59,7 @@ namespace NSROOT
char *absolute;
char *relative;
char *fragment;
char *params;
} URI_t;

URI_t m_parts;
Expand Down
49 changes: 49 additions & 0 deletions noson/src/private/wsrequest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,55 @@ WSRequest::~WSRequest()
{
}

WSRequest::WSRequest(const WSRequest& o, const URIParser& redirection)
: m_server(o.m_server)
, m_port(o.m_port)
, m_secure_uri(o.m_secure_uri)
, m_service_method(o.m_service_method)
, m_charset(o.m_charset)
, m_accept(o.m_accept)
, m_contentType(o.m_contentType)
, m_contentData(o.m_contentData)
, m_headers(o.m_headers)
, m_userAgent(o.m_userAgent)
{
if (redirection.Host())
m_server.assign(redirection.Host());
if (redirection.Scheme())
{
if (strncmp(redirection.Scheme(), "https", 5) == 0)
{
m_secure_uri = true;
m_port = redirection.Port() ? redirection.Port() : 443;
}
else
{
m_secure_uri = false;
m_port = redirection.Port() ? redirection.Port() : 80;
}
}

URIParser o_uri(m_service_url);
m_service_url = "/";
if (redirection.Path())
m_service_url.append(redirection.Path());
// prior redirection fragment then original fragment
if (redirection.Fragment())
m_service_url.append("#").append(redirection.Fragment());
else if (o_uri.Fragment())
m_service_url.append("#").append(o_uri.Fragment());
// prior redirection params then original params
if (redirection.Params() && strlen(redirection.Params()) > 0)
{
m_contentData.clear();
m_contentType = CT_FORM;
m_service_method = HRM_GET;
m_service_url.append("?").append(redirection.Params());
}
else if (o_uri.Params() && strlen(o_uri.Params()) > 0)
m_service_url.append("?").append(o_uri.Params());
}

void WSRequest::RequestService(const std::string& url, HRM_t method)
{
m_service_url = url;
Expand Down
5 changes: 5 additions & 0 deletions noson/src/private/wsrequest.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ namespace NSROOT
WSRequest(const URIParser& uri, HRM_t method = HRM_GET);
~WSRequest();

// Clone for redirection: see RFC-9110 section 10.2.2 Location
WSRequest(const WSRequest& o, const URIParser& redirection);

void RequestService(const std::string& url, HRM_t method = HRM_GET);
void RequestAccept(CT_t contentType);
void RequestAcceptEncoding(bool yesno);
Expand All @@ -71,6 +74,8 @@ namespace NSROOT
const std::string& GetServer() const { return m_server; }
unsigned GetPort() const { return m_port; }
bool IsSecureURI() const { return m_secure_uri; }
HRM_t GetMethod() const { return m_service_method; }
const std::string& GetURL() const { return m_service_url; }

private:
std::string m_server;
Expand Down

0 comments on commit 7fba0bd

Please sign in to comment.