Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"/" or empty request is autocompleted to "/index.html" #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 68 additions & 60 deletions UniWebServer/Assets/UniWebServer/Scripts/FileServer.cs
Original file line number Diff line number Diff line change
@@ -1,61 +1,69 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;


namespace UniWebServer
{
[RequireComponent(typeof(EmbeddedWebServerComponent))]
public class FileServer : MonoBehaviour, IWebResource
{
public string folderPath = "/hextris";
EmbeddedWebServerComponent server;

void Start()
{
server = GetComponent<EmbeddedWebServerComponent>();
server.AddResource(folderPath, this);
}

public void HandleRequest(Request request, Response response)
{
// check if file exist at folder (need to assume a base local root)
string folderRoot = Application.streamingAssetsPath;
string fullPath = folderRoot + Uri.UnescapeDataString(request.uri.LocalPath);
// get file extension to add to header
string fileExt = Path.GetExtension(fullPath);
// not found
if (!File.Exists(fullPath)) {
response.statusCode = 404;
response.message = "Not Found";
return;
}

// serve the file
response.statusCode = 200;
response.message = "OK";
response.headers.Add("Content-Type", MimeTypeMap.GetMimeType(fileExt));

// read file and set bytes
using (FileStream fs = File.OpenRead(fullPath))
{
int length = (int)fs.Length;
byte[] buffer;

// add content length
response.headers.Add("Content-Length", length.ToString());

// use binary for mostly all except text
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
response.SetBytes(buffer);

}
}

}
}
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;


namespace UniWebServer
{
[RequireComponent(typeof(EmbeddedWebServerComponent))]
public class FileServer : MonoBehaviour, IWebResource
{
public string folderPath = "/hextris";
EmbeddedWebServerComponent server;

void Start()
{
server = GetComponent<EmbeddedWebServerComponent>();
server.AddResource(folderPath, this);
}

public void HandleRequest(Request request, Response response)
{
// check if file exist at folder (need to assume a base local root)
string folderRoot = Application.streamingAssetsPath;

// if path is empty, assume request for index.html
string localPath = Uri.UnescapeDataString(request.uri.LocalPath);
if(string.IsNullOrEmpty(localPath) || localPath == "/")
{
localPath = "/index.html";
}

string fullPath = folderRoot + localPath;
// get file extension to add to header
string fileExt = Path.GetExtension(fullPath);
// not found
if (!File.Exists(fullPath)) {
response.statusCode = 404;
response.message = "Not Found";
return;
}

// serve the file
response.statusCode = 200;
response.message = "OK";
response.headers.Add("Content-Type", MimeTypeMap.GetMimeType(fileExt));

// read file and set bytes
using (FileStream fs = File.OpenRead(fullPath))
{
int length = (int)fs.Length;
byte[] buffer;

// add content length
response.headers.Add("Content-Length", length.ToString());

// use binary for mostly all except text
using (BinaryReader br = new BinaryReader(fs))
{
buffer = br.ReadBytes(length);
}
response.SetBytes(buffer);

}
}

}
}