Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
go
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
go
Commits
2a8ea0d1
Commit
2a8ea0d1
authored
Jun 02, 2011
by
Brad Fitzpatrick
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
http: catch panics
R=rsc CC=golang-dev
https://golang.org/cl/4559067
parent
191a6bfc
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
1 deletion
+47
-1
src/pkg/http/serve_test.go
src/pkg/http/serve_test.go
+18
-0
src/pkg/http/server.go
src/pkg/http/server.go
+29
-1
No files found.
src/pkg/http/serve_test.go
View file @
2a8ea0d1
...
...
@@ -13,6 +13,7 @@ import (
.
"http"
"http/httptest"
"io/ioutil"
"log"
"os"
"net"
"reflect"
...
...
@@ -432,6 +433,9 @@ func TestSetsRemoteAddr(t *testing.T) {
}
func
TestChunkedResponseHeaders
(
t
*
testing
.
T
)
{
log
.
SetOutput
(
ioutil
.
Discard
)
// is noisy otherwise
defer
log
.
SetOutput
(
os
.
Stderr
)
ts
:=
httptest
.
NewServer
(
HandlerFunc
(
func
(
w
ResponseWriter
,
r
*
Request
)
{
w
.
Header
()
.
Set
(
"Content-Length"
,
"intentional gibberish"
)
// we check that this is deleted
fmt
.
Fprintf
(
w
,
"I am a chunked response."
)
...
...
@@ -755,6 +759,20 @@ func TestZeroLengthPostAndResponse(t *testing.T) {
}
}
func
TestHandlerPanic
(
t
*
testing
.
T
)
{
log
.
SetOutput
(
ioutil
.
Discard
)
// is noisy otherwise
defer
log
.
SetOutput
(
os
.
Stderr
)
ts
:=
httptest
.
NewServer
(
HandlerFunc
(
func
(
ResponseWriter
,
*
Request
)
{
panic
(
"intentional death for testing"
)
}))
defer
ts
.
Close
()
_
,
err
:=
Get
(
ts
.
URL
)
if
err
==
nil
{
t
.
Logf
(
"expected an error"
)
}
}
func
BenchmarkClientServer
(
b
*
testing
.
B
)
{
b
.
StopTimer
()
ts
:=
httptest
.
NewServer
(
HandlerFunc
(
func
(
rw
ResponseWriter
,
r
*
Request
)
{
...
...
src/pkg/http/server.go
View file @
2a8ea0d1
...
...
@@ -6,12 +6,12 @@
// TODO(rsc):
// logging
// post support
package
http
import
(
"bufio"
"bytes"
"crypto/rand"
"crypto/tls"
"fmt"
...
...
@@ -20,6 +20,7 @@ import (
"net"
"os"
"path"
"runtime"
"strconv"
"strings"
"sync"
...
...
@@ -475,6 +476,33 @@ func (c *conn) close() {
// Serve a new connection.
func
(
c
*
conn
)
serve
()
{
defer
func
()
{
err
:=
recover
()
if
err
==
nil
{
return
}
c
.
rwc
.
Close
()
// TODO(rsc,bradfitz): this is boilerplate. move it to runtime.Stack()
var
buf
bytes
.
Buffer
fmt
.
Fprintf
(
&
buf
,
"http: panic serving %v: %v
\n
"
,
c
.
remoteAddr
,
err
)
for
i
:=
1
;
i
<
20
;
i
++
{
pc
,
file
,
line
,
ok
:=
runtime
.
Caller
(
i
)
if
!
ok
{
break
}
var
name
string
f
:=
runtime
.
FuncForPC
(
pc
)
if
f
!=
nil
{
name
=
f
.
Name
()
}
else
{
name
=
fmt
.
Sprintf
(
"%#x"
,
pc
)
}
fmt
.
Fprintf
(
&
buf
,
" %s %s:%d
\n
"
,
name
,
file
,
line
)
}
log
.
Print
(
buf
.
String
())
}()
for
{
w
,
err
:=
c
.
readRequest
()
if
err
!=
nil
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment