File: status.hpp

package info (click to toggle)
libzeep 7.3.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: cpp: 17,430; javascript: 180; makefile: 12; sh: 11
file content (156 lines) | stat: -rw-r--r-- 4,093 bytes parent folder | download
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
//          Copyright Maarten L. Hekkelman 2026
// Distributed under the Boost Software License, Version 1.0.
//    (See accompanying file LICENSE_1_0.txt or copy at
//          http://www.boost.org/LICENSE_1_0.txt)

#pragma once

#include "zeep/exception.hpp"

#include <cassert>
#include <string>
#include <system_error>

namespace zeep::http
{

/// Various predefined HTTP status codes

enum status_type
{
	cont = 100,
	ok = 200,
	created = 201,
	accepted = 202,
	no_content = 204,
	multiple_choices = 300,
	moved_permanently = 301,
	moved_temporarily = 302,
	see_other = 303,
	not_modified = 304,
	bad_request = 400,
	unauthorized = 401,
	forbidden = 403,
	not_found = 404,
	method_not_allowed = 405,
	unprocessable_entity = 422,
	proxy_authentication_required = 407,
	internal_server_error = 500,
	not_implemented = 501,
	bad_gateway = 502,
	service_unavailable = 503
};

/**
 * @brief The implementation for @ref config_category error messages
 *
 */
class status_type_impl : public std::error_category
{
  public:
	/**
	 * @brief User friendly name
	 *
	 * @return const char*
	 */

	[[nodiscard]] const char *name() const noexcept override
	{
		return "http status";
	}

	/**
	 * @brief Provide the error message as a string for the error code @a ev
	 *
	 * @param ev The error code
	 * @return std::string
	 */

	[[nodiscard]] std::string message(int ev) const override
	{
		switch (static_cast<status_type>(ev))
		{
			case status_type::cont: return "Continue";
			case status_type::ok: return "OK";
			case status_type::created: return "Created";
			case status_type::accepted: return "Accepted";
			case status_type::no_content: return "No Content";
			case status_type::multiple_choices: return "Multiple Choices";
			case status_type::moved_permanently: return "Moved Permanently";
			case status_type::moved_temporarily: return "Found";
			case status_type::see_other: return "See Other";
			case status_type::not_modified: return "Not Modified";
			case status_type::bad_request: return "Bad Request";
			case status_type::unauthorized: return "Unauthorized";
			case status_type::proxy_authentication_required: return "Proxy Authentication Required";
			case status_type::forbidden: return "Forbidden";
			case status_type::not_found: return "Not Found";
			case status_type::method_not_allowed: return "Method not allowed";
			case status_type::unprocessable_entity: return "Unprocessable Entity";
			case status_type::internal_server_error: return "Internal Server Error";
			case status_type::not_implemented: return "Not Implemented";
			case status_type::bad_gateway: return "Bad Gateway";
			case status_type::service_unavailable: return "Service Unavailable";
			default: return "unknown status code";
		}
	}

	/**
	 * @brief Return whether two error codes are equivalent, always false in this case
	 *
	 */

	[[nodiscard]] bool equivalent(const std::error_code & /*code*/, int /*condition*/) const noexcept override
	{
		return false;
	}
};

/**
 * @brief Return the implementation for the config_category
 *
 * @return std::error_category&
 */
inline std::error_category &status_type_category()
{
	static status_type_impl instance;
	return instance;
}

inline std::error_code make_error_code(status_type e)
{
	return { static_cast<int>(e), status_type_category() };
}

inline std::error_condition make_error_condition(status_type e)
{
	return { static_cast<int>(e), status_type_category() };
}

/// Return the string describing the status_type in more detail
std::string get_status_description(status_type status);

// http exception

class http_status_exception : public exception
{
  public:
	http_status_exception(std::error_code ec) noexcept
		: exception(ec.message())
		, m_code(ec)
	{
	}

	http_status_exception(status_type status) noexcept
		: zeep::http::http_status_exception(make_error_code(status))
	{
	}

	[[nodiscard]] const std::error_code &code() const noexcept { return m_code; }
	[[nodiscard]] status_type status() const noexcept { return static_cast<status_type>(m_code.value()); }

  private:
	std::error_code m_code;
};

} // namespace zeep::http