Commit 88ad7672 authored by gejun's avatar gejun

Not set content-length in h2 & change default content-type of h2 to proto

parent 4a0927af
...@@ -28,7 +28,6 @@ DEFINE_string(load_balancer, "", "The algorithm for load balancing"); ...@@ -28,7 +28,6 @@ DEFINE_string(load_balancer, "", "The algorithm for load balancing");
DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds"); DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)"); DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests"); DEFINE_int32(interval_ms, 1000, "Milliseconds between consecutive requests");
DEFINE_string(http_content_type, "application/json", "Content type of http request");
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
// Parse gflags. We recommend you to use gflags as well. // Parse gflags. We recommend you to use gflags as well.
...@@ -69,8 +68,6 @@ int main(int argc, char* argv[]) { ...@@ -69,8 +68,6 @@ int main(int argc, char* argv[]) {
// Set attachment which is wired to network directly instead of // Set attachment which is wired to network directly instead of
// being serialized into protobuf messages. // being serialized into protobuf messages.
cntl.request_attachment().append(FLAGS_attachment); cntl.request_attachment().append(FLAGS_attachment);
} else {
cntl.http_request().set_content_type(FLAGS_http_content_type);
} }
// Because `done'(last parameter) is NULL, this function waits until // Because `done'(last parameter) is NULL, this function waits until
......
...@@ -35,7 +35,6 @@ DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)"); ...@@ -35,7 +35,6 @@ DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_bool(dont_fail, false, "Print fatal when some call failed"); DEFINE_bool(dont_fail, false, "Print fatal when some call failed");
DEFINE_bool(enable_ssl, false, "Use SSL connection"); DEFINE_bool(enable_ssl, false, "Use SSL connection");
DEFINE_int32(dummy_port, -1, "Launch dummy server at this port"); DEFINE_int32(dummy_port, -1, "Launch dummy server at this port");
DEFINE_string(http_content_type, "application/json", "Content type of http request");
std::string g_request; std::string g_request;
std::string g_attachment; std::string g_attachment;
...@@ -62,8 +61,6 @@ static void* sender(void* arg) { ...@@ -62,8 +61,6 @@ static void* sender(void* arg) {
// Set attachment which is wired to network directly instead of // Set attachment which is wired to network directly instead of
// being serialized into protobuf messages. // being serialized into protobuf messages.
cntl.request_attachment().append(g_attachment); cntl.request_attachment().append(g_attachment);
} else {
cntl.http_request().set_content_type(FLAGS_http_content_type);
} }
// Because `done'(last parameter) is NULL, this function waits until // Because `done'(last parameter) is NULL, this function waits until
......
...@@ -37,7 +37,6 @@ DEFINE_int32(backup_timeout_ms, -1, "backup timeout in milliseconds"); ...@@ -37,7 +37,6 @@ DEFINE_int32(backup_timeout_ms, -1, "backup timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)"); DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
DEFINE_bool(dont_fail, false, "Print fatal when some call failed"); DEFINE_bool(dont_fail, false, "Print fatal when some call failed");
DEFINE_int32(dummy_port, -1, "Launch dummy server at this port"); DEFINE_int32(dummy_port, -1, "Launch dummy server at this port");
DEFINE_string(http_content_type, "application/json", "Content type of http request");
std::string g_attachment; std::string g_attachment;
...@@ -66,8 +65,6 @@ static void* sender(void* arg) { ...@@ -66,8 +65,6 @@ static void* sender(void* arg) {
// Set attachment which is wired to network directly instead of // Set attachment which is wired to network directly instead of
// being serialized into protobuf messages. // being serialized into protobuf messages.
cntl.request_attachment().append(g_attachment); cntl.request_attachment().append(g_attachment);
} else {
cntl.http_request().set_content_type(FLAGS_http_content_type);
} }
// Because `done'(last parameter) is NULL, this function waits until // Because `done'(last parameter) is NULL, this function waits until
......
...@@ -1359,7 +1359,6 @@ static void PackH2Message(butil::IOBuf* out, ...@@ -1359,7 +1359,6 @@ static void PackH2Message(butil::IOBuf* out,
H2UnsentRequest* H2UnsentRequest::New(Controller* c) { H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
const HttpHeader& h = c->http_request(); const HttpHeader& h = c->http_request();
const CommonStrings* const common = get_common_strings(); const CommonStrings* const common = get_common_strings();
const bool need_content_length = (h.method() != HTTP_METHOD_GET);
const bool need_content_type = !h.content_type().empty(); const bool need_content_type = !h.content_type().empty();
const bool need_accept = !h.GetHeader(common->ACCEPT); const bool need_accept = !h.GetHeader(common->ACCEPT);
const bool need_user_agent = !h.GetHeader(common->USER_AGENT); const bool need_user_agent = !h.GetHeader(common->USER_AGENT);
...@@ -1367,7 +1366,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) { ...@@ -1367,7 +1366,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
const bool need_authorization = const bool need_authorization =
(!user_info.empty() && !h.GetHeader("Authorization")); (!user_info.empty() && !h.GetHeader("Authorization"));
const size_t maxsize = h.HeaderCount() + 4 const size_t maxsize = h.HeaderCount() + 4
+ (size_t)need_content_length
+ (size_t)need_content_type + (size_t)need_content_type
+ (size_t)need_accept + (size_t)need_accept
+ (size_t)need_user_agent + (size_t)need_user_agent
...@@ -1409,10 +1407,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) { ...@@ -1409,10 +1407,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
*val = butil::endpoint2str(c->remote_side()).c_str(); *val = butil::endpoint2str(c->remote_side()).c_str();
} }
} }
if (need_content_length) {
butil::string_printf(&msg->push(common->CONTENT_LENGTH),
"%" PRIu64, c->request_attachment().size());
}
if (need_content_type) { if (need_content_type) {
msg->push(common->CONTENT_TYPE, h.content_type()); msg->push(common->CONTENT_TYPE, h.content_type());
} }
...@@ -1631,11 +1625,8 @@ H2UnsentResponse::H2UnsentResponse(Controller* c, int stream_id, bool is_grpc) ...@@ -1631,11 +1625,8 @@ H2UnsentResponse::H2UnsentResponse(Controller* c, int stream_id, bool is_grpc)
H2UnsentResponse* H2UnsentResponse::New(Controller* c, int stream_id, bool is_grpc) { H2UnsentResponse* H2UnsentResponse::New(Controller* c, int stream_id, bool is_grpc) {
const HttpHeader* const h = &c->http_response(); const HttpHeader* const h = &c->http_response();
const CommonStrings* const common = get_common_strings(); const CommonStrings* const common = get_common_strings();
const bool need_content_length =
(c->Failed() || !c->has_progressive_writer());
const bool need_content_type = !h->content_type().empty(); const bool need_content_type = !h->content_type().empty();
const size_t maxsize = 1 const size_t maxsize = 1
+ (size_t)need_content_length
+ (size_t)need_content_type; + (size_t)need_content_type;
const size_t memsize = offsetof(H2UnsentResponse, _list) + const size_t memsize = offsetof(H2UnsentResponse, _list) +
sizeof(HPacker::Header) * maxsize; sizeof(HPacker::Header) * maxsize;
...@@ -1647,10 +1638,6 @@ H2UnsentResponse* H2UnsentResponse::New(Controller* c, int stream_id, bool is_gr ...@@ -1647,10 +1638,6 @@ H2UnsentResponse* H2UnsentResponse::New(Controller* c, int stream_id, bool is_gr
butil::string_printf(&msg->push(common->H2_STATUS), butil::string_printf(&msg->push(common->H2_STATUS),
"%d", h->status_code()); "%d", h->status_code());
} }
if (need_content_length) {
butil::string_printf(&msg->push(common->CONTENT_LENGTH),
"%" PRIu64, msg->_data.size());
}
if (need_content_type) { if (need_content_type) {
msg->push(common->CONTENT_TYPE, h->content_type()); msg->push(common->CONTENT_TYPE, h->content_type());
} }
......
...@@ -111,7 +111,6 @@ CommonStrings::CommonStrings() ...@@ -111,7 +111,6 @@ CommonStrings::CommonStrings()
, AUTHORIZATION("authorization") , AUTHORIZATION("authorization")
, ACCEPT_ENCODING("accept-encoding") , ACCEPT_ENCODING("accept-encoding")
, CONTENT_ENCODING("content-encoding") , CONTENT_ENCODING("content-encoding")
, CONTENT_LENGTH("content-length")
, GZIP("gzip") , GZIP("gzip")
, CONNECTION("connection") , CONNECTION("connection")
, KEEP_ALIVE("keep-alive") , KEEP_ALIVE("keep-alive")
...@@ -455,7 +454,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/, ...@@ -455,7 +454,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
const bool is_http2 = (cntl->request_protocol() == PROTOCOL_HTTP2); const bool is_http2 = (cntl->request_protocol() == PROTOCOL_HTTP2);
bool is_grpc = false; bool is_grpc = false;
if (request != NULL) { if (request != NULL) {
// If request is not NULL, message body will be serialized json, // If request is not NULL, message body will be serialized proto/json,
if (!request->IsInitialized()) { if (!request->IsInitialized()) {
return cntl->SetFailed( return cntl->SetFailed(
EREQUEST, "Missing required fields in request: %s", EREQUEST, "Missing required fields in request: %s",
...@@ -465,11 +464,25 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/, ...@@ -465,11 +464,25 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
return cntl->SetFailed(EREQUEST, "request_attachment must be empty " return cntl->SetFailed(EREQUEST, "request_attachment must be empty "
"when request is not NULL"); "when request is not NULL");
} }
HttpContentType content_type = HTTP_CONTENT_OTHERS;
if (cntl->http_request().content_type().empty()) {
// Set content-type if user did not.
// Note that http1.x defaults to json and h2 defaults to pb.
if (is_http2) {
content_type = HTTP_CONTENT_PROTO;
cntl->http_request().set_content_type(common->CONTENT_TYPE_PROTO);
} else {
content_type = HTTP_CONTENT_JSON;
cntl->http_request().set_content_type(common->CONTENT_TYPE_JSON);
}
} else {
bool is_grpc_ct = false;
content_type = ParseContentType(cntl->http_request().content_type(),
&is_grpc_ct);
is_grpc = (is_http2 && is_grpc_ct);
}
butil::IOBufAsZeroCopyOutputStream wrapper(&cntl->request_attachment()); butil::IOBufAsZeroCopyOutputStream wrapper(&cntl->request_attachment());
bool is_grpc_ct = false;
const HttpContentType content_type
= ParseContentType(cntl->http_request().content_type(), &is_grpc_ct);
is_grpc = (is_http2 && is_grpc_ct);
if (content_type == HTTP_CONTENT_PROTO) { if (content_type == HTTP_CONTENT_PROTO) {
// Serialize content as protobuf // Serialize content as protobuf
if (!request->SerializeToZeroCopyStream(&wrapper)) { if (!request->SerializeToZeroCopyStream(&wrapper)) {
...@@ -477,7 +490,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/, ...@@ -477,7 +490,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
return cntl->SetFailed(EREQUEST, "Fail to serialize %s", return cntl->SetFailed(EREQUEST, "Fail to serialize %s",
request->GetTypeName().c_str()); request->GetTypeName().c_str());
} }
} else { // Serialize content as json } else if (content_type == HTTP_CONTENT_JSON) {
std::string err; std::string err;
json2pb::Pb2JsonOptions opt; json2pb::Pb2JsonOptions opt;
opt.bytes_to_base64 = cntl->has_pb_bytes_to_base64(); opt.bytes_to_base64 = cntl->has_pb_bytes_to_base64();
...@@ -489,10 +502,9 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/, ...@@ -489,10 +502,9 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
cntl->request_attachment().clear(); cntl->request_attachment().clear();
return cntl->SetFailed(EREQUEST, "Fail to convert request to json, %s", err.c_str()); return cntl->SetFailed(EREQUEST, "Fail to convert request to json, %s", err.c_str());
} }
// Set content-type if user did not. } else {
if (cntl->http_request().content_type().empty()) { return cntl->SetFailed(EREQUEST, "Unknown content_type=%s",
cntl->http_request().set_content_type(common->CONTENT_TYPE_JSON); cntl->http_request().content_type().c_str());
}
} }
} else { } else {
// Use request_attachment. // Use request_attachment.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment