Skip to content

Commit

Permalink
@ F exercise 2
Browse files Browse the repository at this point in the history
  • Loading branch information
isidore committed Sep 14, 2023
1 parent 5b62d77 commit f7a81d7
Show file tree
Hide file tree
Showing 10 changed files with 316 additions and 54 deletions.
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@
<version>5.10.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
<version>5.0.0</version>
</dependency>
</dependencies>

<profiles>
Expand Down
32 changes: 0 additions & 32 deletions src/main/java/org/samples/Person.java

This file was deleted.

148 changes: 148 additions & 0 deletions src/main/java/org/samples/Request.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
package org.samples;

import jakarta.servlet.http.HttpServletResponse;
import org.samples.required.ActionCode;
import org.samples.required.ClientAbortException;
import org.samples.required.Parameters;
import org.samples.required.RequestBase;

import java.io.IOException;
import java.nio.charset.Charset;
import java.security.InvalidParameterException;


public class Request extends RequestBase {

public void doParseParameters() {
if (parametersParsed) {
return;
}
parametersParsed = true;


Parameters parameters = coyoteRequest.getParameters();

// Set this every time in case limit has been changed via JMX
int maxParameterCount = getMaxParameterCount();
if (parts != null && maxParameterCount > 0) {
maxParameterCount -= parts.size();
}
parameters.setLimit(maxParameterCount);

// getCharacterEncoding() may have been overridden to search for
// hidden form field containing request encoding
Charset charset = getCharset();

boolean useBodyEncodingForURI = connector.getUseBodyEncodingForURI();
parameters.setCharset(charset);
if (useBodyEncodingForURI) {
parameters.setQueryStringCharset(charset);
}
// Note: If !useBodyEncodingForURI, the query string encoding is
// that set towards the start of CoyoteAdapter.service()

parameters.handleQueryParameters();

if (usingInputStream || usingReader) {
return;
}

String contentType = getContentType();
if (contentType == null) {
contentType = "";
}
int semicolon = contentType.indexOf(';');
if (semicolon >= 0) {
contentType = contentType.substring(0, semicolon).trim();
} else {
contentType = contentType.trim();
}

if ("multipart/form-data".equals(contentType)) {
parseParts();
if (partsParseException instanceof IllegalStateException) {
parametersParseException = (IllegalStateException) partsParseException;
} else if (partsParseException != null) {
parametersParseException = new InvalidParameterException(partsParseException.getMessage());
}
return;
}

if (!getConnector().isParseBodyMethod(getMethod())) {
return;
}

if (!("application/x-www-form-urlencoded".equals(contentType))) {
return;
}

int len = getContentLength();

if (len > 0) {
int maxPostSize = connector.getMaxPostSize();
if ((maxPostSize >= 0) && (len > maxPostSize)) {
String message = sm.getString("coyoteRequest.postTooLarge");
Context context = getContext();
if (context != null && context.getLogger().isDebugEnabled()) {
context.getLogger().debug(message);
}
checkSwallowInput();
parametersParseException =
new ClientAbortException(message, HttpServletResponse.SC_REQUEST_ENTITY_TOO_LARGE);
return;
}
byte[] formData = null;
if (len < CACHED_POST_LEN) {
if (postData == null) {
postData = new byte[CACHED_POST_LEN];
}
formData = postData;
} else {
formData = new byte[len];
}
try {
readPostBodyFully(formData, len);
} catch (IOException e) {
// Client disconnect
Context context = getContext();
if (context != null && context.getLogger().isDebugEnabled()) {
context.getLogger().debug(sm.getString("coyoteRequest.parseParameters"), e);
}
response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, null);
if (e instanceof ClientAbortException) {
parametersParseException = new InvalidParameterException(e.getMessage());
} else {
parametersParseException = new ClientAbortException(e);
}
return;
}
parameters.processParameters(formData, 0, len);
} else if ("chunked".equalsIgnoreCase(coyoteRequest.getHeader("transfer-encoding"))) {
byte[] formData = null;
try {
formData = readChunkedPostBody();
} catch (IllegalStateException ise) {
parametersParseException = ise;
return;
} catch (IOException e) {
// Client disconnect
Context context = getContext();
if (context != null && context.getLogger().isDebugEnabled()) {
context.getLogger().debug(sm.getString("coyoteRequest.parseParameters"), e);
}
response.getCoyoteResponse().action(ActionCode.CLOSE_NOW, null);
if (e instanceof ClientAbortException) {
parametersParseException = new InvalidParameterException();
} else {
parametersParseException = new InvalidParameterException();
}
return;
}
if (formData != null) {
parameters.processParameters(formData, 0, formData.length);
}
}
}


}
5 changes: 5 additions & 0 deletions src/main/java/org/samples/required/ActionCode.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package org.samples.required;

public class ActionCode {
public static final Object CLOSE_NOW = null;
}
13 changes: 13 additions & 0 deletions src/main/java/org/samples/required/ClientAbortException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package org.samples.required;

import java.io.IOException;

public class ClientAbortException extends IOException {
public ClientAbortException(String message, int scRequestEntityTooLarge) {

}

public ClientAbortException(IOException e) {

}
}
56 changes: 56 additions & 0 deletions src/main/java/org/samples/required/Parameters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package org.samples.required;
import java.io.IOException;
import java.nio.charset.Charset;

public class Parameters {
public void setLimit(int maxParameterCount) {
}

public boolean getUseBodyEncodingForURI() {
return false;
}
public boolean isParseBodyMethod(Object method){
return false;
}

public void setCharset(Charset charset) {
}

public void setQueryStringCharset(Charset charset) {
}

public void handleQueryParameters() {
}

public int getMaxPostSize() {
return 0;
}

public String getString(String s) {
return null;
}

public Parameters getLogger() {
return null;
}

public boolean isDebugEnabled() {
return false;
}

public void debug(String message) {
}

public void processParameters(byte[] formData, int i, int length) {
}

public void debug(String string, IOException e) {
}

public Parameters getCoyoteResponse() {
return null;
}

public void action(Object closeNow, Object o) {
}
}
88 changes: 88 additions & 0 deletions src/main/java/org/samples/required/RequestBase.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.samples.required;

import jakarta.servlet.http.Part;
import org.samples.Request;

import java.io.IOException;
import java.nio.charset.Charset;
import java.util.Collection;

public class RequestBase {
/**
* Post data buffer.
*/
protected static final int CACHED_POST_LEN = 8192;
protected Parameters response;
protected Request coyoteRequest;
protected Parameters sm;
/**
* Using stream flag.
*/
protected boolean usingInputStream = false;
protected boolean usingReader = false;
protected boolean parametersParsed = false;
protected byte[] postData = null;
protected Exception parametersParseException = null;
/**
* The parts, if any, uploaded with this request.
*/
protected Collection<Part> parts = null;
/**
* The exception thrown, if any when parsing the parts.
*/
protected Exception partsParseException = null;
protected Parameters connector;

protected byte[] readChunkedPostBody() throws IOException {
return new byte[0];
}

protected String getHeader(String s) {
return null;
}

protected void checkSwallowInput() {

}

protected void readPostBodyFully(byte[] formData, int len) throws IOException {
}

protected int getContentLength() {
return 0;
}

protected Object getMethod() {
return null;
}

protected Context getContext() {
return null;
}

protected Parameters getConnector() {
return null;
}

protected void parseParts() {
}

protected String getContentType() {
return null;
}

protected Charset getCharset() {
return null;
}

protected int getMaxParameterCount() {
return 0;
}

protected Parameters getParameters() {
return null;
}

public static class Context extends Parameters {
}
}
16 changes: 1 addition & 15 deletions src/test/java/org/samples/SampleTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,5 @@ public void testNormalJunit()
{
assertEquals(5, 5);
}
@Test
public void testWithApprovalTests()
{
Approvals.verify("Hello World");
}
/**
* note: this requires GSON which is currently added in the pom.xml file.
* This is only required if you want to use the VerifyAsJson.
**/
@Test
public void testJson()
{
Person hero = new Person("jayne", "cobb", true, 38);
Approvals.verifyAsJson(hero);
}

}
6 changes: 0 additions & 6 deletions src/test/java/org/samples/SampleTests.testJson.approved.json

This file was deleted.

This file was deleted.

0 comments on commit f7a81d7

Please sign in to comment.