This is an automated email from the ASF dual-hosted git repository.
zwoop pushed a commit to branch 9.0.x
in repository https://gitbox.apache.org/repos/asf/trafficserver.git
The following commit(s) were added to refs/heads/9.0.x by this push:
new 10f031f Add FetchSM support to dechunking in non-streaming mode. Add
new TS API TSFetchFlagSet()
10f031f is described below
commit 10f031f247d26b9296a205f7bcd1594f65d81746
Author: Sudheer Vinukonda <[email protected]>
AuthorDate: Wed Mar 18 16:17:27 2020 -0700
Add FetchSM support to dechunking in non-streaming mode.
Add new TS API TSFetchFlagSet()
(cherry picked from commit 7675d0b8082247de35146cedbce6875cb2d39f03)
---
.../api/functions/TSFetchCreate.en.rst | 55 ++++++++++++++++++++++
include/ts/experimental.h | 10 ++++
src/traffic_server/FetchSM.cc | 34 +++++++++++--
src/traffic_server/FetchSM.h | 7 ++-
src/traffic_server/InkAPI.cc | 7 +++
5 files changed, 109 insertions(+), 4 deletions(-)
diff --git a/doc/developer-guide/api/functions/TSFetchCreate.en.rst
b/doc/developer-guide/api/functions/TSFetchCreate.en.rst
new file mode 100644
index 0000000..9fe8ed5
--- /dev/null
+++ b/doc/developer-guide/api/functions/TSFetchCreate.en.rst
@@ -0,0 +1,55 @@
+.. Licensed to the Apache Software Foundation (ASF) under one
+ or more contributor license agreements. See the NOTICE file
+ distributed with this work for additional information
+ regarding copyright ownership. The ASF licenses this file
+ to you under the Apache License, Version 2.0 (the
+ "License"); you may not use this file except in compliance
+ with the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing,
+ software distributed under the License is distributed on an
+ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ KIND, either express or implied. See the License for the
+ specific language governing permissions and limitations
+ under the License.
+
+.. include:: ../../../common.defs
+
+.. default-domain:: c
+
+TSFetchCreate
+*************
+
+Traffic Server asynchronous Fetch API.
+
+Synopsis
+========
+
+.. code-block:: cpp
+
+ #include <ts/ts.h>
+
+.. function:: void TSFetchPages(TSFetchUrlParams_t *)
+.. function:: void TSFetchUrl(const char *, int, sockaddr const *, TSCont,
TSFetchWakeUpOptions, TSFetchEvent)
+.. function:: void TSFetchFlagSet(TSFetchSM, int)
+.. function:: TSFetch TSFetchCreate(TSCont, const char *, const char *, const
char *, struct sockaddr const *, int)
+.. function:: void TSFetchHeaderAdd(TSFetchSM, const char *, int, const char
*, int)
+.. function:: void TSFetchWriteData(TSFetchSM, const void *, size_t)
+.. function:: ssize_t TSFetchReadData(TSFetchSM, void *, size_t)
+.. function:: void TSFetchLaunch(TSFetchSM)
+.. function:: void TSFetchDestroy(TSFetchSM)
+.. function:: void TSFetchUserDataSet(TSFetchSM, void *)
+.. function:: void* TSFetchUserDataGet(TSFetchSM)
+.. function:: TSMBuffer TSFetchRespHdrMBufGet(TSFetchSM)
+.. function:: TSMLoc TSFetchRespHdrMLocGet(TSFetchSM)
+
+Description
+===========
+
+Traffic Server provides a number of routines for fetching resources
asynchronously.
+These API are useful to support a number of use cases that may involve sideways
+calls, while handling the client request. Some typical examples include
centralized
+rate limiting framework, database lookups for login/authentication, refreshing
configs
+in the background asynchronously, ESI etc.
diff --git a/include/ts/experimental.h b/include/ts/experimental.h
index 88b7a1d..4fb3d16 100644
--- a/include/ts/experimental.h
+++ b/include/ts/experimental.h
@@ -359,6 +359,16 @@ tsapi TSFetchSM TSFetchCreate(TSCont contp, const char
*method, const char *url,
struct sockaddr const *client_addr, int flags);
/*
+ * Set fetch flags to FetchSM Context
+ *
+ * @param fetch_sm: returned value of TSFetchCreate().
+ * @param flags: can be bitwise OR of several TSFetchFlags.
+ *
+ * return void
+ */
+tsapi void TSFetchFlagSet(TSFetchSM fetch_sm, int flags);
+
+/*
* Create FetchSM, this API will enable stream IO automatically.
*
* @param fetch_sm: returned value of TSFetchCreate().
diff --git a/src/traffic_server/FetchSM.cc b/src/traffic_server/FetchSM.cc
index 9a01e18..f682311 100644
--- a/src/traffic_server/FetchSM.cc
+++ b/src/traffic_server/FetchSM.cc
@@ -359,6 +359,7 @@ void
FetchSM::get_info_from_buffer(IOBufferReader *reader)
{
char *buf, *info;
+ IOBufferBlock *blk;
int64_t read_avail, read_done;
if (!reader) {
@@ -376,15 +377,42 @@ FetchSM::get_info_from_buffer(IOBufferReader *reader)
info = (char *)ats_malloc(sizeof(char) * (read_avail + 1));
client_response = info;
- // To maintain backwards compatibility we don't allow chunking when it's not
streaming.
- if (!(fetch_flags & TS_FETCH_FLAGS_STREAM) || !check_chunked()) {
+ /* Read the data out of the reader */
+ if (reader->block != NULL)
+ reader->skip_empty_blocks();
+
+ blk = reader->block.get();
+
+ // This is the equivalent of TSIOBufferBlockReadStart()
+ buf = blk->start() + reader->start_offset;
+ read_done = blk->read_avail() - reader->start_offset;
+
+ if (header_done == 0 && read_done > 0) {
+ int bytes_used = 0;
+ header_done = 1;
+ if (client_response_hdr.parse_resp(&http_parser, reader, &bytes_used, 0)
== PARSE_RESULT_DONE) {
+ if (bytes_used > 0) {
+ memcpy(info, buf, bytes_used);
+ info += bytes_used;
+ client_bytes += bytes_used;
+ }
+ } else {
+ Error("Failed to parse headers in FetchSM buffer");
+ }
+ // adjust the read_avail
+ read_avail -= bytes_used;
+ }
+
+ // Send the body without dechunk when neither streaming nor dechunk flag is
set
+ // Or when the body is not chunked
+ if (!((fetch_flags & TS_FETCH_FLAGS_STREAM) || (fetch_flags &
TS_FETCH_FLAGS_DECHUNK)) || !check_chunked()) {
/* Read the data out of the reader */
while (read_avail > 0) {
if (reader->block) {
reader->skip_empty_blocks();
}
- IOBufferBlock *blk = reader->block.get();
+ blk = reader->block.get();
// This is the equivalent of TSIOBufferBlockReadStart()
buf = blk->start() + reader->start_offset;
diff --git a/src/traffic_server/FetchSM.h b/src/traffic_server/FetchSM.h
index 0e57e4c..984887d 100644
--- a/src/traffic_server/FetchSM.h
+++ b/src/traffic_server/FetchSM.h
@@ -61,7 +61,6 @@ public:
callback_events = events;
callback_options = options;
_addr.assign(addr);
- fetch_flags = TS_FETCH_FLAGS_DECHUNK;
writeRequest(headers, length);
mutex = new_ProxyMutex();
@@ -76,6 +75,12 @@ public:
resp_buffer->water_mark = INT64_MAX;
}
+ void
+ set_fetch_flags(int flags)
+ {
+ fetch_flags = flags;
+ }
+
int fetch_handler(int event, void *data);
void process_fetch_read(int event);
void process_fetch_write(int event);
diff --git a/src/traffic_server/InkAPI.cc b/src/traffic_server/InkAPI.cc
index cc500ed..aff5c04 100644
--- a/src/traffic_server/InkAPI.cc
+++ b/src/traffic_server/InkAPI.cc
@@ -7929,6 +7929,13 @@ TSFetchUrl(const char *headers, int request_len,
sockaddr const *ip, TSCont cont
fetch_sm->httpConnect();
}
+void
+TSFetchFlagSet(TSFetchSM fetch_sm, int flags)
+{
+ sdk_assert(sdk_sanity_check_fetch_sm(fetch_sm) == TS_SUCCESS);
+ (reinterpret_cast<FetchSM *>(fetch_sm))->set_fetch_flags(flags);
+}
+
TSFetchSM
TSFetchCreate(TSCont contp, const char *method, const char *url, const char
*version, struct sockaddr const *client_addr, int flags)
{