Package org.w3c.www.mime

Examples of org.w3c.www.mime.MimeParser


      reply.setContentType(MimeType.TEXT_PLAIN);
      reply.setStream(process.getInputStream());
      return reply;
  }
  // We MUST parse at least one header:
  MimeParser p = new MimeParser(process.getInputStream(),
              new CGIHeaderHolderFactory());
  Reply           reply  = null ;
  try {
      CGIHeaderHolder h = (CGIHeaderHolder) p.parse();
      // Check for a status code:
      String svalue   = h.getStatus();
      String location = h.getLocation();
      if ( svalue != null ) {
    int status = -1;
    try {
                    String _st = svalue.trim();
        int _space = _st.indexOf(' ');
                    if (_space != -1) {
      _st = _st.substring(0, _space);
        }
        status = Integer.parseInt(_st);
    } catch (Exception ex) {
        // This script has emited an invalid status line:
        String msg = ("Emited an invalid status line ["+
          svalue + "].");
        getServer().errlog(this, msg);
        // Throw an HTTPException:
        reply = request.makeReply(HTTP.INTERNAL_SERVER_ERROR);
        reply.setContent("CGI script emited invalid status.");
        throw new HTTPException(reply);
    }
    // we will use the default for this frame, but remove
    // the length calculated from the script size.
    reply = createDefaultReply(request, status);
    reply.setContentLength(-1);
      } else {
    // No status code available, any location header ?
    if (location != null) {
        reply = request.makeReply(HTTP.FOUND);
    } else {
        reply = createDefaultReply(request, HTTP.OK);
        reply.setContentLength(-1);
    }
      }
      // Set up the location header if needed:
      if ( location != null ) {
    try {
        reply.setLocation(new URL(getURL(request), location));
    } catch (MalformedURLException ex) {
        // This should really not happen:
        getServer().errlog(this, "unable to create location url "+
               location+
               " in base "+getURL(request));
    }
      }
      // And then, the remaining headers:
      Enumeration e = h.enumerateHeaders();
      if ( e != null ) {
    while ( e.hasMoreElements() ) {
        String hname = (String) e.nextElement();
        reply.setValue(hname, (String) h.getValue(hname));
    }
      }
      reply.setStream(p.getInputStream()) ;
  } catch (IOException ex) {
      ex.printStackTrace();
  } catch (MimeParserException ex) {
      // This script has generated invalid output:
      String msg = (getURL(request)
View Full Code Here


    } catch (FileNotFoundException fex) {
        // should never happen
    }
    CountInputStream cis = new CountInputStream(fis);
    MimeParserReplyFactory repfact = new MimeParserReplyFactory();
    MimeParser mp = new MimeParser(cis, repfact);
    try {
        asisreply = (HttpReplyMessage) mp.parse();
    } catch (MimeParserException mex) {
        // probably a "normal" file, serve it as-is...
        return;
    } catch (IOException ioex) {
        // silently fail, iot will fail later anyway
View Full Code Here

  int       processed = 0;
  ClientException err = null;

  this.input  = in;
  this.output = out;
  this.parser = new MimeParser(input, factory);
  try {
      running = true;
  alive_loop:
      while ( (! interrupted&& keep ) {
    // Get the next available request, and  mark client as used:
View Full Code Here

    TimedSocket ts = new TimedSocket();
    socket = ts.getSocket(inetaddr, port);
    socket.setSoTimeout(timeout);
    output = new BufferedOutputStream(socket.getOutputStream());
    input  = new BufferedInputStream(socket.getInputStream());
    parser = new MimeParser(input, reply_factory);
    cached = false;
      } catch (Throwable ex) {
    if (debug) {
        ex.printStackTrace();
    }
View Full Code Here

      acquireConnection();
      s = conn.connect(80);
      OutputStream os     = (new DataOutputStream
           (new BufferedOutputStream
            (s.getOutputStream())));
      MimeParser   parser = new MimeParser(s.getInputStream()
             , manager.getReplyFactory());
      if ( isTwoStage(req) ) {
    // Emit the request headers:
    req.emit(os, Request.EMIT_HEADERS);
    os.flush();
    if ( o != null )
        notifyObserver(o, new ConnectedEvent(this, req, os));
    rep = (Reply) parser.parse();
    // Wait for a 100 status code:
    if ((rep.getStatus() / 100) == 1) {
        // Notify the observer if any:
        if ( o != null )
      notifyObserver(o, new ContinueEvent(this, req, rep));
        // Finish the request normally:
        req.emit(os, Request.EMIT_BODY|Request.EMIT_FOOTERS);
        os.flush();
        rep = (Reply) parser.parse();
    }
      } else {
    req.emit(os, Request.EMIT_HEADERS);
    os.flush();
    if ( o != null )
        notifyObserver(o, new ConnectedEvent(this, req, os));
    rep = (Reply) parser.parse();
    while ((rep.getStatus() / 100) == 1) {
        if ( o != null )
      notifyObserver(o, new ContinueEvent(this, req, rep));
        // Finish the request normally:
        req.emit(os, Request.EMIT_BODY|Request.EMIT_FOOTERS);
        os.flush();
        rep = (Reply) parser.parse();
    }
      }
      os.close();
  } catch (IOException ex) {
      ex.printStackTrace();
View Full Code Here

    {
  if ( debug )
      System.out.println(conn+": runs[11] "+request.getURL());
  RequestObserver o  = request.getObserver();
  OutputStream os    = conn.getOutputStream();
  MimeParser   p     = conn.getParser();
  Reply        reply = null;
  boolean      needsChunk = false;

  if (request.hasOutputStream()) {
      if (request.getContentLength() < 0 ) {
    needsChunk = true;
    String tes[] = request.getTransferEncoding();
    if (tes == null) {
        // FIXME intern this
        request.addTransferEncoding("chunked");
    } else {
        boolean addIt = true;
        for (int i=0; !addIt && (i < tes.length); i++) {
      addIt = addIt && !tes[i].equals("chunked");
        }
        if (addIt) {
      // FIXME intern this
                        request.addTransferEncoding("chunked");
        } else {
      if (os instanceof ChunkedOutputStream) {
          needsChunk = false;
      }
        }
    }
      }  
  }

  try {
      request.emit(os, Request.EMIT_HEADERS);
      os.flush();
      if (o != null) {
    notifyObserver(o, new ConnectedEvent(this, request, os));
      }
      // We don't expect a "100-continue" so let's dump the body
      // If any...
      if ( request.hasOutputStream() ) {
    String       exp   = request.getExpect();
    if ((exp != null) && (exp.equalsIgnoreCase("100-continue"))) {
        if ( o != null) {
      // client is expecting a 100 continue let's fake one
      notifyObserver(o, new ContinueEvent(this, request));
        }
    }
    if (needsChunk) {
        DataOutputStream dos = new DataOutputStream(os);
        ChunkedOutputStream cos = new ChunkedOutputStream(dos);
        request.emit(cos, Request.EMIT_BODY);
        cos.flush();
        cos.close(false);
        request.emit(os,Request.EMIT_FOOTERS);
    } else {
        request.emit(os,Request.EMIT_BODY|Request.EMIT_FOOTERS);
    }
    os.flush();
      }
      reply = (Reply) p.parse(manager.isLenient());
      // "eat" the 100 replies FIXME it indicates an error in
      // the upstream server
      while ((reply.getStatus() / 100) == 1 ) {
    if (o != null) {
        notifyObserver(o, new ContinueEvent(this, request, reply));
    }
    reply = (Reply) p.parse(manager.isLenient());
      }
  } catch (MimeParserException ex) {
      return null;
  } catch (IOException ioex) {
      return null;
View Full Code Here

    {
  if ( debug )
      System.out.println(conn+": runs[11ts] "+request.getURL());
  RequestObserver o  = request.getObserver();
  OutputStream os    = conn.getOutputStream();
  MimeParser   p     = conn.getParser();
  Reply        reply = null;
  boolean needsChunk = false;
 
  if (request.getContentLength() < 0 ) {
      needsChunk = true;
      String tes[] = request.getTransferEncoding();
      if (tes == null) {
    request.addTransferEncoding("chunked"); // FIXME intern this
      } else {
    boolean addIt = true;
    for (int i=0; !addIt && (i < tes.length); i++) {
        addIt = addIt && !tes[i].equals("chunked");
    }
    if (addIt) {
        // FIXME intern thi
        request.addTransferEncoding("chunked");
    } else {
        if (os instanceof ChunkedOutputStream) {
      needsChunk = false;
        }
    }
      }
  }
 
  try {
      request.emit(os, Request.EMIT_HEADERS);
      os.flush();
      if ( o != null )
    notifyObserver(o, new ConnectedEvent(this, request, os));
      reply = (Reply) p.parse(manager.isLenient());
     
      boolean bodySent = false;
      while ((reply.getStatus() / 100) == 1 ||
       reply.getStatus() == HTTP.EXPECTATION_FAILED) {
    if (reply.getStatus() == HTTP.EXPECTATION_FAILED)
        return reply; // FIXME observer?
    reply = null;
    // Notify the observer if any:
    if ( o != null ) {
        notifyObserver(o, new ContinueEvent(this, request, reply));
    }
    // Finish the request normally:
    if (!bodySent) {
        bodySent = true;
        if (needsChunk) {
      DataOutputStream dos = new DataOutputStream(os);
      ChunkedOutputStream cos = new ChunkedOutputStream(dos);
      request.emit(cos, Request.EMIT_BODY);
      cos.flush();
      cos.close(false);
      request.emit(os,Request.EMIT_FOOTERS);
        } else {
      request.emit(os,
             Request.EMIT_BODY|Request.EMIT_FOOTERS);
        }
        os.flush();
    }
    reply = (Reply) p.parse(manager.isLenient());
    // if we don't have any observer, eat the 100 continue!
    while ((reply.getStatus() / 100) == 1 ) {
        if (o != null) {
      notifyObserver(o,
                    new ContinueEvent(this, request, reply));
        }
        reply = (Reply) p.parse(manager.isLenient());
    }
      }
  } catch (MimeParserException ex) {
      return null;
  } catch (IOException ieox) {
View Full Code Here

    {
  if ( debug )
      System.out.println(conn+": runs[10_ka] "+request.getURL());
  RequestObserver o  = request.getObserver();
  OutputStream os    = conn.getOutputStream();
  MimeParser   p     = conn.getParser();
  Reply        reply = null;
  String       exp   = request.getExpect();

  if ((exp != null) && (exp.equalsIgnoreCase("100-continue"))) {
      reply = request.makeReply(HTTP.EXPECTATION_FAILED);
      reply.setContent("100-continue is not supported by upstream "
           + "HTTP/1.0 Server");
      return reply;
  }
  if (request.getConnection() == null) {
      if ( request.hasProxy() ) {
    request.addProxyConnection("Keep-Alive");
      } else {
    request.addConnection("Keep-Alive");
      }
  }
  try {
      request.emit(os, Request.EMIT_HEADERS);
      os.flush();
  } catch (IOException ioex) {
      return null;
  }
  if ( o != null )
      notifyObserver(o, new ConnectedEvent(this, request, os));
  // If this is a two stage method, emit a fake continue event:
  if ( isTwoStage_10(request) ) {
      if ( o != null )
    notifyObserver(o, new ContinueEvent(this, request));
      request.emit(os, Request.EMIT_BODY|Request.EMIT_FOOTERS);
      os.flush();
  }
  try {
      reply = (Reply) p.parse(manager.isLenient());
      // if ever we switch to 1.1 in the meantime...
      while ((reply.getStatus() / 100) == 1 ) {
    if (o != null) {
        notifyObserver(o, new ContinueEvent(this, request, reply));
    }
    reply = null;
    reply = (Reply) p.parse(manager.isLenient());
      }
  } catch (IOException ex) {
      // at this point, we try to parse the reply if we have a body
      if (isTwoStage_10(request)) {
    try {
        reply = (Reply) p.parse(manager.isLenient());
    } catch (MimeParserException mex) {
        // a stale connection?
        return null;
    }
    try {
View Full Code Here

    {
  if ( debug )
      System.out.println(conn+": runs[10] "+request.getURL());
  RequestObserver o  = request.getObserver();
  OutputStream os    = conn.getOutputStream();
  MimeParser   p     = conn.getParser();
  Reply        reply = null;
  String       exp   = request.getExpect();

  if ((exp != null) && (exp.equalsIgnoreCase("100-continue"))) {
      reply = request.makeReply(HTTP.EXPECTATION_FAILED);
      reply.setContent("100-continue is not supported by upstream "
           + "HTTP/1.0 Server");
      return reply;
  }
  // Emit the request headers:
  request.emit(os, Request.EMIT_HEADERS);
  os.flush();
  if ( o != null ) {
      notifyObserver(o, new ConnectedEvent(this, request, os));
      // If this is a two stage method, emit a fake continue event:
      if ( isTwoStage_10(request) )
    notifyObserver(o, new ContinueEvent(this, request));
  }
  // Then emit the body:
  try {
      request.emit(os, Request.EMIT_BODY|Request.EMIT_FOOTERS);
      os.flush();
  } catch (IOException ex) {
      // at this point, we try to parse the reply if we have a body
      if (isTwoStage_10(request)) {
    try {
        reply = (Reply) p.parse(manager.isLenient());
    } catch (MimeParserException mex) {
        // perhaps nothing so...
        throw ex;
    }
    if (reply != null) {
                    // close the request stream
        try {
      request.getOutputStream().close();
        } catch (Exception cex) {};
        return reply;
    }
      }
      // no reply throw the exception again
      throw ex;
  }
  // HTTP 100 status codes *are* forbidden here, unless the server
  // switches...
  try {
      reply = (Reply) p.parse(manager.isLenient());
      while ((reply.getStatus() / 100) == 1 ) {
    if (o != null) {
        notifyObserver(o, new ContinueEvent(this, request, reply));
    }
    reply = null;
    reply = (Reply) p.parse(manager.isLenient());
      }
  } catch (MimeParserException ex) {
      // be nice to lamers
  }
  conn.setCloseOnEOF(true);
View Full Code Here

  // then issue the request if it's ok. (expect 100 continue with
  // a timeout?
  // Emit the request:
  RequestObserver o  = request.getObserver();
  OutputStream os    = conn.getOutputStream();
  MimeParser   p     = conn.getParser();
  Reply        reply = null;
  // Emit the request headers:
  request.emit(os, Request.EMIT_HEADERS);
  if ( o != null ) {
      notifyObserver(o, new ConnectedEvent(this, request, os));
      // If this is a two stage method, try to be nice:
      if ( isTwoStage_10(request) ) {
    // Always emit a fake continue event:
    eventfaked = true;
    notifyObserver(o, new ContinueEvent(this, request));
      }
  }
  // Then emit the body:
  try {
      request.emit(os, Request.EMIT_BODY|Request.EMIT_FOOTERS);
      os.flush();
  } catch (IOException ex) {
      // at this point, we try to parse the reply if we have a body
      if (isTwoStage_10(request)) {
    try {
        reply = (Reply) p.parse(manager.isLenient());
    } catch (MimeParserException mex) {
        // perhaps nothing so...
        throw ex;
    }
    if (reply != null) {
                    // close the request stream
        try {
      request.getOutputStream().close();
        } catch (Exception cex) {};
        return reply;
    }
      }
      // no reply throw the exception again
      throw ex;
  }
  try {
      reply = (Reply) p.parse(manager.isLenient());
      while ((reply.getStatus() / 100) == 1) {
    // If we already faked an event, skip that one:
    if ( eventfaked ) {
        eventfaked = false;
        continue;
    }
    // Notify the observer, if any:
    if ( o != null )
        notifyObserver(o, new ContinueEvent(this, request, reply));
    // Get next reply:
    reply = (Reply) p.parse(manager.isLenient());
      }
      // Now, we know about that server, update infos:
      if ( reply != null ) {
    updateServerInfo(reply);
      }
View Full Code Here

TOP

Related Classes of org.w3c.www.mime.MimeParser

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.