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");
DEFINE_int32(timeout_ms, 100, "RPC timeout in milliseconds");
DEFINE_int32(max_retry, 3, "Max retries(not including the first RPC)");
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[]) {
// Parse gflags. We recommend you to use gflags as well.
......@@ -69,8 +68,6 @@ int main(int argc, char* argv[]) {
// Set attachment which is wired to network directly instead of
// being serialized into protobuf messages.
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
......
......@@ -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(enable_ssl, false, "Use SSL connection");
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_attachment;
......@@ -62,8 +61,6 @@ static void* sender(void* arg) {
// Set attachment which is wired to network directly instead of
// being serialized into protobuf messages.
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
......
......@@ -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_bool(dont_fail, false, "Print fatal when some call failed");
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;
......@@ -66,8 +65,6 @@ static void* sender(void* arg) {
// Set attachment which is wired to network directly instead of
// being serialized into protobuf messages.
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
......
......@@ -1359,7 +1359,6 @@ static void PackH2Message(butil::IOBuf* out,
H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
const HttpHeader& h = c->http_request();
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_accept = !h.GetHeader(common->ACCEPT);
const bool need_user_agent = !h.GetHeader(common->USER_AGENT);
......@@ -1367,7 +1366,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
const bool need_authorization =
(!user_info.empty() && !h.GetHeader("Authorization"));
const size_t maxsize = h.HeaderCount() + 4
+ (size_t)need_content_length
+ (size_t)need_content_type
+ (size_t)need_accept
+ (size_t)need_user_agent
......@@ -1409,10 +1407,6 @@ H2UnsentRequest* H2UnsentRequest::New(Controller* c) {
*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) {
msg->push(common->CONTENT_TYPE, h.content_type());
}
......@@ -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) {
const HttpHeader* const h = &c->http_response();
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 size_t maxsize = 1
+ (size_t)need_content_length
+ (size_t)need_content_type;
const size_t memsize = offsetof(H2UnsentResponse, _list) +
sizeof(HPacker::Header) * maxsize;
......@@ -1647,10 +1638,6 @@ H2UnsentResponse* H2UnsentResponse::New(Controller* c, int stream_id, bool is_gr
butil::string_printf(&msg->push(common->H2_STATUS),
"%d", h->status_code());
}
if (need_content_length) {
butil::string_printf(&msg->push(common->CONTENT_LENGTH),
"%" PRIu64, msg->_data.size());
}
if (need_content_type) {
msg->push(common->CONTENT_TYPE, h->content_type());
}
......
......@@ -111,7 +111,6 @@ CommonStrings::CommonStrings()
, AUTHORIZATION("authorization")
, ACCEPT_ENCODING("accept-encoding")
, CONTENT_ENCODING("content-encoding")
, CONTENT_LENGTH("content-length")
, GZIP("gzip")
, CONNECTION("connection")
, KEEP_ALIVE("keep-alive")
......@@ -455,7 +454,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
const bool is_http2 = (cntl->request_protocol() == PROTOCOL_HTTP2);
bool is_grpc = false;
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()) {
return cntl->SetFailed(
EREQUEST, "Missing required fields in request: %s",
......@@ -465,11 +464,25 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
return cntl->SetFailed(EREQUEST, "request_attachment must be empty "
"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());
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) {
// Serialize content as protobuf
if (!request->SerializeToZeroCopyStream(&wrapper)) {
......@@ -477,7 +490,7 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
return cntl->SetFailed(EREQUEST, "Fail to serialize %s",
request->GetTypeName().c_str());
}
} else { // Serialize content as json
} else if (content_type == HTTP_CONTENT_JSON) {
std::string err;
json2pb::Pb2JsonOptions opt;
opt.bytes_to_base64 = cntl->has_pb_bytes_to_base64();
......@@ -489,10 +502,9 @@ void SerializeHttpRequest(butil::IOBuf* /*not used*/,
cntl->request_attachment().clear();
return cntl->SetFailed(EREQUEST, "Fail to convert request to json, %s", err.c_str());
}
// Set content-type if user did not.
if (cntl->http_request().content_type().empty()) {
cntl->http_request().set_content_type(common->CONTENT_TYPE_JSON);
}
} else {
return cntl->SetFailed(EREQUEST, "Unknown content_type=%s",
cntl->http_request().content_type().c_str());
}
} else {
// 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