trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From kic...@apache.org
Subject [trafficserver] branch master updated: remove unnecessary use of snprintf
Date Thu, 15 Mar 2018 01:17:04 GMT
This is an automated email from the ASF dual-hosted git repository.

kichan pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 24dee06  remove unnecessary use of snprintf
24dee06 is described below

commit 24dee0619183cfb1e1039e4c5a6859dd7087d8bf
Author: Kit Chan <kichan@apache.org>
AuthorDate: Tue Mar 13 23:24:58 2018 -0700

    remove unnecessary use of snprintf
---
 .../experimental/ts_lua/ts_lua_client_request.c    | 12 ++----
 plugins/experimental/ts_lua/ts_lua_http.c          | 18 +--------
 plugins/experimental/ts_lua/ts_lua_remap.c         | 44 ++++------------------
 .../experimental/ts_lua/ts_lua_server_request.c    | 12 ++----
 4 files changed, 16 insertions(+), 70 deletions(-)

diff --git a/plugins/experimental/ts_lua/ts_lua_client_request.c b/plugins/experimental/ts_lua/ts_lua_client_request.c
index d286bb5..f117691 100644
--- a/plugins/experimental/ts_lua/ts_lua_client_request.c
+++ b/plugins/experimental/ts_lua/ts_lua_client_request.c
@@ -527,10 +527,8 @@ ts_lua_client_request_set_url_scheme(lua_State *L)
 static int
 ts_lua_client_request_get_uri(lua_State *L)
 {
-  char uri[TS_LUA_MAX_URL_LENGTH];
   const char *path;
   int path_len;
-  int uri_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -538,13 +536,9 @@ ts_lua_client_request_get_uri(lua_State *L)
 
   path = TSUrlPathGet(http_ctx->client_request_bufp, http_ctx->client_request_url,
&path_len);
 
-  uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path);
-
-  if (uri_len >= TS_LUA_MAX_URL_LENGTH) {
-    lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1);
-  } else {
-    lua_pushlstring(L, uri, uri_len);
-  }
+  lua_pushlstring(L, "/", 1);
+  lua_pushlstring(L, path, path_len >= TS_LUA_MAX_URL_LENGTH - 1 ? TS_LUA_MAX_URL_LENGTH
- 2 : path_len);
+  lua_concat(L, 2);
 
   return 1;
 }
diff --git a/plugins/experimental/ts_lua/ts_lua_http.c b/plugins/experimental/ts_lua/ts_lua_http.c
index 245638b..9e9374c 100644
--- a/plugins/experimental/ts_lua/ts_lua_http.c
+++ b/plugins/experimental/ts_lua/ts_lua_http.c
@@ -350,8 +350,6 @@ ts_lua_http_set_cache_lookup_status(lua_State *L)
 static int
 ts_lua_http_get_cache_lookup_url(lua_State *L)
 {
-  char output[TS_LUA_MAX_URL_LENGTH];
-  int output_len;
   TSMLoc url = TS_NULL_MLOC;
   char *str  = NULL;
   int len;
@@ -372,12 +370,7 @@ ts_lua_http_get_cache_lookup_url(lua_State *L)
 
   str = TSUrlStringGet(http_ctx->client_request_bufp, url, &len);
 
-  output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", len, str);
-  if (output_len >= TS_LUA_MAX_URL_LENGTH) {
-    lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1);
-  } else {
-    lua_pushlstring(L, output, output_len);
-  }
+  lua_pushlstring(L, str, len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH - 1 : len);
 
 done:
   if (url != TS_NULL_MLOC) {
@@ -469,8 +462,6 @@ ts_lua_http_set_parent_proxy(lua_State *L)
 static int
 ts_lua_http_get_parent_selection_url(lua_State *L)
 {
-  char output[TS_LUA_MAX_URL_LENGTH];
-  int output_len;
   TSMLoc url = TS_NULL_MLOC;
   char *str  = NULL;
   int len;
@@ -491,12 +482,7 @@ ts_lua_http_get_parent_selection_url(lua_State *L)
 
   str = TSUrlStringGet(http_ctx->client_request_bufp, url, &len);
 
-  output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", len, str);
-  if (output_len >= TS_LUA_MAX_URL_LENGTH) {
-    lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1);
-  } else {
-    lua_pushlstring(L, output, output_len);
-  }
+  lua_pushlstring(L, str, len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH - 1 : len);
 
 done:
   if (url != TS_NULL_MLOC) {
diff --git a/plugins/experimental/ts_lua/ts_lua_remap.c b/plugins/experimental/ts_lua/ts_lua_remap.c
index 61fd88c..b53b03a 100644
--- a/plugins/experimental/ts_lua/ts_lua_remap.c
+++ b/plugins/experimental/ts_lua/ts_lua_remap.c
@@ -170,10 +170,8 @@ ts_lua_remap_get_from_url_scheme(lua_State *L)
 static int
 ts_lua_remap_get_from_uri(lua_State *L)
 {
-  char uri[TS_LUA_MAX_URL_LENGTH];
   const char *path;
   int path_len;
-  int uri_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -182,13 +180,9 @@ ts_lua_remap_get_from_uri(lua_State *L)
   if (http_ctx->rri != NULL) {
     path = TSUrlPathGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl,
&path_len);
 
-    uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path);
-
-    if (uri_len >= TS_LUA_MAX_URL_LENGTH) {
-      lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1);
-    } else {
-      lua_pushlstring(L, uri, uri_len);
-    }
+    lua_pushlstring(L, "/", 1);
+    lua_pushlstring(L, path, path_len >= TS_LUA_MAX_URL_LENGTH - 1 ? TS_LUA_MAX_URL_LENGTH
- 2 : path_len);
+    lua_concat(L, 2);
   } else {
     lua_pushnil(L);
   }
@@ -199,10 +193,8 @@ ts_lua_remap_get_from_uri(lua_State *L)
 static int
 ts_lua_remap_get_from_url(lua_State *L)
 {
-  char output[TS_LUA_MAX_URL_LENGTH];
   char *url;
   int url_len;
-  int output_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -211,13 +203,7 @@ ts_lua_remap_get_from_url(lua_State *L)
   if (http_ctx->rri != NULL) {
     url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->rri->mapFromUrl,
&url_len);
 
-    output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", url_len, url);
-
-    if (output_len >= TS_LUA_MAX_URL_LENGTH) {
-      lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1);
-    } else {
-      lua_pushlstring(L, output, output_len);
-    }
+    lua_pushlstring(L, url, url_len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH -
1 : url_len);
 
     TSfree(url);
   } else {
@@ -300,10 +286,8 @@ ts_lua_remap_get_to_url_scheme(lua_State *L)
 static int
 ts_lua_remap_get_to_uri(lua_State *L)
 {
-  char uri[TS_LUA_MAX_URL_LENGTH];
   const char *path;
   int path_len;
-  int uri_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -312,13 +296,9 @@ ts_lua_remap_get_to_uri(lua_State *L)
   if (http_ctx->rri != NULL) {
     path = TSUrlPathGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl,
&path_len);
 
-    uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path);
-
-    if (uri_len >= TS_LUA_MAX_URL_LENGTH) {
-      lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1);
-    } else {
-      lua_pushlstring(L, uri, uri_len);
-    }
+    lua_pushlstring(L, "/", 1);
+    lua_pushlstring(L, path, path_len >= TS_LUA_MAX_URL_LENGTH - 1 ? TS_LUA_MAX_URL_LENGTH
- 2 : path_len);
+    lua_concat(L, 2);
   } else {
     lua_pushnil(L);
   }
@@ -329,10 +309,8 @@ ts_lua_remap_get_to_uri(lua_State *L)
 static int
 ts_lua_remap_get_to_url(lua_State *L)
 {
-  char output[TS_LUA_MAX_URL_LENGTH];
   char *url;
   int url_len;
-  int output_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -341,13 +319,7 @@ ts_lua_remap_get_to_url(lua_State *L)
   if (http_ctx->rri != NULL) {
     url = TSUrlStringGet(http_ctx->client_request_bufp, http_ctx->rri->mapToUrl,
&url_len);
 
-    output_len = snprintf(output, TS_LUA_MAX_URL_LENGTH, "%.*s", url_len, url);
-
-    if (output_len >= TS_LUA_MAX_URL_LENGTH) {
-      lua_pushlstring(L, output, TS_LUA_MAX_URL_LENGTH - 1);
-    } else {
-      lua_pushlstring(L, output, output_len);
-    }
+    lua_pushlstring(L, url, url_len >= TS_LUA_MAX_URL_LENGTH ? TS_LUA_MAX_URL_LENGTH -
1 : url_len);
 
     TSfree(url);
   } else {
diff --git a/plugins/experimental/ts_lua/ts_lua_server_request.c b/plugins/experimental/ts_lua/ts_lua_server_request.c
index 7ba6995..67802d4 100644
--- a/plugins/experimental/ts_lua/ts_lua_server_request.c
+++ b/plugins/experimental/ts_lua/ts_lua_server_request.c
@@ -393,10 +393,8 @@ ts_lua_inject_server_request_uri_api(lua_State *L)
 static int
 ts_lua_server_request_get_uri(lua_State *L)
 {
-  char uri[TS_LUA_MAX_URL_LENGTH];
   const char *path;
   int path_len;
-  int uri_len;
 
   ts_lua_http_ctx *http_ctx;
 
@@ -406,13 +404,9 @@ ts_lua_server_request_get_uri(lua_State *L)
 
   path = TSUrlPathGet(http_ctx->server_request_bufp, http_ctx->server_request_url,
&path_len);
 
-  uri_len = snprintf(uri, TS_LUA_MAX_URL_LENGTH, "/%.*s", path_len, path);
-
-  if (uri_len >= TS_LUA_MAX_URL_LENGTH) {
-    lua_pushlstring(L, uri, TS_LUA_MAX_URL_LENGTH - 1);
-  } else {
-    lua_pushlstring(L, uri, uri_len);
-  }
+  lua_pushlstring(L, "/", 1);
+  lua_pushlstring(L, path, path_len >= TS_LUA_MAX_URL_LENGTH - 1 ? TS_LUA_MAX_URL_LENGTH
- 2 : path_len);
+  lua_concat(L, 2);
 
   return 1;
 }

-- 
To stop receiving notification emails like this one, please contact
kichan@apache.org.

Mime
View raw message