Search in sources :

Example 1 with WrapURLConnection

use of php.runtime.ext.net.WrapURLConnection in project jphp by jphp-compiler.

the class PHttpClient method makeConnection.

@Signature
protected URLConnection makeConnection(PHttpRequest request, Environment env) throws Throwable {
    URL url = new URL(request.getUrl());
    URLConnection connection = proxy == null ? url.openConnection() : url.openConnection(proxy);
    if (connection instanceof HttpURLConnection) {
        HttpURLConnection httpURLConnection = (HttpURLConnection) connection;
        httpURLConnection.setReadTimeout(timeout);
        httpURLConnection.setConnectTimeout(timeout);
        httpURLConnection.setRequestMethod(request.getMethod());
        httpURLConnection.setDoInput(true);
        httpURLConnection.setDoOutput(true);
        // cookies.
        ArrayMemory cookies = request.getCookies();
        ForeachIterator iterator = cookies.foreachIterator(false, false);
        StringBuilder cookieHeader = new StringBuilder();
        while (iterator.next()) {
            String name = iterator.getStringKey();
            String value = iterator.getValue().toString();
            cookieHeader.append(URLEncoder.encode(name, "UTF-8")).append('=').append(URLEncoder.encode(value, "UTF-8")).append(';');
        }
        httpURLConnection.setRequestProperty("Cookie", cookieHeader.toString());
        ArrayMemory headers = request.getHeaders();
        iterator = headers.foreachIterator(false, false);
        while (iterator.next()) {
            String name = iterator.getStringKey();
            Memory value = iterator.getValue();
            if (value.isTraversable()) {
                ForeachIterator subIterator = value.getNewIterator(env);
                while (subIterator.next()) {
                    httpURLConnection.addRequestProperty(name, subIterator.getValue().toString());
                }
            } else {
                httpURLConnection.addRequestProperty(name, value.toString());
            }
        }
        Memory body = request.getBody(env);
        if (body.instanceOf(PHttpBody.class)) {
            body.toObject(PHttpBody.class).apply(env, ObjectMemory.valueOf(new WrapURLConnection(env, connection)));
        } else {
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
            writer.write(request.getBody(env).toBinaryString());
            writer.close();
        }
        return httpURLConnection;
    } else {
        throw new IllegalStateException("HttpClient::makeConnection() method must return instance of HttpURLConnection");
    }
}
Also used : ArrayMemory(php.runtime.memory.ArrayMemory) ForeachIterator(php.runtime.lang.ForeachIterator) Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) ObjectMemory(php.runtime.memory.ObjectMemory) StringMemory(php.runtime.memory.StringMemory) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) WrapURLConnection(php.runtime.ext.net.WrapURLConnection)

Example 2 with WrapURLConnection

use of php.runtime.ext.net.WrapURLConnection in project jphp by jphp-compiler.

the class PHttpMultipartFormBody method apply.

@Override
@Signature
public Memory apply(Environment env, Memory... args) throws IOException {
    WrapURLConnection connection = args[0].toObject(WrapURLConnection.class);
    URLConnection conn = connection.getWrappedObject();
    OutputStream out = connection.getOutputStream();
    conn.setRequestProperty("Cache-Control", "no-cache");
    conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + this.boundary);
    ForeachIterator iterator = fields.foreachIterator(false, false);
    while (iterator.next()) {
        StringBuilder sb = new StringBuilder();
        String name = iterator.getStringKey();
        Memory value = iterator.getValue();
        sb.append("--").append(boundary).append(CRLF);
        sb.append("Content-Disposition: form-data; name=\"").append(name).append("\"").append(CRLF);
        sb.append("Content-Type: text/plain; charset=").append(encoding).append(CRLF);
        sb.append(CRLF).append(value).append(CRLF);
        out.write(sb.toString().getBytes());
    }
    iterator = files.foreachIterator(false, false);
    while (iterator.next()) {
        StringBuilder sb = new StringBuilder();
        String name = iterator.getStringKey();
        String fileName = name;
        Memory value = iterator.getValue();
        InputStream inputStream = Stream.getInputStream(env, value);
        if (inputStream == null) {
            throw new IllegalStateException("File '" + name + "' is not valid file or stream");
        }
        if (value.instanceOf(Stream.class)) {
            Stream stream = value.toObject(Stream.class);
            fileName = new File(stream.getPath()).getName();
        } else {
            fileName = new File(value.toString()).getName();
        }
        sb.append("--").append(boundary).append(CRLF).append("Content-Disposition: form-data; name=\"").append(URLEncoder.encode(name, encoding)).append("\"; filename=\"").append(URLEncoder.encode(fileName, encoding)).append("\"").append(CRLF);
        sb.append("Content-Type: ").append(URLConnection.guessContentTypeFromName(fileName)).append(CRLF);
        sb.append("Content-Transfer-Encoding: binary").append(CRLF).append(CRLF);
        out.write(sb.toString().getBytes());
        byte[] buff = new byte[4096];
        int len;
        while ((len = inputStream.read(buff)) > 0) {
            out.write(buff, 0, len);
        }
        Stream.closeStream(env, inputStream);
        sb.append(CRLF);
    }
    out.write(("--" + boundary + "--").getBytes());
    out.write(CRLF.getBytes());
    return Memory.NULL;
}
Also used : Memory(php.runtime.Memory) ArrayMemory(php.runtime.memory.ArrayMemory) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) URLConnection(java.net.URLConnection) ForeachIterator(php.runtime.lang.ForeachIterator) OutputStream(java.io.OutputStream) Stream(php.runtime.ext.core.classes.stream.Stream) InputStream(java.io.InputStream) WrapURLConnection(php.runtime.ext.net.WrapURLConnection) File(java.io.File) Signature(php.runtime.annotation.Reflection.Signature)

Aggregations

Memory (php.runtime.Memory)2 WrapURLConnection (php.runtime.ext.net.WrapURLConnection)2 ForeachIterator (php.runtime.lang.ForeachIterator)2 ArrayMemory (php.runtime.memory.ArrayMemory)2 File (java.io.File)1 InputStream (java.io.InputStream)1 OutputStream (java.io.OutputStream)1 URLConnection (java.net.URLConnection)1 Signature (php.runtime.annotation.Reflection.Signature)1 Stream (php.runtime.ext.core.classes.stream.Stream)1 ObjectMemory (php.runtime.memory.ObjectMemory)1 StringMemory (php.runtime.memory.StringMemory)1