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");
}
}
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;
}
Aggregations