[Commits] 579b785: Add an option to specify port number

Sergei Petrunia psergey at askmonty.org
Fri Sep 4 00:01:08 EEST 2020


revision-id: 579b785002a6a31ecf84d0391de7d839aa612af0 (libmarias3-3.1.2-3-g579b785)
parent(s): d172e86c16224b4e0229ca6f102e662a2315aeff
author: Sergei Petrunia
committer: Sergei Petrunia
timestamp: 2020-09-04 00:01:08 +0300
message:

Add an option to specify port number

---
 libmarias3/marias3.h |  3 ++-
 src/marias3.c        | 14 ++++++++++++++
 src/request.c        | 34 +++++++++++++++++++++-------------
 src/structs.h        |  1 +
 4 files changed, 38 insertions(+), 14 deletions(-)

diff --git a/libmarias3/marias3.h b/libmarias3/marias3.h
index abe255b..a6f2bf3 100644
--- a/libmarias3/marias3.h
+++ b/libmarias3/marias3.h
@@ -80,7 +80,8 @@ enum ms3_set_option_t
   MS3_OPT_DISABLE_SSL_VERIFY,
   MS3_OPT_BUFFER_CHUNK_SIZE,
   MS3_OPT_FORCE_LIST_VERSION,
-  MS3_OPT_FORCE_PROTOCOL_VERSION
+  MS3_OPT_FORCE_PROTOCOL_VERSION,
+  MS3_OPT_PORT_NUMBER
 };
 
 typedef enum ms3_set_option_t ms3_set_option_t;
diff --git a/src/marias3.c b/src/marias3.c
index 581b181..cebcbb0 100644
--- a/src/marias3.c
+++ b/src/marias3.c
@@ -166,6 +166,7 @@ ms3_st *ms3_init(const char *s3key, const char *s3secret,
   ms3->s3key = ms3_cstrdup(s3key);
   ms3->s3secret = ms3_cstrdup(s3secret);
   ms3->region = ms3_cstrdup(region);
+  ms3->port = 0; /* The default value */
 
   if (base_domain && strlen(base_domain))
   {
@@ -534,6 +535,19 @@ uint8_t ms3_set_option(ms3_st *ms3, ms3_set_option_t option, void *value)
       break;
     }
 
+    case MS3_OPT_PORT_NUMBER:
+    {
+      int port_number;
+
+      if (!value)
+      {
+        return MS3_ERR_PARAMETER;
+      }
+      memcpy(&port_number, (void*)value, sizeof(int));
+
+      ms3->port = port_number;
+      break;
+    }
     default:
       return MS3_ERR_PARAMETER;
   }
diff --git a/src/request.c b/src/request.c
index 425367b..cd819c2 100644
--- a/src/request.c
+++ b/src/request.c
@@ -51,7 +51,7 @@ static void set_error_nocopy(ms3_st *ms3, char *error)
   ms3->last_error = error;
 }
 
-static uint8_t build_request_uri(CURL *curl, const char *base_domain,
+static uint8_t build_request_uri(CURL *curl, const char *base_domain, int port,
                                  const char *bucket, const char *object, const char *query, bool use_http,
                                  uint8_t protocol_version)
 {
@@ -61,6 +61,14 @@ static uint8_t build_request_uri(CURL *curl, const char *base_domain,
   const char *http_protocol = "http";
   const char *https_protocol = "https";
   const char *protocol;
+  char port_str[32]; // ":portnumber" or empty string
+
+  if (port)
+  {
+    snprintf(port_str, sizeof(port_str) - 1, ":%d", port);
+  }
+  else
+    port_str[0]= 0; // empty string
 
   if (base_domain)
   {
@@ -83,43 +91,43 @@ static uint8_t build_request_uri(CURL *curl, const char *base_domain,
   if (query)
   {
     if (path_parts + strlen(domain) + strlen(bucket) + strlen(object) + strlen(
-          query) >= MAX_URI_LENGTH - 1)
+          query) + strlen(port_str) >= MAX_URI_LENGTH - 1)
     {
       return MS3_ERR_URI_TOO_LONG;
     }
 
     if (protocol_version == 1)
     {
-      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s/%s%s?%s", protocol,
-               domain, bucket,
+      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s%s/%s%s?%s", protocol,
+               domain, port_str, bucket,
                object, query);
     }
     else
     {
-      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s.%s%s?%s", protocol,
-               bucket, domain,
+      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s.%s%s%s?%s", protocol,
+               bucket, domain, port_str,
                object, query);
     }
   }
   else
   {
     if (path_parts + strlen(domain) + strlen(bucket) + strlen(
-          object) >= MAX_URI_LENGTH - 1)
+          object) + strlen(port_str) >= MAX_URI_LENGTH - 1)
     {
       return MS3_ERR_URI_TOO_LONG;
     }
 
     if (protocol_version == 1)
     {
-      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s/%s%s", protocol,
-               domain,
+      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s%s/%s%s", protocol,
+               domain, port_str,
                bucket,
                object);
     }
     else
     {
-      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s.%s%s", protocol,
-               bucket, domain,
+      snprintf(uri_buffer, MAX_URI_LENGTH - 1, "%s://%s.%s%s%s", protocol,
+               bucket, domain, port_str,
                object);
     }
   }
@@ -717,8 +725,8 @@ uint8_t execute_request(ms3_st *ms3, command_t cmd, const char *bucket,
                            ms3->query_buffer);
   }
 
-  res = build_request_uri(curl, ms3->base_domain, bucket, path, query,
-                          ms3->use_http, ms3->protocol_version);
+  res = build_request_uri(curl, ms3->base_domain, ms3->port, bucket, path,
+                          query, ms3->use_http, ms3->protocol_version);
 
   if (res)
   {
diff --git a/src/structs.h b/src/structs.h
index 8d30a57..de80b53 100644
--- a/src/structs.h
+++ b/src/structs.h
@@ -42,6 +42,7 @@ struct ms3_st
   char *s3secret;
   char *region;
   char *base_domain;
+  int port; // 0 means "Use default"
   size_t buffer_chunk_size;
   CURL *curl;
   char *last_error;


More information about the commits mailing list