Debian Codegraph @

src/tools/davix_tool_main.cpp

raw
1 /*
2 * This File is part of Davix, The IO library for HTTP based protocols
3 * Copyright (C) CERN 2013
4 * Author: Adrien Devresse <[email protected]>
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 *
20 */
21
22 #include <davix_internal.hpp>
23 #include <davix.hpp>
24 #include <tools/davix_tool_params.hpp>
25 #include <tools/davix_tool_util.hpp>
26 #include <cstdio>
27
28
29 // @author : Devresse Adrien
30 // main file for davix low level cmd line tool
31
32
33 using namespace Davix;
34 using namespace std;
35
36 #define READ_BLOCK_SIZE 4096
37
38
39 const std::string scope_main = "Davix::Tools::davix";
40
41
42 static void configureRequest(HttpRequest& req, Tool::OptParams & opts){
43 req.setParameters(opts.params);
44 if(opts.req_type.empty() == false)
45 req.setRequestMethod(opts.req_type);
46
47 req.setRequestBody(opts.req_content);
48
49 }
50
51 static std::string get_request_options(){
52 return " Request Options: \n"
53 "\t--data Content of the request\n"
54 "\t--request, -X: Request operation to use (ex : GET, PUT, PROPFIND, etc..)\n"
55 "\n"
56 ;
57 }
58
59
60 static std::string help_msg(const std::string & cmd_path){
61 std::string help_msg = fmt::format("Usage : {} ", cmd_path);
62 help_msg += Tool::get_base_description_options();
63 help_msg += Tool::get_common_options();
64 help_msg += get_request_options();
65
66 return help_msg;
67 }
68
69
70 static int read_stream(HttpRequest* req, int fd_out, DavixError** err){
71
72 if(req->beginRequest(err) <0
73 || req->readToFd(fd_out, err) < 0)
74 return -1;
75 return req->endRequest(err);
76 }
77
78 int main(int argc, char** argv){
79 int retcode=-1;
80 Tool::OptParams opts;
81 DavixError* tmp_err=NULL;
82 opts.help_msg = help_msg(argv[0]);
83 int out_fd;
84
85 if( (retcode= Tool::parse_davix_options(argc, argv, opts, &tmp_err)) ==0){
86 Context c;
87 configureContext(c, opts);
88 if( (out_fd= Tool::getOutFd(opts, scope_main, &tmp_err)) > 0
89 && (retcode = Tool::configureAuth(opts)) == 0){
90
91 HttpRequest req(c, opts.vec_arg[0], &tmp_err);
92 if( tmp_err == NULL){
93 configureRequest(req, opts);
94 retcode= read_stream(&req, out_fd, &tmp_err);
95 Tool::flushFinalLineShell(out_fd);
96 close(out_fd);
97 }else{
98 retcode =-1;
99 }
100 }
101 }
102 Tool::errorPrint(&tmp_err);
103 return retcode;
104 }
105
106
107
108
109
110
111