Go Pentester - HTTP Servers(1)
HTTP Server Basics
Use net/http package and useful third-party packages by building simple servers.
Building a Simple Server
package main import (
"fmt"
"net/http"
) func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hello %s\n", r.URL.Query().Get("name"))
} func main() {
http.HandleFunc("/hello", hello)
http.ListenAndServe(":8000",nil)
}
Run the above program and test it.
curl -i http://localhost:8000/hello?name=eric
You can also use http.ListenAndServeTLS(), which will start a server using HTTPS and TLS.
Build a Simple Router
package main import (
"fmt"
"net/http"
) type router struct {
} func (r *router) ServeHTTP(w http.ResponseWriter, req *http.Request) {
switch req.URL.Path {
case "/a":
fmt.Fprintf(w, "Executing /a\n")
case "/b":
fmt.Fprintf(w, "Executing /b\n")
case "/c":
fmt.Fprintf(w, "Executing /c\n")
default:
http.Error(w, "404 Not Found", 404)
}
} func main() {
var r router
http.ListenAndServe(":8000", &r)
}
Test the above program by the following commands.
curl http://localhost:8000/a
curl http://localhost:8000/d
Building simple Middleware
A simple middleware, which is a sort of wrapper that will execute on all incoming requests regardless of the destination function.
package main import (
"fmt"
"log"
"net/http"
"time"
) type logger struct {
Inner http.Handler
} func (l *logger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
log.Printf("start %s\n", time.Now().String())
l.Inner.ServeHTTP(w,r)
log.Printf("finish %s",time.Now().String())
} func hello(w http.ResponseWriter, r *http.Request) {
fmt.Fprint(w, "Hello\n")
} func main() {
f := http.HandlerFunc(hello)
l := logger{Inner: f}
http.ListenAndServe(":8000", &l)
}
Run the program and issue a request.
curl http://localhost:8000
Go Pentester - HTTP Servers(1)的更多相关文章
- Go Pentester - HTTP Servers(2)
Routing with the gorilla/mux Package A powerful HTTP router and URL matcher for building Go web serv ...
- Go Pentester - HTTP Servers(3)
Building Middleware with Negroni Reasons use middleware, including logging requests, authenticating ...
- Coping with the TCP TIME-WAIT state on busy Linux servers
Coping with the TCP TIME-WAIT state on busy Linux servers 文章源自于:https://vincent.bernat.im/en/blog/20 ...
- How To Restart timer service on all servers in farm
[array]$servers= Get-SPServer | ? {$_.Role -eq "Application"} $farm = Get-SPFarm foreach ( ...
- eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Sertomcat
eclipse Run On Server 异常:could not load the Tomcat Server configuration at Servers\tomcat V5.0 Serto ...
- coderforces #387 Servers(模拟)
Servers time limit per test 2 seconds memory limit per test 256 megabytes input standard input outpu ...
- Servers
Servers¶ Server interface. class novaclient.v1_1.servers.Server(manager, info, loaded=False) Bases: ...
- 使用servers 启动项目时 ,一直处于启动中, 最后出现无法的问题。
使用eclipse 中的servers 配置了一个server 来启动项目, 发现无法启动 排除法: 去掉项目配置,单独启动该server ,发现可以启动, 说明是项目出现问题 但是项目并没有报错, ...
- servers中添加server时,看不到运行环境的选择。
servers中添加server时,看不到运行环境的选择. 主要原因是tomcat目录中的配置文件格式不对.
随机推荐
- 研华advantech-凌华ADLINK板卡运动控制卡
研华advantech:6路独立D/A输出12位分辨率双缓冲D/A转换器多种电压范围:+/-10V,+/-5V,0—+5V,0—+10V和4—20mA电流环(汇)16路数字量输入及16路数字量输出 P ...
- redis概要学习
redis 概要学习 redis简介 Redis 是完全开源免费的,遵守BSD协议,是一个高性能的key-value数据库. Redis 与其他 key - value 缓存产品有以下三个特点: Re ...
- SpringCloud教程第2篇:Ribbon(F版本)
一.ribbon简介 Spring cloud有两种服务调用方式,一种是ribbon+restTemplate,另一种是feign.在这一篇文章首先讲解下基于ribbon+rest. ribbon是一 ...
- 《Elasticsearch 权威指南》阅读笔记
书籍地址 https://www.elastic.co/guide/cn/elasticsearch/guide/current/languages.html
- ubuntu上面安装mysql
一.安装mysql 1. 安装需要使用root账号,如果不会设置root账号的请自行google.安装mysql过程中,需要设置mysql的root账号的密码,不要忽略了. sudo apt-get ...
- mybatis缓存之一级缓存(二)
这篇文章介绍下mybatis的一级缓存的生命周期 一级缓存的产生 一级缓存的产生,并不是看mappper的xml文件的select方法,看下面的例子 mapper.xml <select id= ...
- 2020年IDEA破解激活码永久
我想很多做开发的小伙伴和小编一样,和往常一样开机搬砖. 打开idea的时候,会收到一个个提示,也是idea许可证过期啦,需要重新激活! 那怎么办呢?我最近发现了一个相对稳定的激活码 . 亲测可用.现在 ...
- dubbo源码解析之负载均衡
在分布式系统中,负载均衡是必不可少的一个模块,dubbo 中提供了五种负载均衡的实现,在阅读这块源码之前,建议先学习负载均衡的基础知识.把看源码当做一个印证自己心中所想的过程,这样会得到事半功倍的效果 ...
- 一.8.django权限管理/drf权限管理
1.什么是权限管理: .权限管理,一般指根据系统设置的安全规则或者安全策略,用户可以访问而且只能访问自己被授权的资源 .权限管理好比如钥匙,有了钥匙就能把门打开,但是权限设置是有级别之分的,假如这个系 ...
- 洛谷 P1043 数字游戏 区间DP
题目描述 丁丁最近沉迷于一个数字游戏之中.这个游戏看似简单,但丁丁在研究了许多天之后却发觉原来在简单的规则下想要赢得这个游戏并不那么容易.游戏是这样的,在你面前有一圈整数(一共n个),你要按顺序将其分 ...