Skip to content

Commit b92b86b

Browse files
authored
Add CONTENT-TYPE header to server response (#31)
1 parent afe51a0 commit b92b86b

File tree

4 files changed

+19
-18
lines changed

4 files changed

+19
-18
lines changed

examples/helloworld/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ name = "server"
1313
path = "src/server.rs"
1414

1515
[dependencies]
16-
ntex = "3.0.0-pre.2"
16+
ntex = { version = "3.0.0-pre.9", features = ["tokio"] }
1717
ntex-grpc = "1"
1818
ntex-h2 = "3"
1919

@@ -23,4 +23,4 @@ env_logger = "0.11"
2323
num_cpus = "1"
2424

2525
[dev-dependencies]
26-
ntex = { version = "3.0.0-pre.2", features = ["openssl", "tokio"] }
26+
ntex = { version = "3.0.0-pre.9", features = ["openssl", "tokio"] }

ntex-grpc/CHANGES.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changes
22

3+
## [1.2.0] - 2025-12-22
4+
5+
* Add "CONTENT-TYPE" header to server response
6+
37
## [1.1.0] - 2025-12-17
48

59
* Upgrade to ntex-service v4

ntex-grpc/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ntex-grpc"
3-
version = "1.1.0"
3+
version = "1.2.0"
44
license = "MIT OR Apache-2.0"
55
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
66
description = "GRPC Client/Server framework"

ntex-grpc/src/server/service.rs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{cell::RefCell, rc::Rc};
22

33
use ntex_bytes::{Buf, BufMut, ByteString, BytesMut};
44
use ntex_h2::{self as h2, StreamRef, frame::Reason, frame::StreamId};
5-
use ntex_http::{HeaderMap, HeaderValue, StatusCode};
5+
use ntex_http::{HeaderMap, HeaderValue, StatusCode, header::CONTENT_TYPE};
66
use ntex_io::{Filter, Io, IoBoxed};
77
use ntex_service::{Service, ServiceCtx, ServiceFactory, cfg::SharedCfg};
88
use ntex_util::{HashMap, time::Millis, time::timeout_checked};
@@ -18,6 +18,7 @@ const ERR_DATA_DECODE: HeaderValue =
1818
const ERR_DECODE_TIMEOUT: HeaderValue =
1919
HeaderValue::from_static("Cannot decode grpc-timeout header");
2020
const ERR_DEADLINE: HeaderValue = HeaderValue::from_static("Deadline exceeded");
21+
const HDR_APP_GRPC: HeaderValue = HeaderValue::from_static("application/grpc");
2122

2223
const MILLIS_IN_HOUR: u64 = 60 * 60 * 1000;
2324
const MILLIS_IN_MINUTE: u64 = 60 * 1000;
@@ -187,17 +188,13 @@ where
187188
path.split_to(n)
188189
} else {
189190
// not found
190-
let _ =
191-
stream.send_response(StatusCode::NOT_FOUND, HeaderMap::default(), true);
191+
let _ = stream.send_response(StatusCode::NOT_FOUND, hdrs(), true);
192192
return Ok(());
193193
};
194194

195195
// stream eof, cannot do anything
196196
if eof {
197-
if stream
198-
.send_response(StatusCode::OK, HeaderMap::default(), false)
199-
.is_ok()
200-
{
197+
if stream.send_response(StatusCode::OK, hdrs(), false).is_ok() {
201198
send_error(&stream, GrpcStatus::InvalidArgument, ERR_DECODE);
202199
}
203200
return Ok(());
@@ -241,10 +238,7 @@ where
241238
let _compressed = data.get_u8();
242239
let len = data.get_u32();
243240
if (len as usize) > data.len() {
244-
if stream
245-
.send_response(StatusCode::OK, HeaderMap::default(), false)
246-
.is_ok()
247-
{
241+
if stream.send_response(StatusCode::OK, hdrs(), false).is_ok() {
248242
send_error(&stream, GrpcStatus::InvalidArgument, ERR_DATA_DECODE);
249243
}
250244
return Ok(());
@@ -264,10 +258,7 @@ where
264258
name: inflight.name,
265259
headers: inflight.headers,
266260
};
267-
if stream
268-
.send_response(StatusCode::OK, HeaderMap::default(), false)
269-
.is_err()
270-
{
261+
if stream.send_response(StatusCode::OK, hdrs(), false).is_err() {
271262
return Ok(());
272263
}
273264
drop(streams);
@@ -333,6 +324,12 @@ where
333324
}
334325
}
335326

327+
fn hdrs() -> HeaderMap {
328+
let mut hdrs = HeaderMap::default();
329+
hdrs.insert(CONTENT_TYPE, HDR_APP_GRPC);
330+
hdrs
331+
}
332+
336333
fn send_error(stream: &StreamRef, st: GrpcStatus, msg: HeaderValue) {
337334
let mut trailers = HeaderMap::default();
338335
trailers.insert(consts::GRPC_STATUS, st.into());

0 commit comments

Comments
 (0)