Complete local HTTP endpoint parity
This commit is contained in:
1 parent
a1761fa731
commit
668d8b787e
13 files changed
+493
-69
No files matched your search
+33
-10
@@ -1,10 +1,16 @@
|
||||
use super::*;
|
||||
|
||||
pub(super) fn send_sse_headers(stream: &mut impl Write) -> Result<(), String> {
|
||||
pub(super) fn send_sse_headers_with_cors(
|
||||
stream: &mut impl Write,
|
||||
cors: bool,
|
||||
) -> Result<(), String> {
|
||||
let cors = if cors {
|
||||
"Access-Control-Allow-Origin: *\r\nAccess-Control-Allow-Methods: GET, POST, OPTIONS\r\nAccess-Control-Allow-Headers: *\r\n"
|
||||
} else {
|
||||
""
|
||||
};
|
||||
stream
|
||||
.write_all(
|
||||
b"HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\nConnection: close\r\n\r\n",
|
||||
)
|
||||
.write_all(format!("HTTP/1.1 200 OK\r\nContent-Type: text/event-stream\r\nCache-Control: no-cache\r\n{cors}Connection: close\r\n\r\n").as_bytes())
|
||||
.map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
@@ -27,25 +33,37 @@ pub(super) fn send_sse_error(stream: &mut impl Write, message: &str) -> Result<(
|
||||
stream.write_all(b"\n\n").map_err(|error| error.to_string())
|
||||
}
|
||||
|
||||
pub(super) fn send_json(stream: &mut TcpStream, code: u16, value: &Value) -> Result<(), String> {
|
||||
pub(super) fn send_json_with_cors(
|
||||
stream: &mut TcpStream,
|
||||
code: u16,
|
||||
value: &Value,
|
||||
cors: bool,
|
||||
) -> Result<(), String> {
|
||||
let mut body = serde_json::to_vec(value).map_err(|error| error.to_string())?;
|
||||
body.push(b'\n');
|
||||
send_response(stream, code, Some("application/json"), &body)
|
||||
send_response_with_cors(stream, code, Some("application/json"), &body, cors)
|
||||
}
|
||||
|
||||
pub(super) fn send_error(stream: &mut TcpStream, code: u16, message: &str) -> Result<(), String> {
|
||||
send_json(
|
||||
pub(super) fn send_error_with_cors(
|
||||
stream: &mut TcpStream,
|
||||
code: u16,
|
||||
message: &str,
|
||||
cors: bool,
|
||||
) -> Result<(), String> {
|
||||
send_json_with_cors(
|
||||
stream,
|
||||
code,
|
||||
&json!({"error": {"message": message, "type": "invalid_request_error"}}),
|
||||
cors,
|
||||
)
|
||||
}
|
||||
|
||||
pub(super) fn send_response(
|
||||
stream: &mut TcpStream,
|
||||
pub(super) fn send_response_with_cors(
|
||||
stream: &mut impl Write,
|
||||
code: u16,
|
||||
content_type: Option<&str>,
|
||||
body: &[u8],
|
||||
cors: bool,
|
||||
) -> Result<(), String> {
|
||||
let reason = match code {
|
||||
200 => "OK",
|
||||
@@ -65,6 +83,11 @@ pub(super) fn send_response(
|
||||
header.push_str(content_type);
|
||||
header.push_str("\r\n");
|
||||
}
|
||||
if cors {
|
||||
header.push_str("Access-Control-Allow-Origin: *\r\n");
|
||||
header.push_str("Access-Control-Allow-Methods: GET, POST, OPTIONS\r\n");
|
||||
header.push_str("Access-Control-Allow-Headers: *\r\n");
|
||||
}
|
||||
header.push_str("Connection: close\r\n\r\n");
|
||||
stream
|
||||
.write_all(header.as_bytes())
|
||||
|
||||
Reference in new issue
Block a user