Skip to content

Commit 8d6181f

Browse files
briancullenapottere
authored andcommitted
Changes to look ahead in isBatchedQuery (#62)
Simple change to ensure that the readlimit passed into InputStream.mark matches the length of the read buffer. This ensures that when InputStream.reset is called the stream is reset correctly. A test has also been added to ensure this behaviour.
1 parent 1dbf88c commit 8d6181f

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

src/main/java/graphql/servlet/GraphQLServlet.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,11 +439,12 @@ private boolean isBatchedQuery(InputStream inputStream) throws IOException {
439439
return false;
440440
}
441441

442+
final int BUFFER_LENGTH = 128;
442443
ByteArrayOutputStream result = new ByteArrayOutputStream();
443-
byte[] buffer = new byte[128];
444+
byte[] buffer = new byte[BUFFER_LENGTH];
444445
int length;
445446

446-
inputStream.mark(0);
447+
inputStream.mark(BUFFER_LENGTH);
447448
while ((length = inputStream.read(buffer)) != -1) {
448449
result.write(buffer, 0, length);
449450
String chunk = result.toString();

src/test/groovy/graphql/servlet/GraphQLServletSpec.groovy

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import org.springframework.mock.web.MockHttpServletResponse
1313
import spock.lang.Shared
1414
import spock.lang.Specification
1515

16+
import javax.servlet.ServletInputStream
17+
import javax.servlet.http.HttpServletRequest
1618
/**
1719
* @author Andrew Potter
1820
*/
@@ -715,4 +717,26 @@ class GraphQLServletSpec extends Specification {
715717
expect:
716718
servlet.getMapper().writeValueAsString(ExecutionTypeInfo.newTypeInfo().type(new GraphQLNonNull(Scalars.GraphQLString)).build()) != "{}"
717719
}
720+
721+
def "isBatchedQuery check uses buffer length as read limit"() {
722+
setup:
723+
HttpServletRequest mockRequest = Mock()
724+
ServletInputStream mockInputStream = Mock()
725+
726+
mockInputStream.markSupported() >> true
727+
mockRequest.getInputStream() >> mockInputStream
728+
mockRequest.getMethod() >> "POST"
729+
730+
when:
731+
servlet.doPost(mockRequest, response)
732+
733+
then:
734+
1 * mockInputStream.mark(128)
735+
736+
then:
737+
1 * mockInputStream.read({ it.length == 128 }) >> -1
738+
739+
then:
740+
1 * mockInputStream.reset()
741+
}
718742
}

0 commit comments

Comments
 (0)