Skip to content

Commit

Permalink
Change API to return a TSReturnCode code.
Browse files Browse the repository at this point in the history
Return a TS_ERROR when we cannot push the url to the promise table.

Signed-off-by: David Calavera <david.calavera@gmail.com>
  • Loading branch information
calavera committed Oct 30, 2019
1 parent 0af60b4 commit f1dbc39
Show file tree
Hide file tree
Showing 7 changed files with 41 additions and 26 deletions.
5 changes: 4 additions & 1 deletion doc/developer-guide/api/functions/TSHttpTxnServerPush.en.rst
Expand Up @@ -26,7 +26,7 @@ Synopsis

`#include <ts/ts.h>`

.. function:: void TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len)
.. function:: TSReturnCode TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len)

Description
===========
Expand All @@ -38,6 +38,9 @@ is not disabled by the client. You can call this API without checking whether
Server Push is available on the transaction and it does nothing if Server Push
is not available.

This API returns an error if the URL to push is not valid, the client has Server Push disabled,
or there is an error creating the H/2 PUSH_PROMISE frame.

See Also
========

Expand Down
2 changes: 1 addition & 1 deletion include/ts/ts.h
Expand Up @@ -2542,7 +2542,7 @@ tsapi TSIOBufferReader TSHttpTxnPostBufferReaderGet(TSHttpTxn txnp);
* @param url the URL string to preload.
* @param url_len the length of the URL string.
*/
tsapi void TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len);
tsapi TSReturnCode TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len);

#ifdef __cplusplus
}
Expand Down
11 changes: 6 additions & 5 deletions proxy/http2/Http2ConnectionState.cc
Expand Up @@ -1639,7 +1639,7 @@ Http2ConnectionState::send_headers_frame(Http2Stream *stream)
ats_free(buf);
}

void
bool
Http2ConnectionState::send_push_promise_frame(Http2Stream *stream, URL &url, const MIMEField *accept_encoding)
{
HTTPHdr h1_hdr, h2_hdr;
Expand All @@ -1651,7 +1651,7 @@ Http2ConnectionState::send_push_promise_frame(Http2Stream *stream, URL &url, con
uint8_t flags = 0x00;

if (client_settings.get(HTTP2_SETTINGS_ENABLE_PUSH) == 0) {
return;
return false;
}

Http2StreamDebug(ua_session, stream->get_id(), "Send PUSH_PROMISE frame");
Expand Down Expand Up @@ -1681,14 +1681,14 @@ Http2ConnectionState::send_push_promise_frame(Http2Stream *stream, URL &url, con
buf = static_cast<uint8_t *>(ats_malloc(buf_len));
if (buf == nullptr) {
h2_hdr.destroy();
return;
return false;
}
Http2ErrorCode result = http2_encode_header_blocks(&h2_hdr, buf, buf_len, &header_blocks_size, *(this->remote_hpack_handle),
client_settings.get(HTTP2_SETTINGS_HEADER_TABLE_SIZE));
if (result != Http2ErrorCode::HTTP2_ERROR_NO_ERROR) {
h2_hdr.destroy();
ats_free(buf);
return;
return false;
}

// Send a PUSH_PROMISE frame
Expand Down Expand Up @@ -1736,7 +1736,7 @@ Http2ConnectionState::send_push_promise_frame(Http2Stream *stream, URL &url, con
stream = this->create_stream(id, error);
if (!stream) {
h2_hdr.destroy();
return;
return false;
}

SCOPED_MUTEX_LOCK(stream_lock, stream->mutex, this_ethread());
Expand All @@ -1759,6 +1759,7 @@ Http2ConnectionState::send_push_promise_frame(Http2Stream *stream, URL &url, con
stream->send_request(*this);

h2_hdr.destroy();
return true;
}

void
Expand Down
2 changes: 1 addition & 1 deletion proxy/http2/Http2ConnectionState.h
Expand Up @@ -247,7 +247,7 @@ class Http2ConnectionState : public Continuation
void send_data_frames(Http2Stream *stream);
Http2SendDataFrameResult send_a_data_frame(Http2Stream *stream, size_t &payload_length);
void send_headers_frame(Http2Stream *stream);
void send_push_promise_frame(Http2Stream *stream, URL &url, const MIMEField *accept_encoding);
bool send_push_promise_frame(Http2Stream *stream, URL &url, const MIMEField *accept_encoding);
void send_rst_stream_frame(Http2StreamId id, Http2ErrorCode ec);
void send_settings_frame(const Http2ConnectionSettings &new_settings);
void send_ping_frame(Http2StreamId id, uint8_t flag, const uint8_t *opaque_data);
Expand Down
4 changes: 2 additions & 2 deletions proxy/http2/Http2Stream.cc
Expand Up @@ -711,12 +711,12 @@ Http2Stream::signal_write_event(bool call_update)
}
}

void
bool
Http2Stream::push_promise(URL &url, const MIMEField *accept_encoding)
{
Http2ClientSession *_proxy_ssn = static_cast<Http2ClientSession *>(this->get_proxy_ssn());
SCOPED_MUTEX_LOCK(lock, _proxy_ssn->connection_state.mutex, this_ethread());
_proxy_ssn->connection_state.send_push_promise_frame(this, url, accept_encoding);
return _proxy_ssn->connection_state.send_push_promise_frame(this, url, accept_encoding);
}

void
Expand Down
2 changes: 1 addition & 1 deletion proxy/http2/Http2Stream.h
Expand Up @@ -76,7 +76,7 @@ class Http2Stream : public ProxyTransaction
void update_write_request(IOBufferReader *buf_reader, int64_t write_len, bool send_update);
void signal_write_event(bool call_update);
void restart_sending();
void push_promise(URL &url, const MIMEField *accept_encoding);
bool push_promise(URL &url, const MIMEField *accept_encoding);

// Stream level window size
ssize_t client_rwnd() const;
Expand Down
41 changes: 26 additions & 15 deletions src/traffic_server/InkAPI.cc
Expand Up @@ -8026,7 +8026,7 @@ TSHttpTxnIsInternal(TSHttpTxn txnp)
return TSHttpSsnIsInternal(TSHttpTxnSsnGet(txnp));
}

void
TSReturnCode
TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len)
{
sdk_assert(sdk_sanity_check_txn(txnp) == TS_SUCCESS);
Expand All @@ -8035,26 +8035,37 @@ TSHttpTxnServerPush(TSHttpTxn txnp, const char *url, int url_len)
url_obj.create(nullptr);
if (url_obj.parse(url, url_len) == PARSE_RESULT_ERROR) {
url_obj.destroy();
return;
return TS_ERROR;
}

HttpSM *sm = reinterpret_cast<HttpSM *>(txnp);
Http2Stream *stream = dynamic_cast<Http2Stream *>(sm->ua_txn);
if (stream) {
Http2ClientSession *ua_session = static_cast<Http2ClientSession *>(stream->get_proxy_ssn());
SCOPED_MUTEX_LOCK(lock, ua_session->mutex, this_ethread());
if (!ua_session->connection_state.is_state_closed() && !ua_session->is_url_pushed(url, url_len)) {
HTTPHdr *hptr = &(sm->t_state.hdr_info.client_request);
TSMLoc obj = reinterpret_cast<TSMLoc>(hptr->m_http);

MIMEHdrImpl *mh = _hdr_mloc_to_mime_hdr_impl(obj);
MIMEField *f = mime_hdr_field_find(mh, MIME_FIELD_ACCEPT_ENCODING, MIME_LEN_ACCEPT_ENCODING);
stream->push_promise(url_obj, f);

ua_session->add_url_to_pushed_table(url, url_len);
}
if (stream == nullptr) {
url_obj.destroy();
return TS_ERROR;
}

Http2ClientSession *ua_session = static_cast<Http2ClientSession *>(stream->get_proxy_ssn());
SCOPED_MUTEX_LOCK(lock, ua_session->mutex, this_ethread());
if (ua_session->connection_state.is_state_closed() || ua_session->is_url_pushed(url, url_len)) {
url_obj.destroy();
return TS_ERROR;
}

HTTPHdr *hptr = &(sm->t_state.hdr_info.client_request);
TSMLoc obj = reinterpret_cast<TSMLoc>(hptr->m_http);

MIMEHdrImpl *mh = _hdr_mloc_to_mime_hdr_impl(obj);
MIMEField *f = mime_hdr_field_find(mh, MIME_FIELD_ACCEPT_ENCODING, MIME_LEN_ACCEPT_ENCODING);
if (!stream->push_promise(url_obj, f)) {
url_obj.destroy();
return TS_ERROR;
}

ua_session->add_url_to_pushed_table(url, url_len);

url_obj.destroy();
return TS_SUCCESS;
}

TSReturnCode
Expand Down

0 comments on commit f1dbc39

Please sign in to comment.