I am making a captcha and am following the example given here. I need to modify the example to use gorilla mux's routing as the rest of my app uses that. For the life of me I can't figure out how to correctly route the path for line 47 of that example. What I have below results in no captcha generated...(the example itself works fine). For shits & giggles I've even tried "http.HandleFunc("/captcha/", captchaHandler)" but that doesn't work either. Any suggestions?
package main
import (
"github.com/dchest/captcha"
"github.com/gorilla/mux"
"io"
"log"
"net/http"
"text/template"
)
var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))
func showFormHandler(w http.ResponseWriter, r *http.Request) {
if r.URL.Path != "/" {
http.NotFound(w, r)
return
}
d := struct {
CaptchaId string
}{
captcha.New(),
}
if err := formTemplate.Execute(w, &d); err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
}
}
func processFormHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html; charset=utf-8")
if !captcha.VerifyString(r.FormValue("captchaId"), r.FormValue("captchaSolution")) {
io.WriteString(w, "Wrong captcha solution! No robots allowed!\n")
} else {
io.WriteString(w, "Great job, human! You solved the captcha.\n")
}
io.WriteString(w, "<br><a href='/'>Try another one</a>")
}
func captchaHandler(w http.ResponseWriter, r *http.Request) {
captcha.Server(captcha.StdWidth, captcha.StdHeight)
}
type Routes []Route
type Route struct {
Method string
Pattern string
HandlerFunc http.HandlerFunc
}
func main() {
/*
http.HandleFunc("/", showFormHandler)
http.HandleFunc("/process", processFormHandler)
//http.HandleFunc("/captcha/", captchaHandler) // doesn't work
http.Handle("/captcha/", captcha.Server(captcha.StdWidth, captcha.StdHeight))
fmt.Println("Server is at localhost:8666")
if err := http.ListenAndServe(":8666", nil); err != nil {
log.Fatal(err)
}
*/
var routes = Routes{
Route{"GET", "/", showFormHandler},
Route{"POST", "/process", processFormHandler},
Route{"GET", "/captcha/", captchaHandler},
}
router := mux.NewRouter().StrictSlash(true)
for _, route := range routes {
var handler http.Handler
handler = route.HandlerFunc
router.Methods(route.Method).Path(route.Pattern).Handler(handler)
}
//router.Methods("GET").Path("/captcha/").HandlerFunc(captcha.Server(captcha.StdWidth, captcha.StdHeight))
port := ":8666"
log.Println("Listening at", port)
log.Fatal(http.ListenAndServe(port, router))
}
const formTemplateSrc = `<!doctype html>
<head><title>Captcha Example</title></head>
<body>
<script>
function setSrcQuery(e, q) {
var src = e.src;
var p = src.indexOf('?');
if (p >= 0) {
src = src.substr(0, p);
}
e.src = src + "?" + q
}
function playAudio() {
var le = document.getElementById("lang");
var lang = le.options[le.selectedIndex].value;
var e = document.getElementById('audio')
setSrcQuery(e, "lang=" + lang)
e.style.display = 'block';
e.autoplay = 'true';
return false;
}
function changeLang() {
var e = document.getElementById('audio')
if (e.style.display == 'block') {
playAudio();
}
}
function reload() {
setSrcQuery(document.getElementById('image'), "reload=" + (new Date()).getTime());
setSrcQuery(document.getElementById('audio'), (new Date()).getTime());
return false;
}
</script>
<select id="lang" onchange="changeLang()">
<option value="en">English</option>
<option value="ru">Russian</option>
<option value="zh">Chinese</option>
</select>
<form action="/process" method=post>
<p>Type the numbers you see in the picture below:</p>
<p><img id=image src="/captcha/{{.CaptchaId}}.png" alt="Captcha image"></p>
Reload | Play Audio
<audio id=audio controls style="display:none" src="/captcha/{{.CaptchaId}}.wav" preload=none>
You browser doesn't support audio.
Download file to play it in the external player.
</audio>
<input type=hidden name=captchaId value="{{.CaptchaId}}"><br>
<input name=captchaSolution>
<input type=submit value=Submit>
</form>
`
EDIT #1:
To be clearer "doesn't work" isn't helpful. It returns a 404 error.
EDIT #2:
The example on github works fine....its only when I modify the route that it returns a 404 when I try to generate a captcha.
You can convert the http.Handler h to a http.HandlerFunc using the method expression:
h.ServeHTTP
Instead of converting to a HandlerFunc, you can register the Handler directly using the route Handler method:
router.Methods("GET").Path("/captcha/").Handler(captcha.Server(captcha.StdWidth, captcha.StdHeight))
Based on your comments and edits, I think you want a prefix match instead of an exact match:
router.Methods("GET").PathPrefix("/captcha/").Handler(captcha.Server(captcha.StdWidth, captcha.StdHeight))
The direct answer to a topic question is simply as is.
func(w http.ResponseWriter, r *http.Request, p map[string]string) {
youhttp.Handler().ServeHTTP(w, r)
}
Related
I have a function which should get the page name and print it, for example, if the URL is http://localhost:8080/login.html the function should print login.html
If you only need to parse the URL you can use below:
package main
import (
"fmt"
"net/url"
)
func main() {
URL := "http://localhost:8080/login.html"
name, err := getPageName(URL)
if err != nil {
panic(err)
}
fmt.Println(name)
}
func getPageName(URL string) (string, error) {
u, err := url.Parse(URL)
if err != nil {
return "", err
}
return u.Path[1:], nil // To remove initial /
}
If you need to get page's HTML and parse the title from <head> you can use go-query
package main
import (
"fmt"
"log"
"net/http"
"github.com/PuerkitoBio/goquery"
)
func main() {
URL := "https://stackoverflow.com"
res, err := http.Get(URL)
if err != nil {
log.Fatal(err)
}
defer res.Body.Close()
if res.StatusCode != 200 {
log.Fatalf("status code error: %d %s", res.StatusCode, res.Status)
}
// Load the HTML document
doc, err := goquery.NewDocumentFromReader(res.Body)
if err != nil {
log.Fatal(err)
}
title := doc.Find("title").Text()
fmt.Println(title)
}
You can do this to get the page name:
func GetPageName(address string) (string, error) {
u, err := url.Parse(address)
if err != nil {
return "", err
}
params := strings.Split(u.Path, "/")
// Index page
if len(params) == 0 || (len(params) == 1 && params[0] == "") {
return "", nil
} else {
pageName := params[len(params)-1]
// Has trailing slash
if pageName == "" {
return params[len(params)-2], nil
}
// Doesn't have trailing slash
return pageName, nil
}
}
If the url address is the index page address, for example host.com or host.com/ it returns an empty string.
Otherwise returns the page name for the given url. For example test for host.com/test and host.com/test/ and host.com/path/test.
I cannot figure out how to make the requests for "access", "clock" and "audit" with the required parameters. Could you give me some guidance on how to make these requests?
package main
import (
"crypto/sha256"
"crypto/subtle"
"encoding/base64"
"encoding/json"
"fmt"
"log"
"net/http"
"os"
"sync"
"time"
)
const secret = "galumphing"
// Required parameters for the request
type params struct {
Act string
Nonce string
Signature string
Timeout int64
Offset int
}
var (
auditLock sync.Mutex
auditBase int
auditLog []string
)
var (
seenLock sync.Mutex
seenMap = map[string]bool{}
)
var (
clockAsk = make(chan bool)
clockTime = make(chan int64, 1)
)
func main() {
// Endpoints available
http.HandleFunc("/903/access", handleAccess)
http.HandleFunc("/903/clock", handleClock)
http.HandleFunc("/903/audit", handleAudit)
err := http.ListenAndServe(os.Args[1], nil)
if err != nil {
log.Fatal(err)
}
}
func checkCapacity(w http.ResponseWriter) (ok bool) {
auditLock.Lock()
defer auditLock.Unlock()
if len(auditLog) > 10 {
w.WriteHeader(http.StatusServiceUnavailable)
return
}
ok = true
return
}
func audit(r *http.Request, params params, ok bool) {
auditLock.Lock()
defer auditLock.Unlock()
auditLog = append(auditLog, fmt.Sprintf("%v %q %q", ok, r.URL.Path, params.Act))
}
func parse(w http.ResponseWriter, r *http.Request) (params params, ok bool) {
defer func() {
if !ok {
audit(r, params, false)
}
}()
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Max-Age", "3600")
if r.Method != "POST" {
w.Header().Set("Allow", "POST, OPTIONS")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
return
}
err := json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("%s: %v", r.URL.Path, err)
return
}
h := sha256.New()
fmt.Fprintf(h, "%s\r\n%s\r\n%s\r\n%s", r.URL.Path, params.Act, params.Nonce, secret)
sig := base64.StdEncoding.EncodeToString(h.Sum(nil))
if subtle.ConstantTimeCompare([]byte(sig), []byte(params.Signature)) != 1 {
w.WriteHeader(http.StatusForbidden)
return
}
seenLock.Lock()
seen := seenMap[params.Signature]
if !seen {
seenMap[params.Signature] = true
}
seenLock.Unlock()
if seen {
w.WriteHeader(http.StatusForbidden)
return
}
ok = true
return
}
func handleAccess(w http.ResponseWriter, r *http.Request) {
if !checkCapacity(w) {
return
}
params, ok := parse(w, r)
if !ok {
return
}
switch params.Act {
case "begin":
if params.Timeout < 0 || params.Timeout > 250000 {
w.WriteHeader(http.StatusBadRequest)
audit(r, params, false)
return
}
timer := time.NewTimer(time.Duration(params.Timeout) * time.Microsecond)
select {
case clockAsk <- true: // https://golang.org/ref/spec#Send_statements
w.WriteHeader(http.StatusNoContent)
audit(r, params, true)
case <-timer.C:
w.WriteHeader(http.StatusConflict)
audit(r, params, false)
return
}
go func() {
<-timer.C
select {
case <-clockTime:
default:
}
}()
case "end":
if params.Timeout < 0 {
w.WriteHeader(http.StatusBadRequest)
audit(r, params, false)
return
}
timer := time.NewTimer(time.Duration(params.Timeout) * time.Microsecond)
select {
case value := <-clockTime: // https://golang.org/ref/spec#Receive_operator
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, value)
audit(r, params, true)
case <-timer.C:
w.WriteHeader(http.StatusConflict)
audit(r, params, false)
}
default:
w.WriteHeader(http.StatusBadRequest)
audit(r, params, false)
}
}
func handleClock(w http.ResponseWriter, r *http.Request) {
if !checkCapacity(w) {
return
}
params, ok := parse(w, r)
if !ok {
return
}
switch params.Act {
case "observe":
if params.Timeout != 0 {
w.WriteHeader(http.StatusBadRequest)
audit(r, params, false)
return
}
select {
case <-clockAsk: // https://golang.org/ref/spec#Receive_operator
select {
case clockTime <- time.Now().Unix(): // https://golang.org/ref/spec#Send_statements
default:
}
default:
}
w.WriteHeader(http.StatusNoContent)
audit(r, params, true)
default:
w.WriteHeader(http.StatusBadRequest)
audit(r, params, false)
}
}
func handleAudit(w http.ResponseWriter, r *http.Request) {
params, ok := parse(w, r)
if !ok {
return
}
ok = false
func() {
auditLock.Lock()
defer auditLock.Unlock()
switch params.Act {
case "":
if params.Offset != 0 {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
fmt.Fprintln(w, auditBase)
case "burble":
if params.Offset < auditBase || params.Offset > auditBase+len(auditLog) {
w.WriteHeader(http.StatusBadRequest)
return
}
w.Header().Set("Content-Type", "text/plain")
w.WriteHeader(http.StatusOK)
for i := params.Offset - auditBase; i < len(auditLog); i++ {
fmt.Fprintf(w, "%d %s\n", auditBase+i, auditLog[i])
}
case "chortle":
if params.Offset > auditBase+len(auditLog) {
w.WriteHeader(http.StatusBadRequest)
return
}
if params.Offset > auditBase {
auditLog = auditLog[params.Offset-auditBase:] // https://golang.org/ref/spec#Slice_expressions
auditBase = params.Offset
}
w.WriteHeader(http.StatusNoContent)
default:
w.WriteHeader(http.StatusBadRequest)
return
}
ok = true
}()
audit(r, params, ok)
}
There are a few ways to go about this. You can either use a tool like Postman, or simply just use curl from the command line to send a post request to your endpoint. Here is the curl command that I used to hit the endpoint (I am using port 8080 for my testing), the bit after the -d flag is the request body:
curl -X POST http://localhost:8080/903/access -d '{"Act": "act", "Nonce": "nonce", "Signature": "signature", "TimeOut": 64, "Offset": 0}'
I did modify the code to cut out most of the logic. This request just shows an example of how the current code can parse the request body. If you are interested more in how some of the other logic works in the handler functions, let me know and I can edit my response. Here is the code that I used for my testing:
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"os"
)
// Required parameters for the request
type params struct {
Act string
Nonce string
Signature string
Timeout int64
Offset int
}
func main() {
// Endpoints available
http.HandleFunc("/903/access", handleAccess)
err := http.ListenAndServe(os.Args[1], nil)
if err != nil {
log.Fatal(err)
}
}
func parse(w http.ResponseWriter, r *http.Request) (params params, ok bool) {
w.Header().Set("Access-Control-Allow-Methods", "POST, OPTIONS")
w.Header().Set("Access-Control-Allow-Origin", "*")
w.Header().Set("Access-Control-Max-Age", "3600")
if r.Method != "POST" {
w.Header().Set("Allow", "POST, OPTIONS")
if r.Method == "OPTIONS" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusMethodNotAllowed)
}
return
}
err := json.NewDecoder(r.Body).Decode(¶ms)
if err != nil {
w.WriteHeader(http.StatusBadRequest)
log.Printf("%s: %v", r.URL.Path, err)
return
}
return
}
func handleAccess(w http.ResponseWriter, r *http.Request) {
params, _ := parse(w, r)
fmt.Printf("%+v\n", params)
}
Here is the output that I received when all is said and done:
{Act:act Nonce:nonce Signature:signature Timeout:64 Offset:0}
I build a proxy,when proxy to 127.0.0.1:9902 fail,then proxy to 127.0.0.1:9903.
But, when proxy to 127.0.0.1:9902 fail,the http request body be closed in Transport.RoundTrip,so proxy to 127.0.0.1:9903 will be error(invalid Read on closed Body).
I have been plagued for a long time.any master can help me and give an idea to proxyTo function by the way? thank you very much!
func proxy(w http.ResponseWriter, r *http.Request) {
ok := proxyTO("http://127.0.0.1:9902", w, r) //server127.0.0.1:9902 not open,so r.Body close after proxyTo
if ok == false {
proxy1("http://127.0.0.1:9903", w, r) //error http: invalid Read on closed Body
}
}
//any idea on proxyTo function?
func proxyTo(addr string, w http.ResponseWriter, r *http.Request) bool {
urlLs, _ := url.Parse(addr)
r.URL.Host = urlLs.Host
r.URL.Scheme = urlLs.Scheme
r.Host = urlLs.Host
r.RequestURI = ""
resp, err := http.DefaultTransport.RoundTrip(r) //how to keep r.Body not close when error
if err != nil {
return false
}
if resp.StatusCode != http.StatusOK {
w.WriteHeader(resp.StatusCode)
return true //here return is ok?
}
for name, vals := range resp.Header {
for _, val := range vals {
w.Header().Add(name, val)
}
}
io.Copy(w, resp.Body)
resp.Body.Close()
return true
}
How do I retrieve the []byte value of this interface?
package main
import (
"reflect"
)
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
//var b []byte
i := byteInterface()
switch {
case reflect.TypeOf(i).Kind() == reflect.Slice && (reflect.TypeOf(i) == reflect.TypeOf([]byte(nil))):
default:
panic("should have bytes")
}
}
You can use a type assertion for this; no need to use the reflect package:
package main
func byteInterface() interface{} {
return []byte("foo")
}
func main() {
i := byteInterface()
if b, ok := i.([]byte); ok {
// use b as []byte
println(len(b))
} else {
panic("should have bytes")
}
}
I have Go program that has a function defined. I also have a map that should have a key for each function. How can I do that?
I have tried this, but this doesn't work.
func a(param string) {
}
m := map[string] func {
'a_func': a,
}
for key, value := range m {
if key == 'a_func' {
value(param)
}
}
Are you trying to do something like this? I've revised the example to use varying types and numbers of function parameters.
package main
import "fmt"
func f(p string) {
fmt.Println("function f parameter:", p)
}
func g(p string, q int) {
fmt.Println("function g parameters:", p, q)
}
func main() {
m := map[string]interface{}{
"f": f,
"g": g,
}
for k, v := range m {
switch k {
case "f":
v.(func(string))("astring")
case "g":
v.(func(string, int))("astring", 42)
}
}
}
m := map[string]func(string, string)
Works if you know the signature (and all the funcs have the same signature)
I think this is cleaner/safer than using interface{}
You can define a type if functions are same interface.
package main
import "log"
type fn func (string)
func foo(msg string) {
log.Printf("foo! Message is %s", msg)
}
func bar(msg string) {
log.Printf("bar! Message is %s", msg)
}
func main() {
m := map[string] fn {
"f": foo,
"b": bar,
}
log.Printf("map is %v", m)
m["f"]("Hello")
m["b"]("World")
}
#Seth Hoenig's answer helped me best, but I just wanted to add that Go accepts functions with defined return value as well:
package main
func main() {
m := map[string]func(string) string{
"foo": func(s string) string { return s + "nurf" },
}
m["foo"]("baz") // "baznurf"
}
If you think it's ugly, you could always use a type (see #smagch's answer).
I used a map[string]func (a type, b *type) I passed a string to search the map and a pointer to modify the slice.
Hope that helps!
var Exceptions map[string]func(step string, item *structs.Item)
func SetExceptions() {
Exceptions = map[string]func(a string, i *structs.Item){
"step1": step1,
}
}
func RunExceptions(state string, item *structs.Item) {
method, methBool := Exceptions[state]
if methBool {
method(state, item)
}
}
func step1(step string, item *structs.Item) {
item.Title = "Modified"
}
Here is the way I made it work in my case:
package main
import (
"fmt"
)
var routes map[string]func() string
func main() {
routes = map[string]func() string{
"GET /": homePage,
"GET /about": aboutPage,
}
fmt.Println("GET /", pageContent("GET /"))
fmt.Println("GET /about", pageContent("GET /about"))
fmt.Println("GET /unknown", pageContent("GET /unknown"))
// Output:
// GET / Home page
// GET /about About page
// GET /unknown 404: Page Not Found
}
func pageContent(route string) string {
page, ok := routes[route]
if ok {
return page()
} else {
return notFoundPage()
}
}
func homePage() string {
return "Home page"
}
func aboutPage() string {
return "About page"
}
func notFoundPage() string {
return "404: Page Not Found"
}
https://play.golang.org/p/8_g6Di1OKZS
Hope this works for you(you can use interface{} instead any)
package main
import (
"fmt"
)
func toon(v any) {
fmt.Println(v)
}
func main() {
names := map[string]any{
"Function": toon,
}
names["Function"].(func(any))("a")
}