Coder Social home page Coder Social logo

gmod-httpserver's Introduction

This Project aims to add a HTTPServer to Gmod.

This project uses the cpp-httplib as the HTTPServer.

TODO:

  • Move IP Whitelist check to a Pre-routing handler
  • Add a Error handler and function
  • Add a Exception handler and function
  • Add a Pre-routing handler and function
  • Add a Post-routing handler and function

Functions

Basic Functions

httpserver.Start(String IP, Number Port)

This will start or restart the HTTP Server, and it will listen on the given address + port.
NOTE: If a Method function was called like httpserver.Get after httpserver.Start was called, you need to call httpserver.Start again!

httpserver.Stop()

This stops the HTTP Server.

(internal function!) httpserver.Think()

This is internally used to manage all requests and to call all functions needed. (Never call this yourself.)

Method Functions

All Method functions add a listener for the given path and the given method, like this:

httpserver.Get("/public", function(_, responseprint("Public GET request"response.SetContent("You sent a GET request to a public site.", "text/plain")
end, false)

httpserver.Get("/private", function(_, responseprint("Private GET request"response.SetContent("You sent a GET request to a private site.", "text/plain")
end, true)

If you enable the IP Whitelist, only requests sent by connected players are processed.

httpserver.Get(String path, function (Request, Response), bool ipwhitelist)

httpserver.Put(String path, function (Request, Response), bool ipwhitelist)

httpserver.Post(String path, function (Request, Response), bool ipwhitelist)

httpserver.Patch(String path, function (Request, Response), bool ipwhitelist)

httpserver.Delete(String path, function (Request, Response), bool ipwhitelist)

httpserver.Options(String path, function (Request, Response), bool ipwhitelist)

Additional Functions

httpserver.IsRunning()

Returns true if the HTTPServer is running.

httpserver.SetTCPnodelay(bool nodelay)

Sets whether a delay should be added to tcp or not.

httpserver.SetReadTimeout(int sec, int usec)

Sets the maximum amount of time before a read times out.

httpserver.SetWriteTimeout(int sec, int usec)

Sets the maximum amount of time before a write times out.

httpserver.SetPayloadMaxLength(int maxlength)

Sets the maximum payload length.

httpserver.SetKeepAliveTimeout(int sec)

Sets the maximum time a connection is kept alive.

httpserver.SetKeepAliveMaxCount(int amount)

Sets the maximum amount of connections that can be kept alive at the same time.

httpserver.SetMountPoint(string path, string folder)

This mounts the given folder to the given path. (You can call this multiple times for the same path to mount multiple folders to it.)

httpserver.RemoveMountPoint(string path)

This removes all mounts for the given path.

Request

Request.body

The body of the HTTP Request.

Request.remote_addr

the IP Address of the Person who sent the HTTP Request

Request.remote_port

The Port the HTTP request was received from.

Request.local_addr

Request.local_port

Request.method

The HTTP Method that was used like GET or PUT.

Request.authorization_count

Request.content_length

The length of the HTTP Request content.

Request.HasHeader(key)

returns true if the client has the given key in the header.

Request.HasParam(key)

returns true if the client has the given key in the parameters.

Request.GetHeader(key)

returns the value of the given key from the header.

Request.GetParam(key)

returns the value of the given key from the parameters.

Response

Response.SetContent(content, content-type)

Sets the content like this:

Response.SetContent("Hello World", "text/plain")

Response.SetRedirect(url, code)

Redirects one to the given URL and returns the given code.

Response.SetHeader(key, value)

Sets the given value for the given key in the header.

gmod-httpserver's People

Contributors

raphaelit7 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

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.