I am trying to build a mini-webserver on an iPad inside an Adobe AIR application. I am using the ServerSocket class to set up the webserver as shown by example at http://coenraets.org/blog/2010/07/creating-a-web-server-using-the-new-server-socket-api-in-air-2/.
I am trying to extend this by allowing the webserver to serve mp4 video files.
I have added the mimetype: mimeTypes[".mp4"] = "video/mp4";
but serving an mp4 to be played in a browser is not exactly the same as providing a html file.
Anyone can point me in the right direction for this? This is the code I currently have when the socket is responding:
private function webserverConnectionSocketDataHandler(evt:ProgressEvent):void{
try
{
var socket:Socket = evt.target as Socket;
var bytes:ByteArray = new ByteArray();
socket.readBytes(bytes);
var request:String = "" + bytes;
var filePath:String = request.substring(5, request.indexOf("HTTP/") - 1);
var file:File = File.applicationDirectory.resolvePath("AppStorage").resolvePath(filePath);
if (file.exists && !file.isDirectory)
{
//Range: bytes=0-131726
var offset:uint = 0;
var bytesRequested:uint = 0;
if(request.indexOf("Range: bytes=") >= 0){
offset = uint(request.split("Range: bytes=")[1].split("-")[0]);
bytesRequested = (request.split("Range: bytes=")[1].split("-")[1]);
}
if(bytesRequested == 0) bytesRequested = file.size;
var stream:FileStream = new FileStream();
stream.open( file, FileMode.READ );
var content:ByteArray = new ByteArray();
stream.readBytes(content, offset, bytesRequested);
stream.close();
if(bytesRequested != file.size){
socket.writeUTFBytes("HTTP/1.1 206 Partial Content\n");
socket.writeUTFBytes("Content-Type: " + getMimeType(filePath) + "\n\n");
socket.writeUTFBytes("Content-Range: " + offset + "-" + bytesRequested + "/" + file.size + "\n\n");
socket.writeUTFBytes("Content-Length: " + (bytesRequested-offset) + "\n\n");
socket.writeUTFBytes("Date: " + new Date().toLocaleTimeString() + "\n\n");
}else{
socket.writeUTFBytes("HTTP/1.1 200 OK\n");
socket.writeUTFBytes("Content-Type: " + getMimeType(filePath) + "\n\n");
socket.writeUTFBytes("Content-Length: " + file.size + "\n\n");
}
socket.writeBytes(content);
}
else
{
socket.writeUTFBytes("HTTP/1.1 404 Not Found\n");
socket.writeUTFBytes("Content-Type: text/html\n\n");
socket.writeUTFBytes("<html><body><h2>Page Not Found</h2></body></html>");
}
socket.flush();
socket.close();
}
catch (error:Error)
{
trace(error.message, "Error");
}
}
But that is not really working as expected. The goal is to be able to view an mp4 from inside my iPad application's AppStorage directory from inside my browser on PC. It works for html files, not for mp4.