Closed ali-zohrevand closed 6 years ago
Hmm, I'm not sure. captcha.Server uses the standard HTTP handler:
https://github.com/dchest/captcha/blob/master/server.go#L70-L71
and then splits request's URL.Path into directory and file component. Perhaps, it should do something different?
I wrote an example, hope this helps.
package main
import (
"fmt"
"github.com/dchest/captcha"
"github.com/julienschmidt/httprouter"
"io"
"log"
"net/http"
"text/template"
)
var formTemplate = template.Must(template.New("example").Parse(formTemplateSrc))
func showFormHandler(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
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, _ httprouter.Params) {
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 main() {
r := httprouter.New()
r.GET("/", showFormHandler)
r.POST("/process", processFormHandler)
captchaHanlder := captcha.Server(captcha.StdWidth, captcha.StdHeight)
r.GET("/captcha/*filepath", func(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
captchaHanlder.ServeHTTP(w, r)
})
fmt.Println("Server is at localhost:8666")
if err := http.ListenAndServe("localhost:8666", r); err != nil {
log.Fatal(err)
}
}
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="ja">Japanese</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>
<a href="#" onclick="reload()">Reload</a> | <a href="#" onclick="playAudio()">Play Audio</a>
<audio id=audio controls style="display:none" src="/captcha/{{.CaptchaId}}.wav" preload=none>
You browser doesn't support audio.
<a href="/captcha/download/{{.CaptchaId}}.wav">Download file</a> 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>
`
thank's man it's worked
hi I've been trying to modify your example to use julienschmidt routing but keep coming up with a 404 error when accessing /captcha/
instead of your code in default http route i have used this code
r.Handler(http.MethodGet,"/captcha/" ,captcha.Server(captcha.StdWidth, captcha.StdHeight))
but still i do not access to "captcha" directory and .png file too. how could i use your library with julienschmidt http routing?