Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import std.stdio, std.socket,
- std.array, std.datetime,
- std.string, std.file;
- ushort port = 80;
- string vYol = "./Dhttp";
- string vDosya = "/index.html";
- void main(){
- Socket dinleyici = new TcpSocket;
- dinleyici.bind(new InternetAddress(port));
- dinleyici.listen(1);
- while(1){
- Socket soket = dinleyici.accept();
- char[2048] alınacak;
- string alınan;
- soket.receive(alınacak);
- bool hata = true;
- foreach(ch; alınacak){
- if(ch != '\xff'){
- alınan ~= ch;
- hata = false;
- }
- }
- if(hata) continue;
- string[] satır = split(alınan, "\n");
- string[][] kelime;
- kelime.length = satır.length;
- kelime[0].length = split(satır[0], " ").length;
- kelime[0] = split(satır[0], " ");
- for(size_t a = 1; a < kelime.length; a++){
- kelime[a] = split(satır[a], ": ");
- for(size_t b = 0; b < kelime[a].length; b++)
- kelime[a][b] = chomp(kelime[a][b].idup).dup;
- }
- GP gp;
- gp = fGP(kelime);
- writeln("IP: ", soket.remoteAddress(),
- "\n\tURL: ", gp.url,
- "\n--------------------\n");
- soket.send(gp.message);
- soket.close();
- }
- }
- struct GP{
- string url;
- string protocol;
- string host;
- string connection;
- string cacheControl;
- string aEncoding;
- string aLanguage;
- string uAgent;
- string referer;
- string message;
- string post;
- }
- GP fGP(string[][] kelime){
- GP gp;
- gp.url = kelime[0][1];
- gp.protocol = kelime[0][2];
- for(size_t i = 1; i < (kelime.length-1); i++){
- switch(kelime[i][0]){
- case "Host":
- gp.host = kelime[i][1];
- break;
- case "Connection":
- gp.connection = kelime[i][1];
- break;
- case "Cache-Control":
- gp.cacheControl = kelime[i][1];
- break;
- case "User-Agent":
- gp.uAgent = kelime[i][1];
- break;
- case "Referer":
- gp.referer = kelime[i][1];
- break;
- case "Accept-Encoding":
- gp.aEncoding = kelime[i][1];
- break;
- case "Accept-Language":
- gp.aLanguage = kelime[i][1];
- break;
- default:
- break;
- }
- }
- string tarih, sonYenileme;
- auto st = Clock.currTime();
- auto dt = cast(DateTime)st;
- tarih = format(
- "%s, %s %s %s %s:%s:%s GMT",
- fDay(dt.dayOfWeek), dt.day, fMonth(dt.month),
- dt.year, dt.hour, dt.minute, dt.second
- );
- sonYenileme = "Mon, 06 Jan 2014 23:59:59 GMT";
- File header, footer;
- header = File(vYol ~ "/header.html", "r");
- footer = File(vYol ~ "/footer.html", "r");
- string sH, sF;
- while(!header.eof)
- sH ~= header.readln;
- while(!footer.eof)
- sF ~= footer.readln;
- if(gp.url == "/") gp.url = vDosya;
- gp.url = vYol ~ gp.url;
- if((gp.url == vYol ~ "/iletisim") || (gp.url == vYol ~ "/iletisim/"))
- gp.url = vYol ~ "/iletisim.html";
- else if((gp.url == vYol ~ "/hakkimda") || (gp.url == vYol ~ "/hakkimda/"))
- gp.url = vYol ~ "/hakkimda.html";
- string tip;
- tip = split(gp.url, ".")[$-1];
- string cTip;
- if(exists(gp.url)){
- switch(tip){
- case ".html":
- cTip = "text/html; charset=UTF-8";
- break;
- case ".jpg":
- cTip = "image/jpeg";
- break;
- case ".gif":
- cTip = "image/gif";
- break;
- case ".png":
- cTip = "image/png";
- break;
- case ".css":
- cTip = "text/css";
- break;
- case ".js":
- cTip = "application/x-javascript";
- break;
- default:
- break;
- }
- string sD;
- File dosya = File(gp.url, "r");
- while(!dosya.eof)
- sD ~= dosya.readln;
- if(tip == "html"){
- sD = sH ~ sD ~ sF;
- }
- gp.message = format(
- "HTTP/1.1 200 OK\n"
- "Date: %s\n"
- "Last-Modified: %s\n"
- "Server: OzanSelte Dhttp\n"
- "Content-Type: %s\n\n%s",
- tarih, sonYenileme, cTip, sD
- );
- }
- else{
- string mD;
- mD = sH ~ "<h1>404 Not Found</h1>" ~ sF;
- gp.message = format(
- "HTTP/1.1 404 Not Found\n"
- "Date: %s\n"
- "Last-Modified: %s\n"
- "Server: OzanSelte Dhttp\n"
- "Content-Type: text/html; charset=UTF-8\n\n%s",
- tarih, sonYenileme, mD
- );
- }
- return(gp);
- }
- string fDay(int day){
- string[7] days =
- ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
- return days[day];
- }
- string fMonth(int month){
- string[12] months =
- ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
- return months[month-1];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement