Advertisement
ozanselte

DhttpD

Jan 6th, 2014
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
D 4.17 KB | None | 0 0
  1. import  std.stdio,  std.socket,
  2.         std.array,  std.datetime,
  3.         std.string, std.file;
  4.  
  5. ushort  port    =   80;
  6. string  vYol    =   "./Dhttp";
  7. string  vDosya  =   "/index.html";
  8.  
  9. void main(){
  10.     Socket dinleyici = new TcpSocket;
  11.     dinleyici.bind(new InternetAddress(port));
  12.     dinleyici.listen(1);
  13.    
  14.     while(1){
  15.         Socket soket = dinleyici.accept();
  16.         char[2048] alınacak;
  17.         string alınan;
  18.         soket.receive(alınacak);
  19.         bool hata = true;
  20.         foreach(ch; alınacak){
  21.             if(ch != '\xff'){
  22.                 alınan ~= ch;
  23.                 hata = false;
  24.             }
  25.         }
  26.  
  27.         if(hata) continue;
  28.        
  29.         string[] satır = split(alınan, "\n");
  30.         string[][] kelime;
  31.        
  32.         kelime.length = satır.length;
  33.        
  34.         kelime[0].length = split(satır[0], " ").length;
  35.         kelime[0] = split(satır[0], " ");
  36.        
  37.         for(size_t a = 1; a < kelime.length; a++){
  38.             kelime[a] = split(satır[a], ": ");
  39.             for(size_t b = 0; b < kelime[a].length; b++)
  40.                 kelime[a][b] = chomp(kelime[a][b].idup).dup;
  41.         }
  42.         GP gp;
  43.         gp = fGP(kelime);
  44.         writeln("IP: ", soket.remoteAddress(),
  45.                 "\n\tURL: ", gp.url,
  46.                 "\n--------------------\n");
  47.         soket.send(gp.message);
  48.         soket.close();
  49.     }
  50. }
  51.  
  52. struct GP{
  53.     string url;
  54.     string protocol;
  55.     string host;
  56.     string connection;
  57.     string cacheControl;
  58.     string aEncoding;
  59.     string aLanguage;
  60.     string uAgent;
  61.     string referer;
  62.     string message;
  63.     string post;
  64. }
  65.  
  66. GP fGP(string[][] kelime){
  67.     GP gp;
  68.     gp.url = kelime[0][1];
  69.     gp.protocol = kelime[0][2];
  70.     for(size_t i = 1; i < (kelime.length-1); i++){
  71.         switch(kelime[i][0]){
  72.             case "Host":
  73.                 gp.host = kelime[i][1];
  74.                 break;
  75.             case "Connection":
  76.                 gp.connection = kelime[i][1];
  77.                 break;
  78.             case "Cache-Control":
  79.                 gp.cacheControl = kelime[i][1];
  80.                 break;
  81.             case "User-Agent":
  82.                 gp.uAgent = kelime[i][1];
  83.                 break;
  84.             case "Referer":
  85.                 gp.referer = kelime[i][1];
  86.                 break;
  87.             case "Accept-Encoding":
  88.                 gp.aEncoding = kelime[i][1];
  89.                 break;
  90.             case "Accept-Language":
  91.                 gp.aLanguage = kelime[i][1];
  92.                 break;
  93.             default:
  94.                 break;
  95.         }
  96.     }
  97.    
  98.     string tarih, sonYenileme;
  99.     auto st = Clock.currTime();
  100.     auto dt = cast(DateTime)st;
  101.     tarih = format(
  102.             "%s, %s %s %s %s:%s:%s GMT",
  103.             fDay(dt.dayOfWeek), dt.day, fMonth(dt.month),
  104.             dt.year, dt.hour, dt.minute, dt.second
  105.             );
  106.     sonYenileme = "Mon, 06 Jan 2014 23:59:59 GMT";
  107.    
  108.     File header, footer;
  109.     header = File(vYol ~ "/header.html", "r");
  110.     footer = File(vYol ~ "/footer.html", "r");
  111.     string sH, sF;
  112.     while(!header.eof)
  113.         sH ~= header.readln;
  114.     while(!footer.eof)
  115.         sF ~= footer.readln;
  116.    
  117.     if(gp.url ==  "/") gp.url = vDosya;
  118.     gp.url = vYol ~ gp.url;
  119.    
  120.     if((gp.url == vYol ~ "/iletisim") || (gp.url == vYol ~ "/iletisim/"))
  121.         gp.url = vYol ~ "/iletisim.html";
  122.     else if((gp.url == vYol ~ "/hakkimda") || (gp.url == vYol ~ "/hakkimda/"))
  123.         gp.url = vYol ~ "/hakkimda.html";
  124.    
  125.     string tip;
  126.     tip = split(gp.url, ".")[$-1];
  127.     string cTip;
  128.    
  129.     if(exists(gp.url)){
  130.         switch(tip){
  131.             case ".html":
  132.                 cTip = "text/html; charset=UTF-8";
  133.                 break;
  134.             case ".jpg":
  135.                 cTip = "image/jpeg";
  136.                 break;
  137.             case ".gif":
  138.                 cTip = "image/gif";
  139.                 break;
  140.             case ".png":
  141.                 cTip = "image/png";
  142.                 break;
  143.             case ".css":
  144.                 cTip = "text/css";
  145.                 break;
  146.             case ".js":
  147.                 cTip = "application/x-javascript";
  148.                 break;
  149.             default:
  150.                 break;
  151.         }
  152.         string sD;
  153.         File dosya = File(gp.url, "r");
  154.         while(!dosya.eof)
  155.             sD ~= dosya.readln;
  156.        
  157.         if(tip == "html"){
  158.             sD = sH ~ sD ~ sF;
  159.         }
  160.        
  161.         gp.message = format(
  162.             "HTTP/1.1 200 OK\n"
  163.             "Date: %s\n"
  164.             "Last-Modified: %s\n"
  165.             "Server: OzanSelte Dhttp\n"
  166.             "Content-Type: %s\n\n%s",
  167.             tarih, sonYenileme, cTip, sD
  168.             );
  169.     }
  170.     else{
  171.         string mD;
  172.         mD = sH ~ "<h1>404 Not Found</h1>" ~ sF;
  173.         gp.message = format(
  174.             "HTTP/1.1 404 Not Found\n"
  175.             "Date: %s\n"
  176.             "Last-Modified: %s\n"
  177.             "Server: OzanSelte Dhttp\n"
  178.             "Content-Type: text/html; charset=UTF-8\n\n%s",
  179.             tarih, sonYenileme, mD
  180.             );
  181.     }
  182.     return(gp);
  183. }
  184.  
  185. string fDay(int day){
  186.     string[7] days =
  187.         ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"];
  188.     return days[day];
  189. }
  190.  
  191. string fMonth(int month){
  192.     string[12] months =
  193.         ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
  194.     return months[month-1];
  195. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement