Skip to content

Commit 3a5602f

Browse files
DPakJneozwu
authored andcommitted
make multipart/mixed requests in compiliance with RFC7230
The current implementation of multipart/mixed requests in batch does not comply with RFC 7230. https://tools.ietf.org/html/rfc7230#section-3.1.1 The version is missing from the request line. As a result, ESF servers throw parse exceptions when using batch requests using these clients. We need this change in order to migrate our server from GSE to ESF. ------------- Created by MOE: https://github.com/google/moe MOE_MIGRATED_REVID=164786022
1 parent a2e9660 commit 3a5602f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

google-api-client/src/main/java/com/google/api/client/googleapis/batch/HttpRequestContent.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.google.api.client.http.HttpContent;
1919
import com.google.api.client.http.HttpHeaders;
2020
import com.google.api.client.http.HttpRequest;
21-
2221
import java.io.IOException;
2322
import java.io.OutputStream;
2423
import java.io.OutputStreamWriter;
@@ -35,19 +34,24 @@ class HttpRequestContent extends AbstractHttpContent {
3534

3635
/** HTTP request. */
3736
private final HttpRequest request;
37+
private static final String HTTP_VERSION = "HTTP/1.1";
3838

3939
HttpRequestContent(HttpRequest request) {
4040
super("application/http");
4141
this.request = request;
4242
}
4343

44+
@Override
4445
public void writeTo(OutputStream out) throws IOException {
4546
Writer writer = new OutputStreamWriter(out, getCharset());
4647
// write method and URL
4748
writer.write(request.getRequestMethod());
4849
writer.write(" ");
4950
writer.write(request.getUrl().build());
51+
writer.write(" ");
52+
writer.write(HTTP_VERSION);
5053
writer.write(NEWLINE);
54+
5155
// write headers
5256
HttpHeaders headers = new HttpHeaders();
5357
headers.fromHttpHeaders(request.getHeaders());

google-api-client/src/test/java/com/google/api/client/googleapis/batch/BatchRequestTest.java

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
package com.google.api.client.googleapis.batch;
44

5+
import static java.nio.charset.StandardCharsets.UTF_8;
6+
57
import com.google.api.client.googleapis.batch.BatchRequest.RequestInfo;
68
import com.google.api.client.googleapis.json.GoogleJsonError;
79
import com.google.api.client.googleapis.json.GoogleJsonError.ErrorInfo;
@@ -701,30 +703,34 @@ public void testExecute_checkWriteTo() throws Exception {
701703

702704
final StringBuilder expectedOutput = new StringBuilder();
703705
expectedOutput.append("--__END_OF_PART__\r\n");
704-
expectedOutput.append("Content-Length: 109\r\n");
706+
expectedOutput.append("Content-Length: 118\r\n");
705707
expectedOutput.append("Content-Type: application/http\r\n");
706708
expectedOutput.append("content-id: 1\r\n");
707709
expectedOutput.append("content-transfer-encoding: binary\r\n");
708710
expectedOutput.append("\r\n");
709-
expectedOutput.append("POST http://test/dummy/url1\r\n");
711+
expectedOutput.append("POST http://test/dummy/url1 HTTP/1.1\r\n");
710712
expectedOutput.append("Content-Length: 26\r\n");
711713
expectedOutput.append("Content-Type: " + request1ContentType + "\r\n");
712714
expectedOutput.append("\r\n");
713715
expectedOutput.append(request1Content + "\r\n");
714716
expectedOutput.append("--__END_OF_PART__\r\n");
715-
expectedOutput.append("Content-Length: 30\r\n");
717+
expectedOutput.append("Content-Length: 39\r\n");
716718
expectedOutput.append("Content-Type: application/http\r\n");
717719
expectedOutput.append("content-id: 2\r\n");
718720
expectedOutput.append("content-transfer-encoding: binary\r\n");
719721
expectedOutput.append("\r\n");
720-
expectedOutput.append("GET http://test/dummy/url2\r\n");
722+
expectedOutput.append("GET http://test/dummy/url2 HTTP/1.1\r\n");
721723
expectedOutput.append("\r\n");
722724
expectedOutput.append("\r\n");
723725
expectedOutput.append("--__END_OF_PART__--\r\n");
724726
MockHttpTransport transport = new MockHttpTransport();
725-
HttpRequest request1 = transport.createRequestFactory().buildRequest(
726-
request1Method, new GenericUrl(request1Url),
727-
new ByteArrayContent(request1ContentType, request1Content.getBytes()));
727+
HttpRequest request1 =
728+
transport
729+
.createRequestFactory()
730+
.buildRequest(
731+
request1Method,
732+
new GenericUrl(request1Url),
733+
new ByteArrayContent(request1ContentType, request1Content.getBytes(UTF_8)));
728734
HttpRequest request2 = transport.createRequestFactory()
729735
.buildRequest(request2Method, new GenericUrl(request2Url), null);
730736
subtestExecute_checkWriteTo(expectedOutput.toString(), request1, request2);
@@ -806,9 +812,9 @@ public boolean retrySupported() {
806812
}
807813
});
808814
subtestExecute_checkWriteTo(new StringBuilder().append("--__END_OF_PART__\r\n")
809-
.append("Content-Length: 27\r\n").append("Content-Type: application/http\r\n")
815+
.append("Content-Length: 36\r\n").append("Content-Type: application/http\r\n")
810816
.append("content-id: 1\r\n").append("content-transfer-encoding: binary\r\n").append("\r\n")
811-
.append("POST http://google.com/\r\n").append("\r\n").append("\r\n")
817+
.append("POST http://google.com/ HTTP/1.1\r\n").append("\r\n").append("\r\n")
812818
.append("--__END_OF_PART__--\r\n").toString(), request1);
813819
}
814820

0 commit comments

Comments
 (0)