Skip to content

Commit 43787b2

Browse files
authored
Merge pull request #412 from MWGuy/http-server-update
jphp-httpserver-ext: Add support for http parts
2 parents fe15ab2 + d205d8b commit 43787b2

8 files changed

Lines changed: 112 additions & 6 deletions

File tree

exts/jphp-httpserver-ext/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
def jettyVersion = '9.4.26.v20200117'
2+
def jettyVersion = '9.4.28.v20200408'
33

44
dependencies {
55
provided project(':jphp-runtime')

exts/jphp-httpserver-ext/src/main/java/org/develnext/jphp/ext/httpserver/HttpServerExtension.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
import php.runtime.ext.support.Extension;
1010
import php.runtime.memory.support.MemoryOperation;
1111

12+
import javax.servlet.http.Part;
13+
1214
class NoLogging implements Logger {
1315
@Override public String getName() { return "no"; }
1416
@Override public void warn(String msg, Object... args) { }
@@ -50,6 +52,7 @@ public String[] getPackageNames() {
5052
@Override
5153
public void onRegister(CompileScope scope) {
5254
registerWrapperClass(scope, Session.class, PWebSocketSession.class);
55+
registerWrapperClass(scope, Part.class, PHttpPart.class);
5356
MemoryOperation.registerWrapper(WebSocketSession.class, PWebSocketSession.class);
5457

5558
registerClass(scope, PHttpServer.class);
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package org.develnext.jphp.ext.httpserver.classes;
2+
3+
import org.develnext.jphp.ext.httpserver.HttpServerExtension;
4+
import php.runtime.annotation.Reflection;
5+
import php.runtime.env.Environment;
6+
import php.runtime.lang.BaseWrapper;
7+
import php.runtime.reflection.ClassEntity;
8+
9+
import javax.servlet.http.Part;
10+
import java.io.ByteArrayInputStream;
11+
import java.io.IOException;
12+
13+
@Reflection.Name("HttpPart")
14+
@Reflection.Namespace(HttpServerExtension.NS)
15+
public class PHttpPart extends BaseWrapper<Part> {
16+
public PHttpPart(Environment env, Part wrappedObject) {
17+
super(env, wrappedObject);
18+
}
19+
20+
public PHttpPart(Environment env, ClassEntity clazz) {
21+
super(env, clazz);
22+
}
23+
24+
@Reflection.Signature
25+
public byte[] readAll() throws IOException {
26+
ByteArrayInputStream inputStream = (ByteArrayInputStream) getWrappedObject().getInputStream();
27+
byte[] array = new byte[inputStream.available()];
28+
inputStream.read(array);
29+
inputStream.close();
30+
return array;
31+
}
32+
33+
@Reflection.Signature
34+
public String getName() {
35+
return getWrappedObject().getName();
36+
}
37+
38+
@Reflection.Signature
39+
public String getContentType() {
40+
return getWrappedObject().getContentType();
41+
}
42+
43+
@Reflection.Signature
44+
public String getSubmittedFileName() {
45+
return getWrappedObject().getSubmittedFileName();
46+
}
47+
48+
@Reflection.Signature
49+
public long getSize() {
50+
return getWrappedObject().getSize();
51+
}
52+
}

exts/jphp-httpserver-ext/src/main/java/org/develnext/jphp/ext/httpserver/classes/PHttpServerRequest.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616
import php.runtime.memory.StringMemory;
1717
import php.runtime.reflection.ClassEntity;
1818

19+
import javax.servlet.MultipartConfigElement;
20+
import javax.servlet.ServletException;
1921
import javax.servlet.http.Cookie;
22+
import javax.servlet.http.Part;
2023
import java.io.IOException;
21-
import java.util.Enumeration;
22-
import java.util.List;
23-
import java.util.Locale;
24-
import java.util.Map;
24+
import java.util.*;
2525

2626
@Name("HttpServerRequest")
2727
@Namespace(HttpServerExtension.NS)
@@ -268,4 +268,10 @@ public Memory cookies(Environment env) {
268268
public void end() {
269269
request.setHandled(true);
270270
}
271+
272+
@Signature
273+
public Collection<Part> getParts() throws IOException, ServletException {
274+
request.setAttribute(Request.MULTIPART_CONFIG_ELEMENT, new MultipartConfigElement(""));
275+
return request.getParts();
276+
}
271277
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
namespace php\http;
4+
5+
use php\io\Stream;
6+
7+
class HttpPart
8+
{
9+
/**
10+
* @return string
11+
*/
12+
public function readAll(): string {}
13+
14+
/**
15+
* @return string
16+
*/
17+
public function getName(): string {}
18+
19+
/**
20+
* @return string
21+
*/
22+
public function getContentType(): string {}
23+
24+
/**
25+
* @return string
26+
*/
27+
public function getSubmittedFileName(): string {}
28+
29+
/**
30+
* @return int
31+
*/
32+
public function getSize(): int {}
33+
}

exts/jphp-httpserver-ext/src/main/resources/JPHP-INF/sdk/php/http/HttpServerRequest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
<?php
22
namespace php\http;
3+
use php\io\IOException;
34
use php\io\Stream;
45
use php\lang\IllegalStateException;
6+
use php\lang\JavaException;
57
use php\util\Locale;
68

79
/**
@@ -198,4 +200,13 @@ function locales(): array
198200
function bodyStream(): Stream
199201
{
200202
}
203+
204+
/**
205+
* @return HttpPart[]
206+
* @throws JavaException
207+
* @throws IOException
208+
*/
209+
function getParts(): array
210+
{
211+
}
201212
}

sandbox/build.gradle

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ dependencies {
1212
compile project(':exts:jphp-zend-ext')
1313
compile project(':exts:jphp-json-ext')
1414
compile project(':exts:jphp-semver-ext')
15+
compile project(':exts:jphp-httpserver-ext')
1516
//compile project(':jphp-json-ext')
1617
//compile project(':jphp-jsoup-ext')
1718
//compile project(':jphp-gdx-ext')

sandbox/src/JPHP-INF/.bootstrap.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +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";
1+
<?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 commit comments

Comments
 (0)