Skip to content

Commit

Permalink
static routes are now all working along with their mime types. it is …
Browse files Browse the repository at this point in the history
…time that we now can test this all out
  • Loading branch information
deanhiller committed Aug 28, 2016
1 parent 50f1f08 commit 10be1d5
Show file tree
Hide file tree
Showing 7 changed files with 134 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,4 +111,11 @@ public String transform(Map<String, String> pathParams) {
return fileSystemPath+relativeUrlPathLeft;
}

@Override
public String toString() {
return "StaticRoute [\n urlPath=" + urlPath + ",\n fileSystemPath=" + fileSystemPath + ",\n isFile=" + isFile
+ ",\n patternToMatch=" + patternToMatch + ",\n isOnClassPath=" + isOnClassPath + ",\n pathParamNames="
+ pathParamNames + "]";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
import java.util.List;

import org.webpieces.data.api.DataWrapper;
import org.webpieces.data.api.DataWrapperGenerator;
import org.webpieces.data.api.DataWrapperGeneratorFactory;
import org.webpieces.httpparser.api.common.Header;
import org.webpieces.httpparser.api.common.KnownHeaderName;
import org.webpieces.httpparser.api.dto.ContentType;
Expand All @@ -20,6 +22,7 @@
public class FullResponse {

private static final Charset DEFAULT_CHARSET = Charset.forName("ISO-8859-1");
private DataWrapperGenerator dataGen = DataWrapperGeneratorFactory.createDataWrapperGenerator();
private HttpResponse response;
private List<HttpChunk> chunks = new ArrayList<>();
private HttpLastChunk lastChunk;
Expand Down Expand Up @@ -51,7 +54,15 @@ public void setLastChunk(HttpLastChunk lastHttpChunk) {
public DataWrapper getBody() {
if(chunks.size() == 0)
return response.getBodyNonNull();
throw new UnsupportedOperationException("Need to implement with DataGen to chain all chunks together");

HttpChunk chunk = chunks.get(0);
DataWrapper data = chunk.getBodyNonNull();
for(int i = 1; i < chunks.size(); i++) {
HttpChunk next = chunks.get(i);
data = dataGen.chainDataWrappers(data, next.getBodyNonNull());
}

return data;
}

public String getBodyAsString() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@ public synchronized CompletableFuture<FrontendSocket> write(HttpPayload payload)
return null;
}
FullResponse nextResp = new FullResponse(response);
if(response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH) != null)
if(response.getHeaderLookupStruct().getHeader(KnownHeaderName.CONTENT_LENGTH) != null) {
payloads.add(nextResp);
else
this.notifyAll();
} else
chunkedResponse = nextResp;

return null;
} else if(payloads.size() == 0) {
log.error("Should get HttpResponse first but instead received something else="+payload);
return null;
}

switch (payload.getMessageType()) {
Expand All @@ -50,6 +48,7 @@ public synchronized CompletableFuture<FrontendSocket> write(HttpPayload payload)
case LAST_CHUNK:
chunkedResponse.setLastChunk(payload.getLastHttpChunk());
payloads.add(chunkedResponse);
this.notifyAll();
chunkedResponse = null;
break;
default:
Expand Down Expand Up @@ -81,6 +80,9 @@ public synchronized List<FullResponse> getResponsesImpl(long waitTimeMs, int cou
long start = System.currentTimeMillis();
while(payloads.size() < count) {
this.wait(waitTimeMs+500);
if(payloads.size() >= count)
return payloads;

long time = System.currentTimeMillis() - start;
if(time > waitTimeMs)
throw new IllegalStateException("While waiting for "+count+" responses, some or all never came. count that came="+payloads.size());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.webpieces.webserver.impl;

import java.io.File;
import java.io.IOException;
import java.io.StringWriter;
import java.nio.ByteBuffer;
Expand Down Expand Up @@ -40,6 +41,8 @@
import org.webpieces.httpparser.api.common.Header;
import org.webpieces.httpparser.api.common.KnownHeaderName;
import org.webpieces.httpparser.api.common.ResponseCookie;
import org.webpieces.httpparser.api.dto.HttpChunk;
import org.webpieces.httpparser.api.dto.HttpLastChunk;
import org.webpieces.httpparser.api.dto.HttpRequest;
import org.webpieces.httpparser.api.dto.HttpResponse;
import org.webpieces.httpparser.api.dto.HttpResponseStatus;
Expand All @@ -53,6 +56,7 @@
import org.webpieces.router.api.dto.RenderStaticResponse;
import org.webpieces.router.api.dto.View;
import org.webpieces.router.api.exceptions.IllegalReturnValueException;
import org.webpieces.router.api.exceptions.NotFoundException;
import org.webpieces.router.impl.CookieTranslator;
import org.webpieces.templating.api.Template;
import org.webpieces.templating.api.TemplateService;
Expand Down Expand Up @@ -149,7 +153,22 @@ public CompletableFuture<Void> sendRenderStatic(RenderStaticResponse renderStati
}

private CompletableFuture<Void> runAsyncFileRead(RenderStaticResponse renderStatic) throws IOException {
Path file = Paths.get(renderStatic.getAbsolutePath());
String fileName = renderStatic.getAbsolutePath();
Path file = Paths.get(fileName);

File f = file.toFile();
if(!f.exists() || !f.isFile())
throw new NotFoundException("File="+file+" was not found");

String extension = null;
int lastDirIndex = fileName.lastIndexOf("/");
int lastDot = fileName.lastIndexOf(".");
if(lastDot > lastDirIndex) {
extension = fileName.substring(lastDot+1);
}

HttpResponse response = createResponse(KnownStatusCode.HTTP_200_OK, null, extension, "application/octet-stream");
channel.write(response);

//NOTE: try with resource is synchronous and won't work here :(
AsynchronousFileChannel asyncFile = AsynchronousFileChannel.open(file, options, fileExecutor);
Expand Down Expand Up @@ -203,9 +222,19 @@ private CompletableFuture<Boolean> read(Path file, AsynchronousFileChannel async

private void sendBuffer(RequestContext ctx, ByteBuffer buf) {
if(buf.remaining() == 0) {
HttpLastChunk last = new HttpLastChunk();
pool.releaseBuffer(buf);
channel.write(last);
log.info("last chunk. empty of course meeting spec");
return;
}

log.info("wanting to send buffer size="+buf.remaining());

DataWrapper data = wrapperFactory.wrapByteBuffer(buf);
HttpChunk chunk = new HttpChunk();
chunk.setBody(data);
channel.write(chunk);
}

private CompletableFuture<ByteBuffer> run(Path file, AsynchronousFileChannel asyncFile, long position) {
Expand Down Expand Up @@ -285,9 +314,7 @@ public void sendRenderHtml(RenderResponse resp) {
extension = "txt";
}

MimeTypeResult mimeType = mimeTypes.extensionToContentType(extension, "text/plain");

HttpResponse response = createResponse(statusCode, content, mimeType);
HttpResponse response = createResponse(statusCode, content, extension, "text/plain");

log.info("sending RENDERHTML response. code="+statusCode+" for path="+request.getRequestLine().getUri().getUri()+" channel="+channel);
if(log.isDebugEnabled())
Expand Down Expand Up @@ -326,12 +353,9 @@ private String getTemplatePath(String packageStr, String templateClassName, Stri
return TemplateUtil.convertTemplateClassToPath(className);
}

private HttpResponse createResponse(KnownStatusCode statusCode, String content, MimeTypeResult mimeType) {

Charset encoding = mimeType.htmlResponsePayloadEncoding;
if(encoding == null)
encoding = config.getDefaultResponseBodyEncoding();
byte[] bytes = content.getBytes(encoding);
private HttpResponse createResponse(KnownStatusCode statusCode, String content, String extension, String defaultMime) {

MimeTypeResult mimeType = mimeTypes.extensionToContentType(extension, defaultMime);

HttpResponseStatus status = new HttpResponseStatus();
status.setKnownStatus(statusCode);
Expand All @@ -343,17 +367,28 @@ private HttpResponse createResponse(KnownStatusCode statusCode, String content,

response.addHeader(new Header(KnownHeaderName.CONTENT_TYPE, mimeType.mime));

DataWrapper data = wrapperFactory.wrapByteArray(bytes);
response.setBody(data);
Integer length = null;
if(content != null) {
Charset encoding = mimeType.htmlResponsePayloadEncoding;
if(encoding == null)
encoding = config.getDefaultResponseBodyEncoding();
byte[] bytes = content.getBytes(encoding);
DataWrapper data = wrapperFactory.wrapByteArray(bytes);
response.setBody(data);
length = bytes.length;
}

addCommonHeaders(statusCode, response, bytes.length);
addCommonHeaders(statusCode, response, length);
return response;
}

private void addCommonHeaders(KnownStatusCode statusCode, HttpResponse response, int contentLength) {
private void addCommonHeaders(KnownStatusCode statusCode, HttpResponse response, Integer contentLength) {

Header header = new Header(KnownHeaderName.CONTENT_LENGTH, contentLength+"");
response.addHeader(header);
if(contentLength != null) {
response.addHeader(new Header(KnownHeaderName.CONTENT_LENGTH, contentLength+""));
} else {
response.addHeader(new Header(KnownHeaderName.TRANSFER_ENCODING, "chunked"));
}

Header connHeader = null;
if(request != null)
Expand All @@ -377,7 +412,6 @@ private void addCommonHeaders(KnownStatusCode statusCode, HttpResponse response,

//X-XSS-Protection: 1; mode=block
//X-Frame-Options: SAMEORIGIN
//Content-Type: image/gif\r\n
//Expires: Mon, 20 Jun 2016 02:33:52 GMT\r\n
//Cache-Control: private, max-age=31536000\r\n
//Last-Modified: Mon, 02 Apr 2012 02:13:37 GMT\r\n
Expand Down Expand Up @@ -433,17 +467,15 @@ public void failureRenderingInternalServerErrorPage(Throwable e) {
+ "The webpieces platform saved them from sending back an ugly stack trace. Contact website owner "
+ "with a screen shot of this page</body></html>";

MimeTypeResult mimeType = mimeTypes.extensionToContentType("txt", "text/plain");
HttpResponse response = createResponse(KnownStatusCode.HTTP_500_INTERNAL_SVR_ERROR, html, mimeType);
HttpResponse response = createResponse(KnownStatusCode.HTTP_500_INTERNAL_SVR_ERROR, html, "txt", "text/plain");

channel.write(response);

closeIfNeeded();
}

public void sendFailure(HttpException exc) {
MimeTypeResult mimeType = mimeTypes.extensionToContentType("txt", "text/plain");
HttpResponse response = createResponse(exc.getStatusCode(), "Something went wrong(are you hacking the system?)", mimeType);
HttpResponse response = createResponse(exc.getStatusCode(), "Something went wrong(are you hacking the system?)", "txt", "text/plain");
channel.write(response);
closeIfNeeded();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,29 +34,60 @@ public void setUp() {
public void testStaticDir() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/public/staticMeta.txt");

// server.processHttpRequests(socket, req , false);
//
// List<FullResponse> responses = socket.getResponses(2000, 1);
// Assert.assertEquals(1, responses.size());
//
// FullResponse response = responses.get(0);
// response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
// response.assertContains("Hi Dean Hiller, this is testing");
// response.assertContentType("some text");
server.processHttpRequests(socket, req , false);

List<FullResponse> responses = socket.getResponses(200000, 1);
Assert.assertEquals(1, responses.size());

FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
response.assertContains("org.webpieces.webserver.staticpath.app.StaticMeta");
response.assertContentType("text/plain; charset=utf-8");
}

@Test
public void testStaticDirJpg() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/public/pics.ext/image.jpg");

server.processHttpRequests(socket, req , false);

List<FullResponse> responses = socket.getResponses(200000, 1);
Assert.assertEquals(1, responses.size());

// @Test
// public void testStaticFile() {
// HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/public/myfile");
//
// server.processHttpRequests(socket, req , false);
//
// List<FullResponse> responses = socket.getResponses();
// Assert.assertEquals(1, responses.size());
//
// FullResponse response = responses.get(0);
// response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
// response.assertContains("Hi Dean Hiller, this is testing");
// }
FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
response.assertContentType("image/jpeg");
int size = response.getBody().getReadableSize();
Assert.assertEquals(18066, size);
}

@Test
public void testStaticDirAndNotFound() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/public/pics.ext/notFound.jpg");

server.processHttpRequests(socket, req , false);

List<FullResponse> responses = socket.getResponses(2000, 1);
Assert.assertEquals(1, responses.size());

FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_404_NOTFOUND);
//render html page when not found...
response.assertContentType("text/html; charset=utf-8");
}

@Test
public void testStaticFile() {
HttpRequest req = Requests.createRequest(KnownHttpMethod.GET, "/public/myfile");

server.processHttpRequests(socket, req , false);

List<FullResponse> responses = socket.getResponses(2000, 1);
Assert.assertEquals(1, responses.size());

FullResponse response = responses.get(0);
response.assertStatusCode(KnownStatusCode.HTTP_200_OK);
response.assertContains("app.TagsMeta");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ public void configure(String currentPackage) {

String property = System.getProperty("user.dir");

//relative path(to user.dir)
addStaticDir("/public/", "src/test/resources/", false);
//absolute path...
addStaticFile("/public/myfile", property + "/src/test/resources/tagsMeta.txt", false);
//relative path(to user.dir)
addStaticDir("/public/", "src/test/resources/", false);

setPageNotFoundRoute("../../basic/biz/BasicController.notFound");
setInternalErrorRoute("../../basic/biz/BasicController.internalError");
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 10be1d5

Please sign in to comment.