Package org.xlightweb

Examples of org.xlightweb.IHttpRequest


     
     
      public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

         
          IHttpRequest req = exchange.getRequest();
         
          int port;
          if (req.isSecure()) {
              port = sslServer.getLocalPort();
          } else {
              port = server.getLocalPort();
          }
         
          URL url = req.getRequestUrl();
         
          req.setRequestUrl(new URL(url.getProtocol(), url.getHost(), port, url.getFile()));
         
          try {
              httpClient.send(req, new ResponseHandler(exchange));
          }catch (ConnectException ce) {
              exchange.sendError(502, ce.getMessage());
View Full Code Here


 
  private static final class CompareParametersHandler implements IHttpRequestHandler {

    public void onRequest(IHttpExchange exchange) throws IOException {

      IHttpRequest request = exchange.getRequest();
     
      StringBuilder sb = new StringBuilder();
      sb.append("requestUri=" + request.getRequestURI() + "\r\n");
      sb.append("queryString=" + request.getQueryString() + "\r\n");
      sb.append("path=" + request.getRequestHandlerPath() + "\r\n");
      sb.append("contextPath=" + request.getContextPath() + "\r\n");


      List<String> paramNames = sort(request.getParameterNameSet());
      for (String key : paramNames) {
        String value = request.getParameter(key);
        sb.append("[param] " + key  + "=" + value + "\r\n");
      }


      exchange.send(new HttpResponse(200, "text/plain", sb.toString()));
View Full Code Here

  private static final class TestRequestHandler implements IHttpRequestHandler {
   

    public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {

      IHttpRequest req = exchange.getRequest();
      String ctxPath = req.getContextPath();
      String srvPath = req.getRequestHandlerPath();
     
      StringBuilder sb = new StringBuilder();
      sb.append("contextPath=" + ctxPath + "\r\n");
      sb.append("servletPath=" + srvPath + "\r\n");
     
View Full Code Here

 
  private static final class ServerHandler implements IHttpRequestHandler {
   
    public void onRequest(IHttpExchange exchange) throws IOException {
     
      IHttpRequest request = exchange.getRequest();
      if (request.getHeader("sleep-time") != null) {
        int sleepTime = Integer.parseInt(request.getHeader("sleep-time"));
        QAUtil.sleep(sleepTime);
      }
      
      exchange.send(new HttpResponse(200, "text/plain", "OK"));
    }
View Full Code Here

    public void onRequest(IHttpExchange exchange) throws IOException {

      // send response
      HttpResponseHeader responseHeader = new HttpResponseHeader(200, "text/plain");

      IHttpRequest request = exchange.getRequest();
     
      BodyDataSink bodyDataSink = exchange.send(responseHeader);
      bodyDataSink.write("requestUri=" + request.getRequestURI() + "\r\n");
      bodyDataSink.write("queryString=" + request.getQueryString() + "\r\n");

      Set<String> paramNames = new TreeSet<String>(request.getParameterNameSet());
      for (String key : paramNames) {
        String[] values = request.getParameterValues(key);
        for (String value : values) {
          bodyDataSink.write("[param] " + key  + "=" + value + "\r\n")
        }
      }
View Full Code Here

     
        IHttpRequestHandler reqHdl = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifNoneMatch = request.getHeader("If-None-Match");
                if ((ifNoneMatch != null) && (ifNoneMatch.equals("\"23\""))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("ETag", "\"23\"");
                exchange.send(resp);
            }
        };
       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
        request.setHeader("If-None-Match", "\"23\"");
        IHttpResponse resp = httpClient.call(request);
        Assert.assertEquals(304, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
View Full Code Here

            private final String date = HttpUtils.toRFC1123DateString(new Date());
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
               
                IHttpRequest request = exchange.getRequest();
                String ifModifiedSince = request.getHeader("If-Modified-Since");
                if ((ifModifiedSince != null) && (ifModifiedSince.equals(date))) {
                    exchange.send(new HttpResponse(304));
                    return;
                }
               
                HttpResponse resp = new HttpResponse(200, "text/plain", "test");
                resp.setHeader("Last-Modified", date);
                exchange.send(resp);
            }
        };
       
       
        HttpServer server = new HttpServer(reqHdl);
        server.start();
       
        HttpClient httpClient = new HttpClient();
        httpClient.setCacheMaxSizeKB(100);
       
        IHttpResponse resp = httpClient.call(new GetRequest("http://localhost:" + server.getLocalPort() + "/"));
        Assert.assertEquals(200, resp.getStatus());
        Assert.assertEquals("test", resp.getBody().readString());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
        String lastModified = resp.getHeader("Last-Modified");
       
        QAUtil.sleep(1000);
       
        IHttpRequest request = new GetRequest("http://localhost:" + server.getLocalPort() + "/");
        request.setHeader("If-Modified-Since", lastModified);
        resp = httpClient.call(request);
        Assert.assertEquals(304, resp.getStatus());
        Assert.assertNull(resp.getHeader(CacheHandler.XHEADER_NAME));
       
        QAUtil.sleep(1000);
View Full Code Here

    }
   
   
   
    public void onMessage(IHttpMessage message) throws IOException {
      IHttpRequest request = (IHttpRequest) message;
     
   
      // handle Connection header
      handleLifeCycleHeaders(request);
     
     
      if (LOG.isLoggable(Level.FINE)) {
       
        if (request.getNonBlockingBody() == null) {
          LOG.fine("[" + getId() + "] bodyless request received from " + getRemoteAddress() +
               ":" + getRemotePort() +
               " (" + getCountMessagesReceived() + ". request) " + request.getRequestHeader().toString());
       
        } else {
          String body = "";
         
          String contentType = request.getContentType();
          if ((contentType != null) && (contentType.startsWith("application/x-www-form-urlencode"))) {
            body = request.getNonBlockingBody().toString() + "\n";           
          }
         
          LOG.fine("[" + getId() + "] request received  from " + getRemoteAddress() +
               ":" + getRemotePort() +
               " (" + getCountMessagesReceived() + ". request) " + request.getRequestHeader().toString() + body);
        }
      }
     
     
     
   
       
      // handle upgrade header
      if (isAutohandleUpgadeHeader) {
        String upgrade = request.getRequestHeader().getUpgrade();
        if ((upgrade != null) && upgrade.equalsIgnoreCase("TLS/1.0")) {

          if (getUnderlyingTcpConnection().isSecuredModeActivateable()) {
            suspendReceiving();
           
            HttpResponse response = new HttpResponse(101);
            response.setHeader("Connection", "Upgrade");
            response.setHeader("Upgrade", "TLS/1.0, HTTP/1.1");
            writeMessage(response, false);
           
            getUnderlyingTcpConnection().activateSecuredMode();
         
            resumeReceiving();
           
          } else {
            send(new HttpResponse(400, "text/html", generateErrorMessageHtml(400, "upgrade TLS is not supported", getId())));
            return;
          }
         
          return;
        }
      }


      // handle 100 continue header
      if (isAutconfirmExpect100ContinueHeader) {
        String expectHeader = request.getHeader("Expect");
        if ((expectHeader != null) && expectHeader.equalsIgnoreCase("100-Continue")) {
          writeMessage(new HttpResponse(100), false);
        }
      }



      if (message.hasBody()) {
         
        if (getBodyDataReceiveTimeoutMillis() != Long.MAX_VALUE) {
          message.getNonBlockingBody().setBodyDataReceiveTimeoutMillis(getBodyDataReceiveTimeoutMillis());
        }

                // is FORM encoded request?
                if (isContentTypeFormUrlencoded(request)) {
           
            final IHttpRequest req = request;
          IBodyCompleteListener cl = new IBodyCompleteListener() {
         
            @Execution(Execution.NONTHREADED)
            public void onComplete() throws IOException {
              handleMessage(newFormEncodedRequestWrapper(req));
View Full Code Here

    }
   

    public void run() {

      IHttpRequest request = null;
     
      do {
        request = requestQueue.poll();

        if (request != null) {
View Full Code Here

       
        IHttpRequestHandler rh = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
                IHttpRequest request = exchange.getRequest();
               
                BodyDataSink dataSink = exchange.forward(request.getRequestHeader());
                dataSink.write("addedLine\r\n");
                dataSink.write(request.getBody().readString());
                dataSink.close();
            }
        };
        chain.addLast(rh);
       
       
        IHttpRequestHandler rh2 = new IHttpRequestHandler() {
           
            public void onRequest(IHttpExchange exchange) throws IOException, BadMessageException {
                IHttpRequest request = exchange.getRequest();
                exchange.send(new HttpResponse(200, request.getBody().readString()));
            }
        };
        chain.addLast(rh2);

       
View Full Code Here

TOP

Related Classes of org.xlightweb.IHttpRequest

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.