Skip to content

Commit

Permalink
Issue #695 improve getResponse methods
Browse files Browse the repository at this point in the history
  • Loading branch information
gregw committed Jul 7, 2016
1 parent 7afc360 commit 183e3ac
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 14 deletions.
Expand Up @@ -282,7 +282,7 @@ public ByteBuffer waitForOutput(long time,TimeUnit unit) throws InterruptedExcep


try(Locker.Lock lock = _locker.lock()) try(Locker.Lock lock = _locker.lock())
{ {
if (BufferUtil.isEmpty(_out)) if (BufferUtil.isEmpty(_out) && !_closed && !_oshut)
_hasOutput.await(time,unit); _hasOutput.await(time,unit);


b=_out; b=_out;
Expand Down
Expand Up @@ -38,11 +38,20 @@
import org.eclipse.jetty.util.ssl.SslContextFactory; import org.eclipse.jetty.util.ssl.SslContextFactory;
import org.eclipse.jetty.util.thread.Scheduler; import org.eclipse.jetty.util.thread.Scheduler;


/**
* A local connector, mostly for testing purposes.
* <pre>
* HttpTester.Request request = HttpTester.newRequest();
* request.setURI("/some/resource");
* HttpTester.Response response =
* HttpTester.parseResponse(HttpTester.from(localConnector.getResponse(request.generate())));
* </pre>
*
*/
public class LocalConnector extends AbstractConnector public class LocalConnector extends AbstractConnector
{ {
private final BlockingQueue<LocalEndPoint> _connects = new LinkedBlockingQueue<>(); private final BlockingQueue<LocalEndPoint> _connects = new LinkedBlockingQueue<>();



public LocalConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, ConnectionFactory... factories) public LocalConnector(Server server, Executor executor, Scheduler scheduler, ByteBufferPool pool, int acceptors, ConnectionFactory... factories)
{ {
super(server,executor,scheduler,pool,acceptors,factories); super(server,executor,scheduler,pool,acceptors,factories);
Expand Down Expand Up @@ -198,20 +207,36 @@ public ByteBuffer getResponse(ByteBuffer requestsBuffer) throws Exception
{ {
return getResponse(requestsBuffer,false,10,TimeUnit.SECONDS); return getResponse(requestsBuffer,false,10,TimeUnit.SECONDS);
} }

/** Get a single response using a parser to search for the end of the message.
* @param requestBuffer The request to send
* @param time The time to wait
* @param unit The units of the wait
* @return ByteBuffer containing response or null.
* @throws Exception If there is a problem
*/
public ByteBuffer getResponse(ByteBuffer requestBuffer, long time,TimeUnit unit) throws Exception
{
boolean head = BufferUtil.toString(requestBuffer).toLowerCase().startsWith("head ");
if (LOG.isDebugEnabled())
LOG.debug("requests {}", BufferUtil.toUTF8String(requestBuffer));
LocalEndPoint endp = executeRequest(requestBuffer);
return endp.waitForResponse(head,time,unit);
}


/** Get a single response using a parser to search for the end of the message. /** Get a single response using a parser to search for the end of the message.
* @param requestsBuffer The request to send * @param requestBuffer The request to send
* @param head True if the response is for a head request * @param head True if the response is for a head request
* @param time The time to wait * @param time The time to wait
* @param unit The units of the wait * @param unit The units of the wait
* @return ByteBuffer containing response or null. * @return ByteBuffer containing response or null.
* @throws Exception If there is a problem * @throws Exception If there is a problem
*/ */
public ByteBuffer getResponse(ByteBuffer requestsBuffer,boolean head, long time,TimeUnit unit) throws Exception public ByteBuffer getResponse(ByteBuffer requestBuffer,boolean head, long time,TimeUnit unit) throws Exception
{ {
if (LOG.isDebugEnabled()) if (LOG.isDebugEnabled())
LOG.debug("requests {}", BufferUtil.toUTF8String(requestsBuffer)); LOG.debug("requests {}", BufferUtil.toUTF8String(requestBuffer));
LocalEndPoint endp = executeRequest(requestsBuffer); LocalEndPoint endp = executeRequest(requestBuffer);
return endp.waitForResponse(head,time,unit); return endp.waitForResponse(head,time,unit);
} }


Expand All @@ -225,6 +250,25 @@ public String getResponse(String rawRequest) throws Exception
{ {
return getResponse(rawRequest,false,30,TimeUnit.SECONDS); return getResponse(rawRequest,false,30,TimeUnit.SECONDS);
} }

/** Get a single response using a parser to search for the end of the message.
* @param rawRequest The request to send
* @param time The time to wait
* @param unit The units of the wait
* @return ByteBuffer containing response or null.
* @throws Exception If there is a problem
*/
public String getResponse(String rawRequest,long time,TimeUnit unit) throws Exception
{
boolean head = rawRequest.toLowerCase().startsWith("head ");
ByteBuffer requestsBuffer = BufferUtil.toBuffer(rawRequest, StandardCharsets.ISO_8859_1);
if (LOG.isDebugEnabled())
LOG.debug("request {}", BufferUtil.toUTF8String(requestsBuffer));
LocalEndPoint endp = executeRequest(requestsBuffer);

return BufferUtil.toString(endp.waitForResponse(head,time,unit), StandardCharsets.ISO_8859_1);
}



/** Get a single response using a parser to search for the end of the message. /** Get a single response using a parser to search for the end of the message.
* @param rawRequest The request to send * @param rawRequest The request to send
Expand All @@ -244,8 +288,6 @@ public String getResponse(String rawRequest, boolean head, long time,TimeUnit un
return BufferUtil.toString(endp.waitForResponse(head,time,unit), StandardCharsets.ISO_8859_1); return BufferUtil.toString(endp.waitForResponse(head,time,unit), StandardCharsets.ISO_8859_1);
} }




/** Local EndPoint /** Local EndPoint
*/ */
public class LocalEndPoint extends ByteArrayEndPoint public class LocalEndPoint extends ByteArrayEndPoint
Expand Down Expand Up @@ -332,7 +374,30 @@ public void waitUntilClosedOrIdleFor(long idleFor,TimeUnit units)
} }
} }
} }


/** Wait for a response using a parser to detect the end of message
* @return Buffer containing full response or null for EOF;
* @throws Exception
*/
public String getResponse() throws Exception
{
return getResponse(false,30,TimeUnit.SECONDS);
}

/** Wait for a response using a parser to detect the end of message
* @param head
* @param time
* @param unit
* @return Buffer containing full response or null for EOF;
* @throws Exception
*/
public String getResponse(boolean head, long time,TimeUnit unit) throws Exception
{
ByteBuffer response = waitForResponse(head,time,unit);
if (response!=null)
return BufferUtil.toString(response);
return null;
}


/** Wait for a response using a parser to detect the end of message /** Wait for a response using a parser to detect the end of message
* @param head * @param head
Expand Down Expand Up @@ -391,18 +456,27 @@ public boolean startResponse(HttpVersion version, int status, String reason)
} }
}; };



HttpParser parser = new HttpParser(handler); HttpParser parser = new HttpParser(handler);
parser.setHeadResponse(head); parser.setHeadResponse(head);
try(ByteArrayOutputStream2 bout = new ByteArrayOutputStream2();) try(ByteArrayOutputStream2 bout = new ByteArrayOutputStream2();)
{ {
loop: while(true) loop: while(true)
{ {
// read a chunk of response // read a chunk of response
ByteBuffer chunk = BufferUtil.hasContent(_responseData) ByteBuffer chunk;
? _responseData : waitForOutput(time,unit); if (BufferUtil.hasContent(_responseData))
_responseData=null; chunk = _responseData;

else
{
chunk = waitForOutput(time,unit);
if (BufferUtil.isEmpty(chunk) && (!isOpen() || isOutputShutdown()))
{
parser.atEOF();
parser.parseNext(BufferUtil.EMPTY_BUFFER);
break loop;
}
}

// Parse the content of this chunk // Parse the content of this chunk
while (BufferUtil.hasContent(chunk)) while (BufferUtil.hasContent(chunk))
{ {
Expand Down

0 comments on commit 183e3ac

Please sign in to comment.