blob: 616264d3d75e480aea750a4bfbc9fadbb1783dd8 [file] [log] [blame]
Alex Vakulenko039da312015-02-03 08:58:55 -08001// Copyright 2014 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef WEBSERVER_LIBWEBSERV_RESPONSE_H_
6#define WEBSERVER_LIBWEBSERV_RESPONSE_H_
7
8#include <map>
9#include <memory>
10#include <string>
11#include <utility>
12#include <vector>
13
14#include <base/macros.h>
Alex Vakulenko039da312015-02-03 08:58:55 -080015#include <libwebserv/export.h>
16
17namespace base {
18class Value;
19} // namespace base
20
Alex Vakulenko039da312015-02-03 08:58:55 -080021namespace libwebserv {
22
Alex Vakulenko31a63792015-02-03 12:44:57 -080023class ProtocolHandler;
Alex Vakulenko039da312015-02-03 08:58:55 -080024
25// Response class is a proxy for HTTP response used by the request handler
26// to provide response HTTP headers and data.
Alex Vakulenko31a63792015-02-03 12:44:57 -080027class LIBWEBSERV_EXPORT Response final {
Alex Vakulenko039da312015-02-03 08:58:55 -080028 public:
29 ~Response();
30
Alex Vakulenko039da312015-02-03 08:58:55 -080031 // Adds a single HTTP response header to the response.
32 void AddHeader(const std::string& header_name, const std::string& value);
33
34 // Adds number of HTTP response headers to the response.
35 void AddHeaders(
36 const std::vector<std::pair<std::string, std::string>>& headers);
37
38 // Generic reply method for sending arbitrary binary data response.
39 void Reply(int status_code,
40 const void* data,
41 size_t data_size,
42 const std::string& mime_type);
43
44 // Reply with text body.
45 void ReplyWithText(int status_code,
46 const std::string& text,
47 const std::string& mime_type);
48
49 // Reply with JSON object. The content type will be "application/json".
50 void ReplyWithJson(int status_code, const base::Value* json);
51
52 // Special form for JSON response for simple objects that have a flat
53 // list of key-value pairs of string type.
54 void ReplyWithJson(int status_code,
55 const std::map<std::string, std::string>& json);
56
57 // Issue a redirect response, so the client browser loads a page at
58 // the URL specified in |redirect_url|. If this is not an external URL,
59 // it must be an absolute path starting at the root "/...".
60 void Redirect(int status_code, const std::string& redirect_url);
61
62 // Send a plain text response (with no Content-Type header).
63 // Usually used with error responses. |error_text| must be plain text.
64 void ReplyWithError(int status_code, const std::string& error_text);
65
66 // Send "404 Not Found" response.
67 void ReplyWithErrorNotFound();
68
69 private:
Alex Vakulenko31a63792015-02-03 12:44:57 -080070 friend class ProtocolHandler;
71
72 LIBWEBSERV_PRIVATE Response(ProtocolHandler* handler,
73 const std::string& request_id);
Alex Vakulenko039da312015-02-03 08:58:55 -080074
75 LIBWEBSERV_PRIVATE void SendResponse();
76
Alex Vakulenko31a63792015-02-03 12:44:57 -080077 ProtocolHandler* handler_{nullptr};
78 std::string request_id_;
Alex Vakulenko039da312015-02-03 08:58:55 -080079 int status_code_{0};
80 std::vector<uint8_t> data_;
81 std::multimap<std::string, std::string> headers_;
82 bool reply_sent_{false};
83
Alex Vakulenko039da312015-02-03 08:58:55 -080084 DISALLOW_COPY_AND_ASSIGN(Response);
85};
86
87} // namespace libwebserv
88
89#endif // WEBSERVER_LIBWEBSERV_RESPONSE_H_