Skip to content

Commit

Permalink
jphp-httpserver-ext: Add support for http parts
Browse files Browse the repository at this point in the history
This commit adds a wrapper around javax.servlet.http.Part, which makes it easy to work with files transmitted via POST
MWGuy committed Apr 17, 2020
1 parent fe15ab2 commit 50fe068
Showing 8 changed files with 113 additions and 6 deletions.
2 changes: 1 addition & 1 deletion exts/jphp-httpserver-ext/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

def jettyVersion = '9.4.26.v20200117'
def jettyVersion = '9.4.28.v20200408'

dependencies {
provided project(':jphp-runtime')
Original file line number Diff line number Diff line change
@@ -9,6 +9,8 @@
import php.runtime.ext.support.Extension;
import php.runtime.memory.support.MemoryOperation;

import javax.servlet.http.Part;

class NoLogging implements Logger {
@Override public String getName() { return "no"; }
@Override public void warn(String msg, Object... args) { }
@@ -50,6 +52,7 @@ public String[] getPackageNames() {
@Override
public void onRegister(CompileScope scope) {
registerWrapperClass(scope, Session.class, PWebSocketSession.class);
registerWrapperClass(scope, Part.class, PHttpPart.class);
MemoryOperation.registerWrapper(WebSocketSession.class, PWebSocketSession.class);

registerClass(scope, PHttpServer.class);
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package org.develnext.jphp.ext.httpserver.classes;

import org.apache.commons.io.IOUtils;
import org.develnext.jphp.ext.httpserver.HttpServerExtension;
import php.runtime.annotation.Reflection;
import php.runtime.env.Environment;
import php.runtime.ext.core.classes.stream.DataStream;
import php.runtime.ext.core.classes.stream.MemoryStream;
import php.runtime.ext.core.classes.stream.ResourceStream;
import php.runtime.lang.BaseWrapper;
import php.runtime.reflection.ClassEntity;

import javax.servlet.http.Part;
import java.io.ByteArrayInputStream;
import java.io.IOException;

@Reflection.Name("HttpPart")
@Reflection.Namespace(HttpServerExtension.NS)
public class PHttpPart extends BaseWrapper<Part> {
public PHttpPart(Environment env, Part wrappedObject) {
super(env, wrappedObject);
}

public PHttpPart(Environment env, ClassEntity clazz) {
super(env, clazz);
}

@Reflection.Signature
public byte[] readAll() throws IOException {
ByteArrayInputStream inputStream = (ByteArrayInputStream) getWrappedObject().getInputStream();
return inputStream.readAllBytes();
}

@Reflection.Signature
public String getName() {
return getWrappedObject().getName();
}

@Reflection.Signature
public String getContentType() {
return getWrappedObject().getContentType();
}

@Reflection.Signature
public String getSubmittedFileName() {
return getWrappedObject().getSubmittedFileName();
}

@Reflection.Signature
public long getSize() {
return getWrappedObject().getSize();
}
}
Original file line number Diff line number Diff line change
@@ -16,12 +16,12 @@
import php.runtime.memory.StringMemory;
import php.runtime.reflection.ClassEntity;

import javax.servlet.MultipartConfigElement;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.Part;
import java.io.IOException;
import java.util.Enumeration;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.*;

@Name("HttpServerRequest")
@Namespace(HttpServerExtension.NS)
@@ -268,4 +268,10 @@ public Memory cookies(Environment env) {
public void end() {
request.setHandled(true);
}

@Signature
public Collection<Part> getParts() throws IOException, ServletException {
request.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, new MultipartConfigElement(""));
return request.getParts();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace php\http;

use php\io\Stream;

class HttpPart
{
/**
* @return string
*/
public function readAll(): string {}

/**
* @return string
*/
public function getName(): string {}

/**
* @return string
*/
public function getContentType(): string {}

/**
* @return string
*/
public function getSubmittedFileName(): string {}

/**
* @return int
*/
public function getSize(): int {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php
namespace php\http;
use php\io\IOException;
use php\io\Stream;
use php\lang\IllegalStateException;
use php\lang\JavaException;
use php\util\Locale;

/**
@@ -198,4 +200,13 @@ function locales(): array
function bodyStream(): Stream
{
}

/**
* @return HttpPart[]
* @throws JavaException
* @throws IOException
*/
function getParts(): array
{
}
}
1 change: 1 addition & 0 deletions sandbox/build.gradle
Original file line number Diff line number Diff line change
@@ -12,6 +12,7 @@ dependencies {
compile project(':exts:jphp-zend-ext')
compile project(':exts:jphp-json-ext')
compile project(':exts:jphp-semver-ext')
compile project(':exts:jphp-httpserver-ext')
//compile project(':jphp-json-ext')
//compile project(':jphp-jsoup-ext')
//compile project(':jphp-gdx-ext')
2 changes: 1 addition & 1 deletion sandbox/src/JPHP-INF/.bootstrap.php
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<?phpuse php\time\Time;function test() { $b = [1, 2, 3]; for ($i = 0; $i < 100000000; $i++) { $xa = $b[$i]; $xc = $b[$i]; $xb = $b[$i]; } return $xb + $xa + $xc;}function test2() { $a = ['x' => 1, 'y' => 2, 'z' => 3]; $x = 'x'; $y = 'y'; $z = 'z'; for ($i = 0; $i < 100000000; $i++) { $xa = $a[$x]; $xc = $a[$y]; $xb = $a[$z]; } return $xb + $xa + $xc;}$t = Time::millis();var_dump(test2());echo Time::millis() - $t, "ms";
<?phpuse php\http\HttpServer;use php\http\HttpServerRequest;use php\http\HttpServerResponse;use php\io\Stream;use php\lang\System;use php\lib\fs;use php\lib\str;$server = new HttpServer(8080);$server->get("/", function (HttpServerRequest $request, HttpServerResponse $response) { $response->write("<h1>Hello user!</h1><p>Upload files to me through the POST method</p>");});$server->post("/", function (HttpServerRequest $request, HttpServerResponse $response) { foreach ($request->getParts() as $part) { echo "Got new http part with name '{$part->getName()}'='{$part->getSubmittedFileName()}' and size '{$part->getSize()}'!\n"; $tempPath = generateTempPath(); Stream::putContents($tempPath, $part->readAll()); echo " -> saved as {$tempPath}\n"; } $response->write("ok");});$server->run();/** * @return string */function generateTempPath(): string { return fs::abs((System::getProperty("java.io.tempdir") ?: "/tmp") . "/" . str::uuid());}

0 comments on commit 50fe068

Please sign in to comment.