Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix error message when sent HTTP GET to RPC #4314

Merged
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 21 additions & 4 deletions rpc/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::IoHandler;
use axum::routing::post;
use axum::response::IntoResponse;
use axum::routing::{get, post};
use axum::{Extension, Router};
use ckb_app_config::RpcConfig;
use ckb_async_runtime::Handle;
use ckb_error::AnyError;
use ckb_logger::info;

use axum::http::StatusCode;
use ckb_stop_handler::{new_tokio_exit_rx, CancellationToken};
use futures_util::{SinkExt, TryStreamExt};
use jsonrpc_core::MetaIoHandler;
Expand Down Expand Up @@ -90,14 +92,21 @@ impl RpcServer {
.with_pipeline_size(4);

// HTTP and WS server.
let method_router =
post(handle_jsonrpc::<Option<Session>>).get(handle_jsonrpc_ws::<Option<Session>>);
let post_router = post(handle_jsonrpc::<Option<Session>>);
let get_router = if enable_websocket {
get(handle_jsonrpc_ws::<Option<Session>>)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If enable_websocket is set to true, will the client side receive the error message Used HTTP Method is not allowed. POST or OPTIONS is required when sending a GET HTTP request?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, it's only for HTTP endpoint.
but the error message for Websocket still have a difference, for request like this send to port 28114 (Websocket port):

curl -s -X GET http://127.0.0.1:28114  -H 'Content-Type: application/json' -d '{ "id": 42,   "jsonrpc": "2.0", "method": "get_raw_tx_pool", "params": [] }

the old:

WebSocket Protocol Error: Unable to parse WebSocket key.%

now:

Connection header did not include 'upgrade'%

I think it's not necessary to fix it.

} else {
get(get_error_handler)
};
let method_router = post_router.merge(get_router);

let mut app = Router::new()
.route("/", method_router.clone())
.route("/*path", method_router)
.layer(Extension(Arc::clone(rpc)))
.layer(CorsLayer::permissive())
.layer(TimeoutLayer::new(Duration::from_secs(30)));
.layer(TimeoutLayer::new(Duration::from_secs(30)))
.layer(Extension(stream_config.clone()));

if enable_websocket {
let ws_config: StreamServerConfig =
Expand Down Expand Up @@ -179,3 +188,11 @@ impl RpcServer {
Ok(tcp_address)
}
}

/// used for compatible with old PRC error responce for GET
async fn get_error_handler() -> impl IntoResponse {
(
StatusCode::METHOD_NOT_ALLOWED,
"Used HTTP Method is not allowed. POST or OPTIONS is required",
)
}
Loading