Skip to content

Commit

Permalink
Add utf8 & send method to webserver
Browse files Browse the repository at this point in the history
  • Loading branch information
am3n committed Oct 21, 2024
1 parent 1a0cb8f commit 7893103
Show file tree
Hide file tree
Showing 18 changed files with 449 additions and 292 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# NeedTool

![MinAPI](https://img.shields.io/badge/API-21%2B-blue)
![MinAPI](https://img.shields.io/badge/API-19%2B-blue)
[![](https://jitpack.io/v/am3n/NeedTool.svg)](https://jitpack.io/#am3n/NeedTool)

Installation
Expand Down
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ android {

defaultConfig {
applicationId "ir.am3n.needtool.sample"
minSdk 16
minSdk 19
targetSdk 33
multiDexEnabled true
vectorDrawables.useSupportLibrary = true
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<activity
android:name=".MainActivity"
android:exported="true">

</activity>

<activity
android:name=".WebServerAct"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
Expand Down
95 changes: 95 additions & 0 deletions app/src/main/java/ir/am3n/needtool/sample/WebServerAct.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
package ir.am3n.needtool.sample

import android.annotation.SuppressLint
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import ir.am3n.needtool.webserver.HttpHandler
import ir.am3n.needtool.webserver.HttpServer
import kotlinx.android.synthetic.main.activity_webserver.serverButton
import kotlinx.android.synthetic.main.activity_webserver.serverTextView
import org.json.JSONObject
import java.io.InputStream
import java.net.InetSocketAddress
import java.util.Scanner
import java.util.concurrent.Executors

@SuppressLint("SetTextI18n")
class WebServerAct : AppCompatActivity() {

private var serverUp = false

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_webserver)

serverButton.setOnClickListener {
serverUp = if (!serverUp){
startServer(port = 5000)
true
} else{
stopServer()
false
}
}

}

private fun streamToString(inputStream: InputStream): String {
val s = Scanner(inputStream).useDelimiter("\\A")
return if (s.hasNext()) s.next() else ""
}

private var mHttpServer: HttpServer? = null


private fun startServer(port: Int) {
try {
mHttpServer = HttpServer.create(InetSocketAddress(port), 0)
mHttpServer!!.executor = Executors.newCachedThreadPool()
mHttpServer!!.createContext("/", rootHandler)
mHttpServer!!.createContext("/index", rootHandler)
mHttpServer!!.createContext("/messages", messageHandler)
mHttpServer!!.start()
serverTextView.text = "server running"
serverButton.text = "stop server"
} catch (e: Exception) {
e.printStackTrace()
}

}

private fun stopServer() {
if (mHttpServer != null) {
mHttpServer!!.stop(0)
serverTextView.text = "server down"
serverButton.text = "start server"
}
}

private val rootHandler = HttpHandler { exchange ->
run {
when (exchange!!.requestMethod) {
"GET" -> {
exchange.sendUTF8Response("Welcome to سلام my server po")
}
}
}
}

private val messageHandler = HttpHandler { exchange ->
run {
when (exchange!!.requestMethod) {
"GET" -> {
exchange.sendUTF8Response("Would be all messages stringified json")
}
"POST" -> {
val inputStream = exchange.requestBody
val requestBody = streamToString(inputStream)
val jsonBody = JSONObject(requestBody)
exchange.sendUTF8JsonResponse(jsonBody)
}
}
}
}

}
42 changes: 42 additions & 0 deletions app/src/main/res/layout/activity_webserver.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/activity_main">

<TextView
android:id="@+id/serverTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dp"
android:gravity="center"
android:text="server down"
android:textSize="16sp" />

<Button
android:id="@+id/serverButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:layout_marginEnd="20dp"
android:background="@color/colorPrimary"
android:text="start server"
android:textAppearance="@style/Base.TextAppearance.AppCompat.Inverse"
android:textColor="#ffffff" />

</LinearLayout>

</RelativeLayout>
2 changes: 1 addition & 1 deletion needtool/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ android {
compileSdk 33

defaultConfig {
minSdk 16
minSdk 19
targetSdk 33
multiDexEnabled true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class Headers implements Map<String, List<String>>
HashMap<String, List<String>> map;

public Headers() {
this.map = new HashMap<String, List<String>>(32);
this.map = new HashMap<>(32);
}

private String normalize(final String s) {
Expand Down Expand Up @@ -76,7 +76,7 @@ public void add(final String s, final String s2) {
final String normalize = this.normalize(s);
List<String> value = this.map.get(normalize);
if (value == null) {
value = new LinkedList<String>();
value = new LinkedList<>();
this.map.put(normalize, value);
}
value.add(s2);
Expand All @@ -85,7 +85,7 @@ public void add(final String s, final String s2) {
public void set(final String s, final String e) {
final LinkedList<String> list = new LinkedList<String>();
list.add(e);
this.put(s, (List<String>)list);
this.put(s, list);
}

public List<String> remove(final Object o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,18 @@

package ir.am3n.needtool.webserver;

import org.jetbrains.annotations.NotNull;
import org.json.JSONArray;
import org.json.JSONObject;

import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.net.InetSocketAddress;
import java.io.IOException;
import java.io.OutputStream;
import java.io.InputStream;
import java.net.URI;
import java.nio.charset.StandardCharsets;

public abstract class HttpExchange
{
Expand Down Expand Up @@ -48,4 +55,36 @@ protected HttpExchange() {
public abstract void setStreams(final InputStream p0, final OutputStream p1);

public abstract HttpPrincipal getPrincipal();

public void sendUTF8Response(String data) throws IOException {
this.getResponseHeaders().set("Content-Type", "text/plain; charset=utf-8");
send(data);
}

public void sendUTF8HtmlResponse(String data) throws IOException {
getResponseHeaders().set("Content-Type", "text/htnl; charset=utf-8");
send(data);
}

public void sendUTF8JsonResponse(@NotNull JSONObject oject) throws IOException {
getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
send(oject.toString());
}

public void sendUTF8JsonResponse(@NotNull JSONArray oject) throws IOException {
getResponseHeaders().set("Content-Type", "application/json; charset=utf-8");
send(oject.toString());
}

private void send(String data) throws IOException {
sendResponseHeaders(200, 0);
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(getResponseBody(), StandardCharsets.UTF_8),
8 * 1024
);
writer.write(data);
writer.flush();
writer.close();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,51 @@

import java.io.IOException;
import java.io.InputStream;

import ir.am3n.needtool.webserver.HttpExchange;
import ir.am3n.needtool.webserver.Authenticator;
import ir.am3n.needtool.webserver.Filter;

public class AuthFilter extends Filter
{
public class AuthFilter extends Filter {
private Authenticator authenticator;

public AuthFilter(final Authenticator authenticator) {
this.authenticator = authenticator;
}

@Override
public String description() {
return "Authentication filter";
}

public void setAuthenticator(final Authenticator authenticator) {
this.authenticator = authenticator;
}

public void consumeInput(final HttpExchange httpExchange) throws IOException {
final InputStream requestBody = httpExchange.getRequestBody();
while (requestBody.read(new byte[4096]) != -1) {}
while (requestBody.read(new byte[4096]) != -1) {
}
requestBody.close();
}

@Override
public void doFilter(final HttpExchange httpExchange, final Chain chain) throws IOException {
if (this.authenticator != null) {
final Authenticator.Result authenticate = this.authenticator.authenticate(httpExchange);
if (authenticate instanceof Authenticator.Success) {
ExchangeImpl.get(httpExchange).setPrincipal(((Authenticator.Success)authenticate).getPrincipal());
ExchangeImpl.get(httpExchange).setPrincipal(((Authenticator.Success) authenticate).getPrincipal());
chain.doFilter(httpExchange);
}
else if (authenticate instanceof Authenticator.Retry) {
final Authenticator.Retry retry = (Authenticator.Retry)authenticate;
} else if (authenticate instanceof Authenticator.Retry) {
final Authenticator.Retry retry = (Authenticator.Retry) authenticate;
this.consumeInput(httpExchange);
httpExchange.sendResponseHeaders(retry.getResponseCode(), -1L);
}
else if (authenticate instanceof Authenticator.Failure) {
final Authenticator.Failure failure = (Authenticator.Failure)authenticate;
} else if (authenticate instanceof Authenticator.Failure) {
final Authenticator.Failure failure = (Authenticator.Failure) authenticate;
this.consumeInput(httpExchange);
httpExchange.sendResponseHeaders(failure.getResponseCode(), -1L);
}
}
else {
} else {
chain.doFilter(httpExchange);
}
}
Expand Down
Loading

0 comments on commit 7893103

Please sign in to comment.