Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / rgw / rgw_loadgen.cc
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
2 // vim: ts=8 sw=2 smarttab
3
4 #include <algorithm>
5 #include <sstream>
6 #include <string.h>
7
8 #include "rgw_loadgen.h"
9 #include "rgw_auth_s3.h"
10
11
12 #define dout_subsys ceph_subsys_rgw
13
14 void RGWLoadGenRequestEnv::set_date(utime_t& tm)
15 {
16   date_str = rgw_to_asctime(tm);
17 }
18
19 int RGWLoadGenRequestEnv::sign(RGWAccessKey& access_key)
20 {
21   map<string, string> meta_map;
22   map<string, string> sub_resources;
23
24   string canonical_header;
25   string digest;
26
27   rgw_create_s3_canonical_header(request_method.c_str(),
28                                  nullptr, /* const char *content_md5 */
29                                  content_type.c_str(),
30                                  date_str.c_str(),
31                                  meta_map,
32                                  uri.c_str(),
33                                  sub_resources,
34                                  canonical_header);
35
36   headers["HTTP_DATE"] = date_str;
37   try {
38     /* FIXME(rzarzynski): kill the dependency on g_ceph_context. */
39     const auto signature = static_cast<std::string>(
40       rgw::auth::s3::get_v2_signature(g_ceph_context, canonical_header,
41                                       access_key.key));
42     headers["HTTP_AUTHORIZATION"] = \
43       std::string("AWS ") + access_key.id + ":" + signature;
44   } catch (int ret) {
45     return ret;
46   }
47
48   return 0;
49 }
50
51 size_t RGWLoadGenIO::write_data(const char* const buf,
52                                 const size_t len)
53 {
54   return len;
55 }
56
57 size_t RGWLoadGenIO::read_data(char* const buf, const size_t len)
58 {
59   const size_t read_len = std::min(left_to_read,
60                                    static_cast<uint64_t>(len));
61   left_to_read -= read_len;
62   return read_len;
63 }
64
65 void RGWLoadGenIO::flush()
66 {
67 }
68
69 size_t RGWLoadGenIO::complete_request()
70 {
71   return 0;
72 }
73
74 void RGWLoadGenIO::init_env(CephContext *cct)
75 {
76   env.init(cct);
77
78   left_to_read = req->content_length;
79
80   char buf[32];
81   snprintf(buf, sizeof(buf), "%lld", (long long)req->content_length);
82   env.set("CONTENT_LENGTH", buf);
83
84   env.set("CONTENT_TYPE", req->content_type.c_str());
85   env.set("HTTP_DATE", req->date_str.c_str());
86
87   for (map<string, string>::iterator iter = req->headers.begin(); iter != req->headers.end(); ++iter) {
88     env.set(iter->first.c_str(), iter->second.c_str());
89   }
90
91   env.set("REQUEST_METHOD", req->request_method.c_str());
92   env.set("REQUEST_URI", req->uri.c_str());
93   env.set("QUERY_STRING", req->query_string.c_str());
94   env.set("SCRIPT_URI", req->uri.c_str());
95
96   char port_buf[16];
97   snprintf(port_buf, sizeof(port_buf), "%d", req->port);
98   env.set("SERVER_PORT", port_buf);
99 }
100
101 size_t RGWLoadGenIO::send_status(const int status,
102                                  const char* const status_name)
103 {
104   return 0;
105 }
106
107 size_t RGWLoadGenIO::send_100_continue()
108 {
109   return 0;
110 }
111
112 size_t RGWLoadGenIO::send_header(const boost::string_ref& name,
113                                  const boost::string_ref& value)
114 {
115   return 0;
116 }
117
118 size_t RGWLoadGenIO::complete_header()
119 {
120   return 0;
121 }
122
123 size_t RGWLoadGenIO::send_content_length(const uint64_t len)
124 {
125   return 0;
126 }