From 48f446a93883fc615d23ac1148fd4b082aed9c86 Mon Sep 17 00:00:00 2001
From: tong li <litong01@us.ibm.com>
Date: Mon, 18 Mar 2013 09:47:59 -0400
Subject: [PATCH] Allow user to specify headers at the command line.

Currently swift client does not allow content type to be specified
when upload an object. It does not allow a user to change content
type neither. This patch will allow a user to specify content
type by using upload command or modify the content type by using
post command.

For example:
1. To upload an object with specific content type:
swift upload -H content-type:applicaiton/xml con1 file1

2. To modify an object to a specific content type:
swift post -H content-type:application/josn con1 file1

This patch also allow a client to specify other request headers.

Change-Id: I12db83a1d465285e0906889cc67dfaa44e059568
Fixes: bug #1154621
---
 bin/swift | 16 +++++++++++++++-
 1 file changed, 15 insertions(+), 1 deletion(-)

diff --git a/bin/swift b/bin/swift
index fd26fd3f..5b6dfd4e 100755
--- a/bin/swift
+++ b/bin/swift
@@ -701,6 +701,10 @@ def st_post(parser, args, print_queue, error_queue):
     parser.add_option('-m', '--meta', action='append', dest='meta', default=[],
         help='Sets a meta data item with the syntax name:value. This option '
         'may be repeated. Example: -m Color:Blue -m Size:Large')
+    parser.add_option('-H', '--header', action='append', dest='header',
+        default=[], help='Set request headers with the syntax header:value. '
+        ' This option may be repeated. Example -H content-type:text/plain '
+        '-H "Content-Length: 4000"')
     (options, args) = parse_args(parser, args)
     args = args[1:]
     if (options.read_acl or options.write_acl or options.sync_to or
@@ -737,6 +741,8 @@ def st_post(parser, args, print_queue, error_queue):
             conn.put_container(args[0], headers=headers)
     elif len(args) == 2:
         headers = split_headers(options.meta, 'X-Object-Meta-', error_queue)
+        # add header options to the headers object for the request.
+        headers.update(split_headers(options.header, '', error_queue))
         try:
             conn.post_object(args[0], args[1], headers=headers)
         except ClientException, err:
@@ -783,6 +789,11 @@ def st_upload(parser, args, print_queue, error_queue):
     parser.add_option('', '--segment-threads', type=int,
                       default=10, help='Number of threads to use for '
                       'uploading object segments')
+    parser.add_option('-H', '--header', action='append', dest='header',
+        default=[], help='Set request headers with the syntax header:value. '
+        ' This option may be repeated. Example -H content-type:text/plain '
+        '-H "Content-Length: 4000"')
+
     (options, args) = parse_args(parser, args)
     args = args[1:]
     if len(args) < 2:
@@ -857,6 +868,9 @@ def st_upload(parser, args, print_queue, error_queue):
                     except ClientException, err:
                         if err.http_status != 404:
                             raise
+                # Merge the command line header options to the put_headers
+                put_headers.update(split_headers(options.header, '',
+                                                 error_queue))
                 # Don't do segment job if object is not big enough
                 if options.segment_size and \
                         getsize(path) > int(options.segment_size):
@@ -1011,7 +1025,7 @@ def split_headers(options, prefix='', error_queue=None):
     for item in options:
         split_item = item.split(':', 1)
         if len(split_item) == 2:
-            headers[prefix + split_item[0]] = split_item[1]
+            headers[(prefix + split_item[0]).title()] = split_item[1]
         else:
             error_string = "Metadata parameter %s must contain a ':'.\n%s" \
                            % (item, st_post_help)