@@ -14,7 +14,7 @@ import (
14
14
"log"
15
15
"mime"
16
16
"mime/multipart"
17
- . "net/http"
17
+ "net/http"
18
18
"net/textproto"
19
19
"net/url"
20
20
"os"
@@ -107,11 +107,11 @@ type File interface {
107
107
Stat () (os.FileInfo , error )
108
108
}
109
109
110
- func dirList (w ResponseWriter , r * Request , f File ) {
110
+ func dirList (w http. ResponseWriter , r * http. Request , f File ) {
111
111
dirs , err := f .Readdir (- 1 )
112
112
if err != nil {
113
113
logf (r , "http: error reading directory: %v" , err )
114
- Error (w , "Error reading directory" , StatusInternalServerError )
114
+ http . Error (w , "Error reading directory" , http . StatusInternalServerError )
115
115
return
116
116
}
117
117
sort .Slice (dirs , func (i , j int ) bool { return dirs [i ].Name () < dirs [j ].Name () })
@@ -163,7 +163,7 @@ func dirList(w ResponseWriter, r *Request, f File) {
163
163
// ServeContent uses it to handle requests using If-Match, If-None-Match, or If-Range.
164
164
//
165
165
// Note that *os.File implements the io.ReadSeeker interface.
166
- func ServeContent (w ResponseWriter , req * Request , name string , modtime time.Time , content io.ReadSeeker ) {
166
+ func ServeContent (w http. ResponseWriter , req * http. Request , name string , modtime time.Time , content io.ReadSeeker ) {
167
167
sizeFunc := func () (int64 , error ) {
168
168
size , err := content .Seek (0 , io .SeekEnd )
169
169
if err != nil {
@@ -192,14 +192,14 @@ var errNoOverlap = errors.New("invalid range: failed to overlap")
192
192
// if modtime.IsZero(), modtime is unknown.
193
193
// content must be seeked to the beginning of the file.
194
194
// The sizeFunc is called at most once. Its error, if any, is sent in the HTTP response.
195
- func serveContent (w ResponseWriter , r * Request , name string , modtime time.Time , sizeFunc func () (int64 , error ), content io.ReadSeeker ) {
195
+ func serveContent (w http. ResponseWriter , r * http. Request , name string , modtime time.Time , sizeFunc func () (int64 , error ), content io.ReadSeeker ) {
196
196
setLastModified (w , modtime )
197
197
done , rangeReq := checkPreconditions (w , r , modtime )
198
198
if done {
199
199
return
200
200
}
201
201
202
- code := StatusOK
202
+ code := http . StatusOK
203
203
204
204
// If Content-Type isn't set, use the file's extension to find it, but
205
205
// if the Content-Type is unset explicitly, do not sniff the type.
@@ -211,10 +211,10 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
211
211
// read a chunk to decide between utf-8 text and binary
212
212
var buf [sniffLen ]byte
213
213
n , _ := io .ReadFull (content , buf [:])
214
- ctype = DetectContentType (buf [:n ])
214
+ ctype = http . DetectContentType (buf [:n ])
215
215
_ , err := content .Seek (0 , io .SeekStart ) // rewind to output whole file
216
216
if err != nil {
217
- Error (w , "seeker can't seek" , StatusInternalServerError )
217
+ http . Error (w , "seeker can't seek" , http . StatusInternalServerError )
218
218
return
219
219
}
220
220
}
@@ -225,7 +225,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
225
225
226
226
size , err := sizeFunc ()
227
227
if err != nil {
228
- Error (w , err .Error (), StatusInternalServerError )
228
+ http . Error (w , err .Error (), http . StatusInternalServerError )
229
229
return
230
230
}
231
231
@@ -238,7 +238,7 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
238
238
if err == errNoOverlap {
239
239
w .Header ().Set ("Content-Range" , fmt .Sprintf ("bytes */%d" , size ))
240
240
}
241
- Error (w , err .Error (), StatusRequestedRangeNotSatisfiable )
241
+ http . Error (w , err .Error (), http . StatusRequestedRangeNotSatisfiable )
242
242
return
243
243
}
244
244
if sumRangesSize (ranges ) > size {
@@ -263,15 +263,15 @@ func serveContent(w ResponseWriter, r *Request, name string, modtime time.Time,
263
263
// multipart responses."
264
264
ra := ranges [0 ]
265
265
if _ , err := content .Seek (ra .start , io .SeekStart ); err != nil {
266
- Error (w , err .Error (), StatusRequestedRangeNotSatisfiable )
266
+ http . Error (w , err .Error (), http . StatusRequestedRangeNotSatisfiable )
267
267
return
268
268
}
269
269
sendSize = ra .length
270
- code = StatusPartialContent
270
+ code = http . StatusPartialContent
271
271
w .Header ().Set ("Content-Range" , ra .contentRange (size ))
272
272
case len (ranges ) > 1 :
273
273
sendSize = rangesMIMESize (ranges , ctype , size )
274
- code = StatusPartialContent
274
+ code = http . StatusPartialContent
275
275
276
276
pr , pw := io .Pipe ()
277
277
mw := multipart .NewWriter (pw )
@@ -362,7 +362,7 @@ const (
362
362
condFalse
363
363
)
364
364
365
- func checkIfMatch (w ResponseWriter , r * Request ) condResult {
365
+ func checkIfMatch (w http. ResponseWriter , r * http. Request ) condResult {
366
366
im := r .Header .Get ("If-Match" )
367
367
if im == "" {
368
368
return condNone
@@ -392,12 +392,12 @@ func checkIfMatch(w ResponseWriter, r *Request) condResult {
392
392
return condFalse
393
393
}
394
394
395
- func checkIfUnmodifiedSince (r * Request , modtime time.Time ) condResult {
395
+ func checkIfUnmodifiedSince (r * http. Request , modtime time.Time ) condResult {
396
396
ius := r .Header .Get ("If-Unmodified-Since" )
397
397
if ius == "" || isZeroTime (modtime ) {
398
398
return condNone
399
399
}
400
- if t , err := ParseTime (ius ); err == nil {
400
+ if t , err := http . ParseTime (ius ); err == nil {
401
401
// The Date-Modified header truncates sub-second precision, so
402
402
// use mtime < t+1s instead of mtime <= t to check for unmodified.
403
403
if modtime .Before (t .Add (1 * time .Second )) {
@@ -408,7 +408,7 @@ func checkIfUnmodifiedSince(r *Request, modtime time.Time) condResult {
408
408
return condNone
409
409
}
410
410
411
- func checkIfNoneMatch (w ResponseWriter , r * Request ) condResult {
411
+ func checkIfNoneMatch (w http. ResponseWriter , r * http. Request ) condResult {
412
412
inm := get (r .Header , "If-None-Match" )
413
413
if inm == "" {
414
414
return condNone
@@ -437,15 +437,15 @@ func checkIfNoneMatch(w ResponseWriter, r *Request) condResult {
437
437
return condTrue
438
438
}
439
439
440
- func checkIfModifiedSince (r * Request , modtime time.Time ) condResult {
440
+ func checkIfModifiedSince (r * http. Request , modtime time.Time ) condResult {
441
441
if r .Method != "GET" && r .Method != "HEAD" {
442
442
return condNone
443
443
}
444
444
ims := r .Header .Get ("If-Modified-Since" )
445
445
if ims == "" || isZeroTime (modtime ) {
446
446
return condNone
447
447
}
448
- t , err := ParseTime (ims )
448
+ t , err := http . ParseTime (ims )
449
449
if err != nil {
450
450
return condNone
451
451
}
@@ -457,7 +457,7 @@ func checkIfModifiedSince(r *Request, modtime time.Time) condResult {
457
457
return condTrue
458
458
}
459
459
460
- func checkIfRange (w ResponseWriter , r * Request , modtime time.Time ) condResult {
460
+ func checkIfRange (w http. ResponseWriter , r * http. Request , modtime time.Time ) condResult {
461
461
if r .Method != "GET" && r .Method != "HEAD" {
462
462
return condNone
463
463
}
@@ -478,7 +478,7 @@ func checkIfRange(w ResponseWriter, r *Request, modtime time.Time) condResult {
478
478
if modtime .IsZero () {
479
479
return condFalse
480
480
}
481
- t , err := ParseTime (ir )
481
+ t , err := http . ParseTime (ir )
482
482
if err != nil {
483
483
return condFalse
484
484
}
@@ -495,13 +495,13 @@ func isZeroTime(t time.Time) bool {
495
495
return t .IsZero () || t .Equal (unixEpochTime )
496
496
}
497
497
498
- func setLastModified (w ResponseWriter , modtime time.Time ) {
498
+ func setLastModified (w http. ResponseWriter , modtime time.Time ) {
499
499
if ! isZeroTime (modtime ) {
500
- w .Header ().Set ("Last-Modified" , modtime .UTC ().Format (TimeFormat ))
500
+ w .Header ().Set ("Last-Modified" , modtime .UTC ().Format (http . TimeFormat ))
501
501
}
502
502
}
503
503
504
- func writeNotModified (w ResponseWriter ) {
504
+ func writeNotModified (w http. ResponseWriter ) {
505
505
// RFC 7232 section 4.1:
506
506
// a sender SHOULD NOT generate representation metadata other than the
507
507
// above listed fields unless said metadata exists for the purpose of
@@ -513,19 +513,19 @@ func writeNotModified(w ResponseWriter) {
513
513
if h .Get ("Etag" ) != "" {
514
514
delete (h , "Last-Modified" )
515
515
}
516
- w .WriteHeader (StatusNotModified )
516
+ w .WriteHeader (http . StatusNotModified )
517
517
}
518
518
519
519
// checkPreconditions evaluates request preconditions and reports whether a precondition
520
520
// resulted in sending StatusNotModified or StatusPreconditionFailed.
521
- func checkPreconditions (w ResponseWriter , r * Request , modtime time.Time ) (done bool , rangeHeader string ) {
521
+ func checkPreconditions (w http. ResponseWriter , r * http. Request , modtime time.Time ) (done bool , rangeHeader string ) {
522
522
// This function carefully follows RFC 7232 section 6.
523
523
ch := checkIfMatch (w , r )
524
524
if ch == condNone {
525
525
ch = checkIfUnmodifiedSince (r , modtime )
526
526
}
527
527
if ch == condFalse {
528
- w .WriteHeader (StatusPreconditionFailed )
528
+ w .WriteHeader (http . StatusPreconditionFailed )
529
529
return true , ""
530
530
}
531
531
switch checkIfNoneMatch (w , r ) {
@@ -534,7 +534,7 @@ func checkPreconditions(w ResponseWriter, r *Request, modtime time.Time) (done b
534
534
writeNotModified (w )
535
535
return true , ""
536
536
} else {
537
- w .WriteHeader (StatusPreconditionFailed )
537
+ w .WriteHeader (http . StatusPreconditionFailed )
538
538
return true , ""
539
539
}
540
540
case condNone :
@@ -552,7 +552,7 @@ func checkPreconditions(w ResponseWriter, r *Request, modtime time.Time) (done b
552
552
}
553
553
554
554
// name is '/'-separated, not filepath.Separator.
555
- func serveFile (w ResponseWriter , r * Request , fs FileSystem , name string , redirect bool ) {
555
+ func serveFile (w http. ResponseWriter , r * http. Request , fs FileSystem , name string , redirect bool ) {
556
556
const indexPage = "/index.html"
557
557
558
558
// redirect .../index.html to .../
@@ -566,15 +566,15 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
566
566
f , err := fs .Open (name )
567
567
if err != nil {
568
568
msg , code := toHTTPError (err )
569
- Error (w , msg , code )
569
+ http . Error (w , msg , code )
570
570
return
571
571
}
572
572
defer f .Close ()
573
573
574
574
d , err := f .Stat ()
575
575
if err != nil {
576
576
msg , code := toHTTPError (err )
577
- Error (w , msg , code )
577
+ http . Error (w , msg , code )
578
578
return
579
579
}
580
580
@@ -625,7 +625,7 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
625
625
writeNotModified (w )
626
626
return
627
627
}
628
- w .Header ().Set ("Last-Modified" , d .ModTime ().UTC ().Format (TimeFormat ))
628
+ w .Header ().Set ("Last-Modified" , d .ModTime ().UTC ().Format (http . TimeFormat ))
629
629
dirList (w , r , f )
630
630
return
631
631
}
@@ -642,23 +642,23 @@ func serveFile(w ResponseWriter, r *Request, fs FileSystem, name string, redirec
642
642
// all errors. We don't want to start leaking information in error messages.
643
643
func toHTTPError (err error ) (msg string , httpStatus int ) {
644
644
if os .IsNotExist (err ) {
645
- return "404 page not found" , StatusNotFound
645
+ return "404 page not found" , http . StatusNotFound
646
646
}
647
647
if os .IsPermission (err ) {
648
- return "403 Forbidden" , StatusForbidden
648
+ return "403 Forbidden" , http . StatusForbidden
649
649
}
650
650
// Default:
651
- return "500 Internal Server Error" , StatusInternalServerError
651
+ return "500 Internal Server Error" , http . StatusInternalServerError
652
652
}
653
653
654
654
// localRedirect gives a Moved Permanently response.
655
655
// It does not convert relative paths to absolute paths like Redirect does.
656
- func localRedirect (w ResponseWriter , r * Request , newPath string ) {
656
+ func localRedirect (w http. ResponseWriter , r * http. Request , newPath string ) {
657
657
if q := r .URL .RawQuery ; q != "" {
658
658
newPath += "?" + q
659
659
}
660
660
w .Header ().Set ("Location" , newPath )
661
- w .WriteHeader (StatusMovedPermanently )
661
+ w .WriteHeader (http . StatusMovedPermanently )
662
662
}
663
663
664
664
// ServeFile replies to the request with the contents of the named
@@ -682,15 +682,15 @@ func localRedirect(w ResponseWriter, r *Request, newPath string) {
682
682
// Outside of those two special cases, ServeFile does not use
683
683
// r.URL.Path for selecting the file or directory to serve; only the
684
684
// file or directory provided in the name argument is used.
685
- func ServeFile (w ResponseWriter , r * Request , name string ) {
685
+ func ServeFile (w http. ResponseWriter , r * http. Request , name string ) {
686
686
log .Printf ("request method %s\n " , r .Method )
687
687
if containsDotDot (r .URL .Path ) {
688
688
// Too many programs use r.URL.Path to construct the argument to
689
689
// serveFile. Reject the request under the assumption that happened
690
690
// here and ".." may not be wanted.
691
691
// Note that name might not contain "..", for example if code (still
692
692
// incorrectly) used filepath.Join(myDir, r.URL.Path).
693
- Error (w , "invalid URL path" , StatusBadRequest )
693
+ http . Error (w , "invalid URL path" , http . StatusBadRequest )
694
694
return
695
695
}
696
696
dir , file := filepath .Split (name )
@@ -726,11 +726,11 @@ type fileHandler struct {
726
726
// As a special case, the returned file server redirects any request
727
727
// ending in "/index.html" to the same path, without the final
728
728
// "index.html".
729
- func FileServer (root FileSystem ) Handler {
729
+ func FileServer (root FileSystem ) http. Handler {
730
730
return & fileHandler {root }
731
731
}
732
732
733
- func (f * fileHandler ) ServeHTTP (w ResponseWriter , r * Request ) {
733
+ func (f * fileHandler ) ServeHTTP (w http. ResponseWriter , r * http. Request ) {
734
734
upath := r .URL .Path
735
735
if ! strings .HasPrefix (upath , "/" ) {
736
736
upath = "/" + upath
@@ -745,7 +745,7 @@ func (f *fileHandler) ServeHTTP(w ResponseWriter, r *Request) {
745
745
}
746
746
747
747
// upload file
748
- func uploadFile (w ResponseWriter , r * Request , fs FileSystem , name string ) {
748
+ func uploadFile (w http. ResponseWriter , r * http. Request , fs FileSystem , name string ) {
749
749
r .ParseMultipartForm (32 << 20 )
750
750
file , handler , err := r .FormFile ("file" )
751
751
if err != nil {
@@ -892,8 +892,8 @@ func sumRangesSize(ranges []httpRange) (size int64) {
892
892
}
893
893
894
894
// is nil, logging is done via the log package's standard logger.
895
- func logf (r * Request , format string , args ... interface {}) {
896
- s , _ := r .Context ().Value (ServerContextKey ).(* Server )
895
+ func logf (r * http. Request , format string , args ... interface {}) {
896
+ s , _ := r .Context ().Value (http . ServerContextKey ).(* http. Server )
897
897
if s != nil && s .ErrorLog != nil {
898
898
s .ErrorLog .Printf (format , args ... )
899
899
} else {
@@ -914,7 +914,7 @@ var htmlReplacer = strings.NewReplacer(
914
914
const sniffLen = 512
915
915
916
916
// get is like Get, but key must already be in CanonicalHeaderKey form.
917
- func get (h Header , key string ) string {
917
+ func get (h http. Header , key string ) string {
918
918
if v := h [key ]; len (v ) > 0 {
919
919
return v [0 ]
920
920
}
0 commit comments