Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
shuLhan committed Feb 3, 2024
1 parent 5f4fd8d commit fa733e5
Show file tree
Hide file tree
Showing 8 changed files with 244 additions and 0 deletions.
1 change: 1 addition & 0 deletions NOTES
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

7 changes: 7 additions & 0 deletions TODO
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
= TODO

[ ] lib/ini: allow unmarshal value into []byte
[ ] lib/ini: remove ';' as comment.
[x] lib/ascii/ascii_example_test.go:115:2: SA1019: rand.Seed has been deprecated
[x] lib/numbers/int.go:36:2: SA1019: rand.Seed has been deprecated
[x] lib/mining/knn/neighbor_test.go:58:2: SA1019: rand.Seed has been deprecated
44 changes: 44 additions & 0 deletions _doc/RFC_2183__CONTENT-DISPOSITION.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
= The Content-Disposition Header Field
:toc:
:sectlinks:
:sectnums:
:url-rfc2183: https://tools.ietf.org/html/rfc2183

This document provide note and summary of
{url-rfc2183}[RFC 2183^], Communicating Presentation Information in Internet
Messages: The Content-Disposition Header Field.


== Syntax

----
disposition := "Content-Disposition" ":"
disposition-type
*(";" disposition-parm)
disposition-type := "inline"
/ "attachment"
/ extension-token
; values are not case-sensitive
disposition-parm := filename-parm
/ creation-date-parm
/ modification-date-parm
/ read-date-parm
/ size-parm
/ parameter
filename-parm := "filename" "=" value
creation-date-parm := "creation-date" "=" quoted-date-time
modification-date-parm := "modification-date" "=" quoted-date-time
read-date-parm := "read-date" "=" quoted-date-time
size-parm := "size" "=" 1*DIGIT
quoted-date-time := quoted-string
; contents MUST be an RFC 822 `date-time'
; numeric timezones (+HHMM or -HHMM) MUST be used
----
1 change: 1 addition & 0 deletions _doc/lib/websocket/AUTOBAHN.adoc
90 changes: 90 additions & 0 deletions cmd/hexo/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// Program hexcho print the input hex as hex itself, int64, octal, bytes,
// string, and binary.
package main

import (
"flag"
"fmt"
"log"
"os"
"strconv"

libbytes "github.com/shuLhan/share/lib/bytes"
)

func main() {
var (
optFile bool
)

flag.BoolVar(&optFile, `file`, false, `make each arguments as files`)
flag.Parse()

var (
arg string
hex int64
err error
b byte
)

if optFile {
dumpFiles()
return
}

for _, arg = range flag.Args() {
fmt.Printf("[0x%s]\n", arg)

hex, err = strconv.ParseInt(arg, 16, 64)
if err != nil {
log.Println(err)
continue
}

var bytes = make([]byte, 0, 8)
for x := 56; x >= 0; x -= 8 {
bytes = append(bytes, byte(hex>>x))
}

fmt.Printf(" int64: %d\n", hex)

fmt.Printf(" hex:")
for _, b = range bytes {
fmt.Printf(" %8x", b)
}
fmt.Println()

fmt.Printf(" bytes:")
for _, b = range bytes {
fmt.Printf(" %8d", b)
}
fmt.Println()

fmt.Printf(" char:")
for _, b = range bytes {
fmt.Printf(" %8c", b)
}
fmt.Println()
fmt.Printf(" binary:")
for _, b = range bytes {
fmt.Printf(" %08b", b)
}
fmt.Println()
}
}

func dumpFiles() {
var (
arg string
content []byte
err error
)
for _, arg = range flag.Args() {
content, err = os.ReadFile(arg)
if err != nil {
log.Fatalf(`hexo: file %s: %s`, arg, err)
}

libbytes.DumpPrettyTable(os.Stdout, arg, content)
}
}
31 changes: 31 additions & 0 deletions lib/http/internal/cmd/httpd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Program httpd run HTTP server that serve files in directory testdata for
// testing with external tools.
// This program should be run from directory lib/http.
package main

import (
"fmt"
"log"

libhttp "github.com/shuLhan/share/lib/http"
"github.com/shuLhan/share/lib/http/internal"
)

func main() {
var (
srv *libhttp.Server
err error
)

srv, err = internal.NewTestServer()
if err != nil {
log.Fatal(err)
}

fmt.Printf("Starting test server at http://%s\n", srv.Options.Address)

err = srv.Start()
if err != nil {
log.Println(err)
}
}
64 changes: 64 additions & 0 deletions lib/http/internal/internal.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
// Package internal contains helpers for testing http.
package internal

import (
"fmt"
"net/http"
"strings"

libhttp "github.com/shuLhan/share/lib/http"
"github.com/shuLhan/share/lib/memfs"
)

// NewTestServer create new HTTP server for testing.
func NewTestServer() (srv *libhttp.Server, err error) {
var (
logp = `NewTestServer`
opts = &libhttp.ServerOptions{
Memfs: &memfs.MemFS{
Opts: &memfs.Options{
Root: `./testdata`,
MaxFileSize: 30,
TryDirect: true,
},
},
HandleFS: handleFS,
Address: `127.0.0.1:14832`,
}
)

srv, err = libhttp.NewServer(opts)
if err != nil {
return nil, fmt.Errorf(`%s: %w`, logp, err)
}

return srv, nil
}

// handleFS authenticate the request to Memfs using cookie.
//
// If the node does not start with "/auth/" it will return true.
//
// If the node path is start with "/auth/" and cookie name "sid" exist
// with value "authz" it will return true;
// otherwise it will redirect to "/" and return false.
func handleFS(node *memfs.Node, res http.ResponseWriter, req *http.Request) bool {
var (
lowerPath = strings.ToLower(node.Path)

cookieSid *http.Cookie
err error
)
if strings.HasPrefix(lowerPath, "/auth/") {
cookieSid, err = req.Cookie("sid")
if err != nil {
http.Redirect(res, req, "/", http.StatusSeeOther)
return false
}
if cookieSid.Value != "authz" {
http.Redirect(res, req, "/", http.StatusSeeOther)
return false
}
}
return true
}
6 changes: 6 additions & 0 deletions lib/http/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,9 @@ func (srv *Server) registerPut(ep *Endpoint) (err error) {

// ServeHTTP handle mapping of client request to registered endpoints.
func (srv *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
//log.Println(`REQUEST:`)
//req.Write(log.Writer())

switch req.Method {
case http.MethodDelete:
srv.handleDelete(res, req)
Expand Down Expand Up @@ -301,6 +304,9 @@ func (srv *Server) ServeHTTP(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(http.StatusNotImplemented)
return
}
//log.Println("RESPONSE:")
//res.Header().Write(log.Writer())
//log.Println()
}

// Start the HTTP server.
Expand Down

0 comments on commit fa733e5

Please sign in to comment.