Code Monkey home page Code Monkey logo

cjango-unchained's People

Contributors

caprice-j avatar mengdilin avatar official71 avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

official71

cjango-unchained's Issues

HttpResponse Class

HttpResponse Design

member variables

  • string content: a raw string representing the body of the http response
  • int status_code: the status of a http response
  • string reason_phrase: the reason for the current status
    • ex: "200 OK"
  • static const code_to_reason map: populated with status code and reason_phrase pairs once
  • string content-type: type of the content

member functions

  • HttpResponse(String) constructor where string is the body of the http response
  • HttpResponse(String body, String content_type) constructor
    • HttpResponse response = HttpResponse("Text only, please.", "text/plain");
  • write(String) allows HttpResponse to be populated incrementally.
    • Example:
      HttpResponse response = HttpResponse();
      response.write("<p>Here's the text of the Web page.</p>");
      response.write("<p>Here's another paragraph.</p>");
      

Second Phase

  • HttpResponse cookie support
  • HttpResponse session support
  • support adding and removing of header fields

reference:

Router class

Router Class

member variables

  • pattern_to_callback: a map that given a url_path, returns a callback function corresponding to the url_pattern.
  • patterns_list: a vector of patterns, in the exact order of how the user registers the patterns.
    Example:
           Router.add("/abc", some call back);
           Router.add("/efg", some call back);
           Router.add("/xyz", some call back);
           //patterns_list should have the patterns in the order: ["/abc", "/efg", "/xyz"]
    

member functions

  • Router(map<string, functor> routes): constructor that calls add_route to populate pattern_to_callback and patterns_list.

  • add_route(string url_pattern, functor callback): callback must have the signature HttpResponse f(HttpRequest). Given a url_pattern and a callback functor, we store this pair in pattern_to_callback member variable and update patterns_list. We throw an error if we are trying to register the same url_pattern with different callback functors. Note, url_pattern may be a regex pattern or an actual url_path.

  • string resolve(HttpRequest): given a http request, it returns the url_pattern corresponding to the http request. Example: a request for "/abc/e" will return "/abc/e". resolve becomes more complicated if we allow regex resolution.

    • example: parameter: HttpRequest for url path "articles/14/12/"
      patterns_list/pattern_to_callback have a url_pattern "articles/([0-9]{4})/([0-9]{2})/$"
      return: the url_pattern
      Note that if there are 2 potential matching patterns for a given HttpRequest, we return the first matching pattern found in patterns_list.
  • HttpResponse get_http_response(HttpRequest): given a http request, it returns the corresponding http response.

pseudo code:
HttpResponse get_http_response(HttpRequest request) {
    string url_path = resolve(request);
    functor callback = pattern_to_callback.find(url_path);
    return callback(request):
}

Note, have to handle casese if resolve cannot find any url_path, this would be a "404 Not Found" response. We perhaps will have helper functions for constructing these error responses.

App Class

App Class

member variables

  • static router: a global router instance

member functions

  • run(int port): runs forever on a given port.
    1. we set up a listening socket that polls for client connections.
    2. if there is a client connection, spawn a new thread that runs handle_request(client socket)
      We can look into boost:asio for doing this
  • handle_request(client socket): given a client socket, it reads the raw http request string from the socket, creates a HttpRequest object given the raw string, gets callback function by calling resolve(HttpRequest) on the newly created http request object, and sends back a http response corresponding to the request back to the client socket.
  • App(map<string, functor> routes): app constructor that takes a map of url path to functor.
    • in the constructor, we initialize our static router with this map.

HttpRequest Class

HTTP REQUEST DESIGN

member variables:

  • scheme: A string representing the scheme of the request (http/version or https/version). For simplicity sake, we only support http/1.0; thus, scheme is always set to http/1.0.
  • body: The raw HTTP request body as a string.
  • path: A string representing the full path to the requested page, not including "http" or domain.
    Example: "/music/bands/the_beatles/"
  • method: either "GET", "POST", "HEAD"
  • parameters: a map storing all HTTP GET parameters or all HTTP POST parameters. Since we are supporting passing parameters from urls such as: /hello?var1=abc via a get request, a GET request also needs to support parameters.
  • meta (optional): a map storing values of all available HTTP headers:
    • CONTENT_TYPE: The MIME type of the request body.
    • CONTENT_LENGTH: The length of the request body (as a string).
    • REMOTE_ADDR: The IP address of client
    • REMOTE_HOST: The hostname of the client (I think this is optional if we have the IP)
    • SERVER_NAME: The hostname of the server
    • SERVER_PORT: The port of the server (as a string)
  • resolver_match: a resolved url. This attribute is only set after URL resolving took place.

member functions

  • get_host(): returns the host of the request resolved from our socket binding.
    • ex: "127.0.0.1:8000"
  • get_port() returns the port of the request resolved from our socket binding
    • ex: 8000
  • provide const get methods for the member variables. (Note all the maps need to be immutable to everything outside of this class)
  • HttpRequest(String) constructor
  • private helper functions used to parse the raw http request string and construct the HttpRequest object.

Second Phase:

  • cookies: we should support cookies
    The server sends the following in its response header to set a cookie field.
    Set-Cookie:name=value
    
    If there is a cookie set, then the browser sends the following in its request header.
    Cookie:name=value
    
  • We should provide http session's support.

reference:

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.