Skip to content

Commit 40400df

Browse files
committed
Merge pull request koush#264 from nutsiepully/pattern_fix
asyncHttpServer.directory fix. replaceAll replaces the entire url pattern.
2 parents 04f46c0 + e23ad66 commit 40400df

File tree

1 file changed

+8
-4
lines changed

1 file changed

+8
-4
lines changed

AndroidAsync/src/com/koushikdutta/async/http/server/AsyncHttpServer.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ public void post(String regex, HttpServerRequestCallback callback) {
332332
}
333333

334334
public static android.util.Pair<Integer, InputStream> getAssetStream(final Context context, String asset) {
335-
AssetManager am = context.getAssets();
335+
AssetManager am = context.getResources().getAssets();
336336
try {
337337
InputStream is = am.open(asset);
338338
return new android.util.Pair<Integer, InputStream>(is.available(), is);
@@ -373,12 +373,16 @@ public static String tryGetContentType(String path) {
373373
return null;
374374
}
375375

376+
private String replacePrefix(Matcher m) {
377+
return m.group(0).substring(m.end(1));
378+
}
379+
376380
public void directory(Context context, String regex, final String assetPath) {
377381
final Context _context = context.getApplicationContext();
378382
addAction(AsyncHttpGet.METHOD, regex, new HttpServerRequestCallback() {
379383
@Override
380384
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
381-
String path = request.getMatcher().replaceAll("");
385+
String path = replacePrefix(request.getMatcher());
382386
android.util.Pair<Integer, InputStream> pair = getAssetStream(_context, assetPath + path);
383387
final InputStream is = pair.second;
384388
response.getHeaders().getHeaders().set("Content-Length", String.valueOf(pair.first));
@@ -401,7 +405,7 @@ public void onCompleted(Exception ex) {
401405
addAction(AsyncHttpHead.METHOD, regex, new HttpServerRequestCallback() {
402406
@Override
403407
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
404-
String path = request.getMatcher().replaceAll("");
408+
String path = replacePrefix(request.getMatcher());
405409
android.util.Pair<Integer, InputStream> pair = getAssetStream(_context, assetPath + path);
406410
final InputStream is = pair.second;
407411
StreamUtility.closeQuietly(is);
@@ -428,7 +432,7 @@ public void directory(String regex, final File directory, final boolean list) {
428432
addAction("GET", regex, new HttpServerRequestCallback() {
429433
@Override
430434
public void onRequest(AsyncHttpServerRequest request, final AsyncHttpServerResponse response) {
431-
String path = request.getMatcher().replaceAll("");
435+
String path = replacePrefix(request.getMatcher());
432436
File file = new File(directory, path);
433437

434438
if (file.isDirectory() && list) {

0 commit comments

Comments
 (0)