Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix #379 #492

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@
public class BufferedDataSink implements DataSink {
DataSink mDataSink;
public BufferedDataSink(DataSink datasink) {
mPendingWrites = new ByteBufferList();
setDataSink(datasink);
}

public BufferedDataSink(AsyncSocket mSocket, ByteBufferList byteBufferList) {
mPendingWrites = byteBufferList;
setDataSink(mSocket);
}

public boolean isBuffering() {
return mPendingWrites.hasRemaining() || forceBuffering;
}
Expand Down Expand Up @@ -52,7 +58,7 @@ private void writePending() {
mWritable.onWriteable();
}

ByteBufferList mPendingWrites = new ByteBufferList();
ByteBufferList mPendingWrites;

@Override
public void write(ByteBufferList bb) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,12 @@ public FilteredDataSink(DataSink sink) {
super(sink);
setMaxBuffer(0);
}


public FilteredDataSink(AsyncSocket mSocket, ByteBufferList mWritePendings) {
super(mSocket, mWritePendings);
setMaxBuffer(0);
}

public ByteBufferList filter(ByteBufferList bb) {
return bb;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import java.util.UUID;

public class WebSocketImpl implements WebSocket {

private final ByteBufferList mWritePendings = new ByteBufferList();

@Override
public void end() {
mSocket.end();
Expand Down Expand Up @@ -136,7 +139,7 @@ public WebSocketImpl(AsyncHttpServerRequest request, AsyncHttpServerResponse res
response.getHeaders().set("Sec-WebSocket-Protocol", protocol);
// if (origin != null)
// response.getHeaders().getHeaders().set("Access-Control-Allow-Origin", "http://" + origin);
response.writeHead();
response.writeHead(mWritePendings);

setupParser(false, false);
}
Expand All @@ -159,7 +162,7 @@ public static void addWebSocketUpgradeHeaders(AsyncHttpRequest req, String proto

public WebSocketImpl(AsyncSocket socket) {
mSocket = socket;
mSink = new BufferedDataSink(mSocket);
mSink = new BufferedDataSink(mSocket, mWritePendings);
}

public static WebSocket finishHandshake(Headers requestHeaders, AsyncHttpResponse response) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.nio.ByteBuffer;

import com.koushikdutta.async.AsyncSocket;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.FilteredDataSink;
Expand All @@ -11,6 +12,10 @@ public ChunkedOutputFilter(DataSink sink) {
super(sink);
}

public ChunkedOutputFilter(AsyncSocket mSocket, ByteBufferList mWritePenings) {
super(mSocket, mWritePenings);
}

@Override
public ByteBufferList filter(ByteBufferList bb) {
String chunkLen = Integer.toString(bb.remaining(), 16) + "\r\n";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.koushikdutta.async.http.server;

import com.koushikdutta.async.AsyncSocket;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.http.AsyncHttpResponse;
Expand Down Expand Up @@ -34,4 +35,6 @@ public interface AsyncHttpServerResponse extends DataSink, CompletedCallback {
*/
public void onCompleted(Exception ex);
public AsyncSocket getSocket();

void writeHead(ByteBufferList mWritePendings);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,7 @@

import android.text.TextUtils;

import com.koushikdutta.async.AsyncServer;
import com.koushikdutta.async.AsyncSocket;
import com.koushikdutta.async.ByteBufferList;
import com.koushikdutta.async.DataSink;
import com.koushikdutta.async.Util;
import com.koushikdutta.async.*;
import com.koushikdutta.async.callback.CompletedCallback;
import com.koushikdutta.async.callback.DataCallback;
import com.koushikdutta.async.callback.WritableCallback;
Expand All @@ -31,6 +27,7 @@
public class AsyncHttpServerResponseImpl implements AsyncHttpServerResponse {
private Headers mRawHeaders = new Headers();
private long mContentLength = -1;
private ByteBufferList mWritePendings;

@Override
public Headers getHeaders() {
Expand Down Expand Up @@ -108,12 +105,12 @@ public void onCompleted(Exception ex) {
return;
}
if (isChunked) {
ChunkedOutputFilter chunked = new ChunkedOutputFilter(mSocket);
ChunkedOutputFilter chunked = new ChunkedOutputFilter(mSocket, mWritePendings);
chunked.setMaxBuffer(0);
mSink = chunked;
}
else {
mSink = mSocket;
mSink = new BufferedDataSink(mSocket, mWritePendings);
}

mSink.setClosedCallback(closedCallback);
Expand Down Expand Up @@ -192,6 +189,12 @@ public void writeHead() {
initFirstWrite();
}

@Override
public void writeHead(ByteBufferList mWritePendings) {
this.mWritePendings = mWritePendings;
writeHead();
}

@Override
public void setContentType(String contentType) {
mRawHeaders.set("Content-Type", contentType);
Expand Down