rand

package
v0.0.16 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 26, 2024 License: MIT Imports: 10 Imported by: 0

README

Rand

Go Reference

The rand module is a custom HTTP handler for Caddy, designed to generate and serve random data in response to HTTP requests. This module can be particularly useful for testing, simulations, or any scenario where random data generation is required.

Features

  • Generates random data based on the provided seed.
  • Allows specifying the size of the random data to be generated.
  • Integrates seamlessly with Caddy's modular design.
Installation

To install rand, you will need to build a custom Caddy binary that includes this module. This can be achieved using the xcaddy utility:

  1. Install xcaddy:

    go install github.com/caddyserver/xcaddy/cmd/xcaddy@latest
    
  2. Build Caddy with the random data generator:

    xcaddy build --with github.com/point-c/caddy-randhandler
    
  3. Run Your Custom Caddy Build:

    ./caddy run
    

Configuration

Caddyfile

You can configure the rand module in your Caddyfile using the rand directive. Here's an example configuration:

:80 {
    route {
        rand
    }
}

This configuration sets up a Caddy server listening on port 80, where the rand module handles all incoming requests.

JSON Configuration

Alternatively, you can configure the module using Caddy's JSON configuration. Here's an example:

{
   "apps": {
      "http": {
         "servers": {
            "srv0": {
               "listen": [":80"],
               "routes": [
                  {
                     "handle": [
                        {
                           "handler": "subroute",
                           "routes": [{"handle": [{"handler": "rand"}]}]
                        }
                     ]
                  }
               ]
            }
         }
      }
   }
}

This JSON configuration achieves the same setup as the Caddyfile example.

Usage

Once configured, the module will respond to HTTP requests with random data. You can control the seed and size of the random data using HTTP headers:

  • Rand-Seed: The seed for the random data generator. If not specified, the current Unix microsecond timestamp is used.
  • Rand-Size: The size of the random data in bytes. If not specified or set to a negative value, the module will stream random data indefinitely.
Example Request

Here's how you might use curl to make a request to a server using the rand module:

curl -H "Rand-Seed: 12345" -H "Rand-Size: 1024" http://localhost:80

This request will return 1024 bytes of random data generated using the seed 12345.

Testing

The package includes tests that demonstrate its functionality. Use Go's testing tools to run the tests:

go test

Godocs

To regenerate godocs:

go generate -tags docs ./...

Documentation

Index

Constants

This section is empty.

Variables

View Source
var NewRand = func(seed int64) io.Reader { return rand.New(rand.NewSource(seed)) }

NewRand function creates a new random data generator.

Functions

This section is empty.

Types

type HeaderKey

type HeaderKey interface {
	Key() string
	Default() int64
}

type HeaderSeed

type HeaderSeed struct{}

func (HeaderSeed) Default

func (HeaderSeed) Default() int64

Default returns the current unix micro time.

func (HeaderSeed) Key

func (HeaderSeed) Key() string

Key returns `Rand-Seed`.

type HeaderSize

type HeaderSize struct{}

func (HeaderSize) Default

func (HeaderSize) Default() int64

Default returns -1.

func (HeaderSize) Key

func (HeaderSize) Key() string

Key return `Rand-Size`.

type HeaderValue

type HeaderValue[K HeaderKey] int64

func (*HeaderValue[K]) GetValue

func (h *HeaderValue[K]) GetValue(headers http.Header)

GetValue extracts a value from the HTTP headers.

type HeaderValues

type HeaderValues struct {
	// contains filtered or unexported fields
}

HeaderValues struct holds the seed and size values extracted from headers.

func NewHeaderValues

func NewHeaderValues(headers http.Header) *HeaderValues

NewHeaderValues creates a new HeaderValues instance from HTTP headers.

func (*HeaderValues) Seed

func (h *HeaderValues) Seed() int64

Seed returns the seed value.

func (*HeaderValues) Size

func (h *HeaderValues) Size() int64

Size returns the size value

type Rand

type Rand struct{}

Rand struct represents the custom Caddy HTTP handler for generating random data.

func (*Rand) CaddyModule

func (r *Rand) CaddyModule() caddy.ModuleInfo

CaddyModule implemnts caddy.Module.

func (*Rand) ServeHTTP

func (r *Rand) ServeHTTP(resp http.ResponseWriter, req *http.Request, _ caddyhttp.Handler) (err error)

ServeHTTP handles sending random data to the client. The following headers can be optionally set to modify the generation of data: - `Rand-Seed`: int64 seed value for the random data generator. - `Rand-Size`: int64 size in bytes of data to generate. A value less than zero represents an infinite stream.

func (*Rand) UnmarshalCaddyfile

func (r *Rand) UnmarshalCaddyfile(*caddyfile.Dispenser) error

UnmarshalCaddyfile implements caddyfile.Unmarshaler.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL