Search in sources :

Example 6 with MessageHeader

use of sun.net.www.MessageHeader in project jdk8u_jdk by JetBrains.

the class RequestURIServer method run.

public void run() {
    try {
        Socket sock = ss.accept();
        InputStream is = sock.getInputStream();
        OutputStream os = sock.getOutputStream();
        MessageHeader headers = new MessageHeader(is);
        String requestLine = headers.getValue(0);
        int first = requestLine.indexOf(' ');
        int second = requestLine.lastIndexOf(' ');
        String URIString = requestLine.substring(first + 1, second);
        URI requestURI = new URI(URIString);
        if (requestURI.getFragment() != null)
            os.write(replyFAILED.getBytes("UTF-8"));
        else
            os.write(replyOK.getBytes("UTF-8"));
        sock.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : MessageHeader(sun.net.www.MessageHeader)

Example 7 with MessageHeader

use of sun.net.www.MessageHeader in project jdk8u_jdk by JetBrains.

the class NoNTLM method test.

/**
     * Test the http protocol handler with the given authentication schemes
     * in the WWW-Authenticate header.
     */
static void test(String... schemes) throws IOException {
    // the authentication scheme that the client is expected to choose
    String expected = null;
    for (String s : schemes) {
        if (expected == null) {
            expected = s;
        } else if (s.equals("Digest")) {
            expected = s;
        }
    }
    // server reply
    String reply = authReplyFor(schemes);
    System.out.println("====================================");
    System.out.println("Expect client to choose: " + expected);
    System.out.println(reply);
    try (ServerSocket ss = new ServerSocket(0)) {
        Client.start(ss.getLocalPort());
        // client <--- 401 ---- server
        try (Socket s = ss.accept()) {
            new MessageHeader().parseHeader(s.getInputStream());
            s.getOutputStream().write(reply.getBytes("US-ASCII"));
        }
        // client ---- GET ---> server
        // client <--- 200 ---- server
        String auth;
        try (Socket s = ss.accept()) {
            MessageHeader mh = new MessageHeader();
            mh.parseHeader(s.getInputStream());
            s.getOutputStream().write(OKAY.getBytes("US-ASCII"));
            auth = mh.findValue("Authorization");
        }
        // check Authorization header
        if (auth == null)
            throw new RuntimeException("Authorization header not found");
        System.out.println("Server received Authorization header: " + auth);
        String[] values = auth.split(" ");
        if (!values[0].equals(expected))
            throw new RuntimeException("Unexpected value");
    }
}
Also used : MessageHeader(sun.net.www.MessageHeader)

Example 8 with MessageHeader

use of sun.net.www.MessageHeader in project jdk8u_jdk by JetBrains.

the class Manifest method stream.

/* Add a manifest file at current position in a stream
     */
public void stream(OutputStream os) throws IOException {
    PrintStream ps;
    if (os instanceof PrintStream) {
        ps = (PrintStream) os;
    } else {
        ps = new PrintStream(os);
    }
    /* the first header in the file should be the global one.
         * It should say "Manifest-Version: x.x"; if not add it
         */
    MessageHeader globals = entries.elementAt(0);
    if (globals.findValue("Manifest-Version") == null) {
        /* Assume this is a user-defined manifest.  If it has a Name: <..>
             * field, then it is not global, in which case we just add our own
             * global Manifest-version: <version>
             * If the first MessageHeader has no Name: <..>, we assume it
             * is a global header and so prepend Manifest to it.
             */
        String jdkVersion = System.getProperty("java.version");
        if (globals.findValue("Name") == null) {
            globals.prepend("Manifest-Version", VERSION);
            globals.add("Created-By", "Manifest JDK " + jdkVersion);
        } else {
            ps.print("Manifest-Version: " + VERSION + "\r\n" + "Created-By: " + jdkVersion + "\r\n\r\n");
        }
        ps.flush();
    }
    globals.print(ps);
    for (int i = 1; i < entries.size(); ++i) {
        MessageHeader mh = entries.elementAt(i);
        mh.print(ps);
    }
}
Also used : MessageHeader(sun.net.www.MessageHeader)

Example 9 with MessageHeader

use of sun.net.www.MessageHeader in project jdk8u_jdk by JetBrains.

the class SignatureFile method stream.

/**
     * Add a signature file at current position in a stream
     */
public void stream(OutputStream os) throws IOException {
    /* the first header in the file should be the global one.
         * It should say "SignatureFile-Version: x.x"; barf if not
         */
    MessageHeader globals = entries.elementAt(0);
    if (globals.findValue("Signature-Version") == null) {
        throw new JarException("Signature file requires " + "Signature-Version: 1.0 in 1st header");
    }
    PrintStream ps = new PrintStream(os);
    globals.print(ps);
    for (int i = 1; i < entries.size(); ++i) {
        MessageHeader mh = entries.elementAt(i);
        mh.print(ps);
    }
}
Also used : MessageHeader(sun.net.www.MessageHeader)

Example 10 with MessageHeader

use of sun.net.www.MessageHeader in project jdk8u_jdk by JetBrains.

the class FtpURLConnection method getInputStream.

/**
     * Get the InputStream to retreive the remote file. It will issue the
     * "get" (or "dir") command to the ftp server.
     *
     * @return  the <code>InputStream</code> to the connection.
     *
     * @throws  IOException if already opened for output
     * @throws  FtpProtocolException if errors occur during the transfert.
     */
@Override
public InputStream getInputStream() throws IOException {
    if (!connected) {
        connect();
    }
    if (http != null) {
        return http.getInputStream();
    }
    if (os != null) {
        throw new IOException("Already opened for output");
    }
    if (is != null) {
        return is;
    }
    MessageHeader msgh = new MessageHeader();
    boolean isAdir = false;
    try {
        decodePath(url.getPath());
        if (filename == null || type == DIR) {
            ftp.setAsciiType();
            cd(pathname);
            if (filename == null) {
                is = new FtpInputStream(ftp, ftp.list(null));
            } else {
                is = new FtpInputStream(ftp, ftp.nameList(filename));
            }
        } else {
            if (type == ASCII) {
                ftp.setAsciiType();
            } else {
                ftp.setBinaryType();
            }
            cd(pathname);
            is = new FtpInputStream(ftp, ftp.getFileStream(filename));
        }
        /* Try to get the size of the file in bytes.  If that is
            successful, then create a MeteredStream. */
        try {
            long l = ftp.getLastTransferSize();
            msgh.add("content-length", Long.toString(l));
            if (l > 0) {
                // Wrap input stream with MeteredStream to ensure read() will always return -1
                // at expected length.
                // Check if URL should be metered
                boolean meteredInput = ProgressMonitor.getDefault().shouldMeterInput(url, "GET");
                ProgressSource pi = null;
                if (meteredInput) {
                    pi = new ProgressSource(url, "GET", l);
                    pi.beginTracking();
                }
                is = new MeteredStream(is, pi, l);
            }
        } catch (Exception e) {
            e.printStackTrace();
        /* do nothing, since all we were doing was trying to
            get the size in bytes of the file */
        }
        if (isAdir) {
            msgh.add("content-type", "text/plain");
            msgh.add("access-type", "directory");
        } else {
            msgh.add("access-type", "file");
            String ftype = guessContentTypeFromName(fullpath);
            if (ftype == null && is.markSupported()) {
                ftype = guessContentTypeFromStream(is);
            }
            if (ftype != null) {
                msgh.add("content-type", ftype);
            }
        }
    } catch (FileNotFoundException e) {
        try {
            cd(fullpath);
            /* if that worked, then make a directory listing
                and build an html stream with all the files in
                the directory */
            ftp.setAsciiType();
            is = new FtpInputStream(ftp, ftp.list(null));
            msgh.add("content-type", "text/plain");
            msgh.add("access-type", "directory");
        } catch (IOException ex) {
            throw new FileNotFoundException(fullpath);
        } catch (FtpProtocolException ex2) {
            throw new FileNotFoundException(fullpath);
        }
    } catch (FtpProtocolException ftpe) {
        throw new IOException(ftpe);
    }
    setProperties(msgh);
    return is;
}
Also used : ProgressSource(sun.net.ProgressSource) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MessageHeader(sun.net.www.MessageHeader) MeteredStream(sun.net.www.MeteredStream) FtpProtocolException(sun.net.ftp.FtpProtocolException) FtpProtocolException(sun.net.ftp.FtpProtocolException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

MessageHeader (sun.net.www.MessageHeader)20 HeaderParser (sun.net.www.HeaderParser)2 MeteredStream (sun.net.www.MeteredStream)2 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Iterator (java.util.Iterator)1 ProgressSource (sun.net.ProgressSource)1 FtpProtocolException (sun.net.ftp.FtpProtocolException)1