jsonrpsee_http_server/
response.rs1use jsonrpsee_types::error::reject_too_big_request;
30
31use crate::types::error::{ErrorCode, ErrorResponse};
32use crate::types::Id;
33
34const JSON: &str = "application/json; charset=utf-8";
35const TEXT: &str = "text/plain";
36
37pub fn internal_error() -> hyper::Response<hyper::Body> {
39 let error = serde_json::to_string(&ErrorResponse::borrowed(ErrorCode::InternalError.into(), Id::Null))
40 .expect("built from known-good data; qed");
41
42 from_template(hyper::StatusCode::INTERNAL_SERVER_ERROR, error, JSON)
43}
44
45pub fn host_not_allowed() -> hyper::Response<hyper::Body> {
47 from_template(hyper::StatusCode::FORBIDDEN, "Provided Host header is not whitelisted.\n".to_owned(), TEXT)
48}
49
50pub fn method_not_allowed() -> hyper::Response<hyper::Body> {
52 from_template(
53 hyper::StatusCode::METHOD_NOT_ALLOWED,
54 "Used HTTP Method is not allowed. POST or OPTIONS is required\n".to_owned(),
55 TEXT,
56 )
57}
58
59pub fn invalid_allow_origin() -> hyper::Response<hyper::Body> {
61 from_template(
62 hyper::StatusCode::FORBIDDEN,
63 "Origin of the request is not whitelisted. CORS headers would not be sent and any side-effects were cancelled as well.\n".to_owned(),
64 TEXT,
65 )
66}
67
68pub fn invalid_allow_headers() -> hyper::Response<hyper::Body> {
70 from_template(
71 hyper::StatusCode::FORBIDDEN,
72 "Requested headers are not allowed for CORS. CORS headers would not be sent and any side-effects were cancelled as well.\n".to_owned(),
73 TEXT,
74 )
75}
76
77pub fn too_large(limit: u32) -> hyper::Response<hyper::Body> {
79 let error = serde_json::to_string(&ErrorResponse::borrowed(reject_too_big_request(limit), Id::Null))
80 .expect("built from known-good data; qed");
81
82 from_template(hyper::StatusCode::PAYLOAD_TOO_LARGE, error, JSON)
83}
84
85pub fn malformed() -> hyper::Response<hyper::Body> {
87 let error = serde_json::to_string(&ErrorResponse::borrowed(ErrorCode::ParseError.into(), Id::Null))
88 .expect("built from known-good data; qed");
89
90 from_template(hyper::StatusCode::BAD_REQUEST, error, JSON)
91}
92
93fn from_template<S: Into<hyper::Body>>(
95 status: hyper::StatusCode,
96 body: S,
97 content_type: &'static str,
98) -> hyper::Response<hyper::Body> {
99 hyper::Response::builder()
100 .status(status)
101 .header("content-type", hyper::header::HeaderValue::from_static(content_type))
102 .body(body.into())
103 .expect("Unable to parse response body for type conversion")
106}
107
108pub fn ok_response(body: String) -> hyper::Response<hyper::Body> {
110 from_template(hyper::StatusCode::OK, body, JSON)
111}
112
113pub fn unsupported_content_type() -> hyper::Response<hyper::Body> {
115 from_template(
116 hyper::StatusCode::UNSUPPORTED_MEDIA_TYPE,
117 "Supplied content type is not allowed. Content-Type: application/json is required\n".to_owned(),
118 TEXT,
119 )
120}