Skip to content

Instantly share code, notes, and snippets.

@DeepSky8
Created January 20, 2015 14:33
Show Gist options
  • Select an option

  • Save DeepSky8/1cffae6deb310deedbc3 to your computer and use it in GitHub Desktop.

Select an option

Save DeepSky8/1cffae6deb310deedbc3 to your computer and use it in GitHub Desktop.
Trying to handle different types of requests, and have a recorded compilation of the data that the browsers sent when creating the request
import java.net._
import java.io._
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.File
import java.io.{ BufferedReader, InputStreamReader, File, FileInputStream, InputStream, OutputStream, FileOutputStream, FileReader }
import javax.swing.ImageIcon;
class Request(method: String, version: String, pathway: String) {
def returnMethod(method: String): String {
}
def returnVersion(version: String): String {
}
def returnPathway(pathway: String): String {
}
}
object ServeFiles{
def Listen(): Unit = {
val doorbell = new Thread(new Runnable {
val request = new ServerSocket(80)
override def run(): Unit = {
while (true) {
val doorway = request.accept
Spawn(doorway)
}
}
})
doorbell.start()
}
def Spawn(doorway: Socket): Unit = {
val answer = new Thread(new Runnable {
override def run(): Unit = {
if (doorway != null) {
val requestReader = new BufferedReader(new InputStreamReader(doorway.getInputStream))
while(requestReader.ready()) {
val requestLine = requestReader.readLine
println(requestLine)
if(requestLine != null) {
val label = requestLine.split(" ")(0)
label match {
case "GET" => pathway(requestLine)
case "Host:" => collector(requestLine)
}
}
}
doorway.close
}
def collector(string: String): Request = {
new Request()
}
def encodeData(string: String): Array[String] = {
val first = string.split(" ")(0)
val second = string.split(" ")(1)
Array(first, second)
}
def pathway(string: String): Unit = {
val method = string.split(" ")(0)
val pathway = string.split(" ")(1).tail
val version = string.split(" ")(2)
println(pathway)
serverLocation(pathway)
// Array(method, pathway, version)
}
def serverLocation(pathway: String): Unit = {
val getFile = new File("C:\\Server\\" + pathway)
val fileReader = new FileReader(getFile)
val outsideConnection = doorway.getOutputStream()
val tenBytes = new Array[Char](10)
var bytesRead = 0
do {
bytesRead = fileReader.read(tenBytes)
outsideConnection.write(tenBytes.take(bytesRead).map(_.toByte))
} while(bytesRead >= 10)
}
}
})
answer.start
}
def main(args: Array[String]): Unit = {
Listen
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment