2011-10-04 7 views
7

डी का उपयोग करके, मैं आने वाले HTTP ट्रैफ़िक को कैसे सुनूंगा और इसका जवाब दूंगा?डी का उपयोग करके, मैं इनकमिंग HTTP अनुरोधों को कैसे सुनूंगा और उन्हें जवाब दूंगा?

उदाहरण (स्यूडोकोड में) के लिए:

socket = new socket("locahost", 80) 
socket.onRequestRecevied(handleRequest); 

function response handleRequest(request) { 
    //do something with the request and respond 
    request.respond("hello world") 
} 

मैं जानता हूँ कि वहाँ कि तुलना में यह करने के लिए एक बहुत अधिक है, लेकिन मैं भेजे http अनुरोध का जवाब देने पर कई संसाधनों को खोजने के लिए नहीं कर पाए हैं।

संपादित करें: मेरे वर्तमान प्रयासों ने केवल "अपवाद बनाने में असमर्थ: ऑपरेशन की अनुमति नहीं है" जैसे अपवाद पैदा किए हैं। जिसका अर्थ यह हो सकता है कि मैं इसे सही तरीके से कर रहा हूं लेकिन बस एक सिस्टम त्रुटि संदेश प्राप्त कर रहा हूं।

+0

क्या आपके पास पोर्ट 80, या किसी अन्य निम्न-स्तरीय बंदरगाह पर सुनने की अनुमति है? पोर्ट 8080 या प्रतिबंधित सीमा में कुछ और नहीं के साथ प्रयास करें। – gmfawcett

+0

मैं पोर्ट 3000 के साथ कोशिश कर रहा था। मैंने 8080 और कुछ अन्य लोगों के साथ भी कोशिश की है। –

उत्तर

5

यह सामान्य भेजे TCP कनेक्शन को सुनने और उन्हें जवाब के रूप में ही:

import std.socket; 
    Socket s = new TcpSocket(AddressFamily.INET); 
    s.bind(new InternetAddress("0.0.0.0", 80)); 
    s.listen(8); 
    while (true) { 
    Socket conn=s.accept(); 
    //handle incoming message (I'm putting it in a task pool to handle ;)) 
    taskPool.put(task!handleHttp(conn)); 
    } 

handleHttp(Socket) http अनुरोध प्राप्त करने और के रूप में परिभाषित http मानक होना प्रतिक्रिया भेजने के लिए तर्क युक्त (आप करेंगे हालांकि अपने स्वयं को ढूंढना होगा)

+0

ऐसा लगता है कि अब काम कर रहा है। धन्यवाद। –

5

वर्तमान में मानक लाइब्रेरी में कोई HTTP सर्वर नहीं है। वेब काम के लिए एडम रूपपे है, लेकिन वर्तमान में इसमें एक स्टैंडअलोन वेब सर्वर शामिल नहीं है।

शैक्षिक उद्देश्यों के लिए नीचे दिया गया कार्यक्रम एक नंगे-हड्डियों, सिंगल-थ्रेडेड मूल HTTP सर्वर है। एक वैध HTTP प्रतिक्रिया उत्पन्न करना अभी भी आपके ऊपर है; लेकिन कम से कम यह शीर्षलेख को पार करता है, और आपको अनुरोध के विवरण के आधार पर प्रतिक्रिया देने का मौका देता है।

import std.algorithm; 
import std.conv; 
import std.stdio; 
import std.socket; 
import std.string; 

// usage: ./server port 

void main(string[] args) 
{ 
    enum BACKLOG = 8; 
    ushort PORT = to!ushort(args[1]); 
    Socket s = new TcpSocket(AddressFamily.INET); 
    s.bind(new InternetAddress("0.0.0.0", PORT)); 
    s.listen(BACKLOG); 

    scope (exit) 
    { 
    s.shutdown(SocketShutdown.BOTH); 
    s.close(); 
    } 

    while (true) 
    { 
    debug writeln("waiting..."); 
    Socket conn = s.accept(); 
    scope (exit) 
    { 
     conn.shutdown(SocketShutdown.BOTH); 
     conn.close(); 
    } 
    try 
    { 
     handleHttp(conn); 
    } 
    catch (Throwable e) 
    { 
     stderr.writeln("thrown: ", e); 
    } 
    }  
} 

void handleHttp(Socket conn) 
{ 
    // Make a buffer big enough to read a full HTTP header. My approach here is to 
    // read the header in its entirety before proceeding. This isn't production 
    // quality, but good enough for some applications. 

    ubyte[8192] buf; // big enough for some purposes... 
    size_t position, headerEnd, len, newpos; 

    // Receive the whole header before parsing it. 
    while (true) 
    { 
    len = conn.receive(buf[position..$]); 

    if (len == 0)    // empty request 
     return; 

    newpos = position + len; 
    headerEnd = countUntil(buf[position..newpos], "\r\n\r\n"); 
    position = newpos; 

    if (headerEnd >= 0) 
     break; 
    } 

    // Anything beyond headerEnd+4 is part of the request body. For now, bail: 
    // no POSTs or PUTs allowed. Feel free to remove the assert & implement them! 
    assert (position-(headerEnd+4) == 0, 
      "Sorry, only content-free requests are supported."); 

    // Now parse the header. 
    auto lines   = splitter(buf[0..headerEnd], "\r\n"); 
    string request_line = cast(string) lines.front; 
    lines.popFront; 

    debug writeln(request_line); 

    // a very simple Header structure. 
    struct Pair 
    { 
    string key, value; 

    this(ubyte[] line) 
    { 
     auto tmp = countUntil(line, ": "); 
     key  = cast(string) line[0..tmp]; // maybe down-case these? 
     value  = cast(string) line[tmp+2..$]; 
    } 
    } 

    Pair[] headers; 
    foreach(line; lines) 
    headers ~= Pair(line); 

    auto tmp  = splitter(request_line, ' '); 
    string method = tmp.front; tmp.popFront; 
    string url  = tmp.front; tmp.popFront; 
    string protocol = tmp.front; tmp.popFront; 

    debug writefln("%s, %s, %s", method, url, protocol); 

    // Prepare a response, and send it 

    string resp = join(["HTTP/1.1 200 OK", 
         "Content-Length: 2", 
         "Content-Type: text/plain", 
         "Connection: close", 
         "", 
         "OK"], 
        "\r\n"); 

    conn.send(cast(ubyte[]) resp); 
} 
+0

धन्यवाद! क्या आप नहीं जानते कि इसे बहु-थ्रेड कैसे बनाया जाए? –

1

एक मल्टी-थ्रेडेड (घटना पर आधारित) वेब सर्वर जो देशी डी लिपियों का समर्थन करता है G-Wan कहा जाता है नहीं है।

मैंने कभी भी 'डी' स्क्रिप्ट के साथ इसका उपयोग नहीं किया, केवल सी ++ स्क्रिप्ट के साथ जिसके लिए यह अपेक्षित काम करता था।

संबंधित मुद्दे