forked from liutongxuan/seastar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfile_handler.hh
165 lines (141 loc) · 4.78 KB
/
file_handler.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* This file is open source software, licensed to you under the terms
* of the Apache License, Version 2.0 (the "License"). See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. You may not use this file except in compliance with the License.
*
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Copyright 2015 Cloudius Systems
*/
#ifndef HTTP_FILE_HANDLER_HH_
#define HTTP_FILE_HANDLER_HH_
#include "handlers.hh"
namespace seastar {
namespace httpd {
/**
* This is a base class for file transformer.
*
* File transformer adds the ability to modify a file content before returning
* the results.
*
* The transformer decides according to the file extension if transforming is
* needed.
*/
class file_transformer {
public:
/**
* Any file transformer should implement this method.
* @param content the content to transform
* @param req the request
* @param extension the file extension originating the content
*/
virtual void transform(sstring& content, const request& req,
const sstring& extension) = 0;
virtual ~file_transformer() = default;
};
/**
* A base class for handlers that interact with files.
* directory and file handlers both share some common logic
* with regards to file handling.
* they both needs to read a file from the disk, optionally transform it,
* and return the result or page not found on error
*/
class file_interaction_handler : public handler_base {
public:
file_interaction_handler(file_transformer* p = nullptr)
: transformer(p) {
}
~file_interaction_handler();
/**
* Allows setting a transformer to be used with the files returned.
* @param t the file transformer to use
* @return this
*/
file_interaction_handler* set_transformer(file_transformer* t) {
transformer = t;
return this;
}
/**
* if the url ends without a slash redirect
* @param req the request
* @param rep the reply
* @return true on redirect
*/
bool redirect_if_needed(const request& req, reply& rep) const;
/**
* A helper method that returns the file extension.
* @param file the file to check
* @return the file extension
*/
static sstring get_extension(const sstring& file);
protected:
/**
* read a file from the disk and return it in the replay.
* @param file the full path to a file on the disk
* @param req the reuest
* @param rep the reply
*/
future<std::unique_ptr<reply> > read(const sstring& file,
std::unique_ptr<request> req, std::unique_ptr<reply> rep);
file_transformer* transformer;
};
/**
* The directory handler get a disk path in the
* constructor.
* and expect a path parameter in the handle method.
* it would concatenate the two and return the file
* e.g. if the path is /usr/mgmt/public in the path
* parameter is index.html
* handle will return the content of /usr/mgmt/public/index.html
*/
class directory_handler : public file_interaction_handler {
public:
/**
* The directory handler map a base path and a path parameter to a file
* @param doc_root the root directory to search the file from.
* For example if the root is '/usr/mgmt/public' and the path parameter
* will be '/css/style.css' the file wil be /usr/mgmt/public/css/style.css'
*/
explicit directory_handler(const sstring& doc_root,
file_transformer* transformer = nullptr);
future<std::unique_ptr<reply>> handle(const sstring& path,
std::unique_ptr<request> req, std::unique_ptr<reply> rep) override;
private:
sstring doc_root;
};
/**
* The file handler get a path to a file on the disk
* in the constructor.
* it will always return the content of the file.
*/
class file_handler : public file_interaction_handler {
public:
/**
* The file handler map a file to a url
* @param file the full path to the file on the disk
*/
explicit file_handler(const sstring& file, file_transformer* transformer =
nullptr, bool force_path = true)
: file_interaction_handler(transformer), file(file), force_path(
force_path) {
}
future<std::unique_ptr<reply>> handle(const sstring& path,
std::unique_ptr<request> req, std::unique_ptr<reply> rep) override;
private:
sstring file;
bool force_path;
};
}
}
#endif /* HTTP_FILE_HANDLER_HH_ */