Debian Codegraph @

src/fileops/fileutils.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 "fileutils.hpp"
24
25 namespace Davix {
26
27
28 const std::string ans_header_byte_range("Content-Range");
29 const std::string ans_header_content_type("Content-Type");
30 const std::string ans_header_multi_part_value("multipart");
31 const std::string ans_header_boundary_field("boundary=");
32 const std::string ans_header_content_length("Content-Length");
33 const std::string offset_value("bytes=");
34 const std::string req_header_byte_range("Range");
35
36
37 int davixRequestToFileStatus(HttpRequest* req, const std::string & scope, DavixError** err){
38 const int reqcode = req->getRequestCode();
39 int ret = 0;
40 if( httpcodeIsValid(reqcode) == false){
41 DavixError* tmp_err=NULL;
42 httpcodeToDavixError(reqcode, scope, "",&tmp_err);
43 if(tmp_err && tmp_err->getStatus() != StatusCode::OK){
44 DavixError::propagateError(err, tmp_err);
45 ret = -1;
46 }else{
47 DavixError::clearError(&tmp_err);
48 }
49 }
50 return ret;
51 }
52
53
54 void check_file_status(HttpRequest & req, const std::string & scope){
55 const int code =req.getRequestCode();
56 if( httpcodeIsValid(code) == false){
57 httpcodeToDavixException(code, scope);
58 }
59 }
60
61 void setup_offset_request(HttpRequest* req, const dav_off_t *start_len, const dav_size_t *size_read, const dav_size_t number_ops){
62 std::ostringstream buffer;
63
64 buffer << offset_value;
65
66 for(size_t i = 0; i<number_ops; ++i){
67 if( i > 0)
68 buffer << ",";
69
70 if(size_read[i] > 0)
71 buffer << start_len[i] << "-"<< (start_len[i]+size_read[i]-1);
72 else
73 buffer << start_len [i]<< "-";
74 }
75 req->addHeaderField(req_header_byte_range, buffer.str());
76
77 }
78
79 std::vector< std::pair<dav_size_t, std::string> > generateRangeHeaders(dav_size_t max_header_size, OffsetCallback & offset_provider){
80 std::vector< std::pair<dav_size_t, std::string> > range_rec;
81 dav_off_t begin, end;
82 int ret;
83 std::string range_string;
84 dav_size_t range_size =0;
85 std::ostringstream buffer;
86 range_string.reserve(max_header_size);
87 range_string.append(offset_value);
88
89
90 while( ( ret = offset_provider(begin, end)) >= 0){
91 buffer.str("");
92
93 /* First range? Don't add a coma */
94 if(range_string.size() != offset_value.size())
95 buffer << ",";
96 buffer << begin << '-' << end;
97
98 range_string.append(buffer.str());
99 range_size++;
100 if(range_string.size() >= max_header_size){
101 range_rec.push_back(std::make_pair(range_size, range_string));
102 range_size = 0;
103 range_string.assign(offset_value);
104 }
105 }
106 if(range_size > 0)
107 range_rec.push_back(std::make_pair(range_size, range_string));
108
109 return range_rec;
110 }
111
112
113
114
115
116 } // namespace Davix