github.com/hxx258456/ccgo@v0.0.5-0.20230213014102-48b35f46f66f/net/webdav/litmus_test_server.go (about)

     1  // Copyright 2015 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  //go:build ignore
     6  // +build ignore
     7  
     8  /*
     9  This program is a server for the WebDAV 'litmus' compliance test at
    10  http://www.webdav.org/neon/litmus/
    11  To run the test:
    12  
    13  go run litmus_test_server.go
    14  
    15  and separately, from the downloaded litmus-xxx directory:
    16  
    17  make URL=http://localhost:9999/ check
    18  */
    19  package main
    20  
    21  import (
    22  	"flag"
    23  	"fmt"
    24  	http "github.com/hxx258456/ccgo/gmhttp"
    25  	"log"
    26  	"net/url"
    27  
    28  	"github.com/hxx258456/ccgo/net/webdav"
    29  )
    30  
    31  var port = flag.Int("port", 9999, "server port")
    32  
    33  func main() {
    34  	flag.Parse()
    35  	log.SetFlags(0)
    36  	h := &webdav.Handler{
    37  		FileSystem: webdav.NewMemFS(),
    38  		LockSystem: webdav.NewMemLS(),
    39  		Logger: func(r *http.Request, err error) {
    40  			litmus := r.Header.Get("X-Litmus")
    41  			if len(litmus) > 19 {
    42  				litmus = litmus[:16] + "..."
    43  			}
    44  
    45  			switch r.Method {
    46  			case "COPY", "MOVE":
    47  				dst := ""
    48  				if u, err := url.Parse(r.Header.Get("Destination")); err == nil {
    49  					dst = u.Path
    50  				}
    51  				o := r.Header.Get("Overwrite")
    52  				log.Printf("%-20s%-10s%-30s%-30so=%-2s%v", litmus, r.Method, r.URL.Path, dst, o, err)
    53  			default:
    54  				log.Printf("%-20s%-10s%-30s%v", litmus, r.Method, r.URL.Path, err)
    55  			}
    56  		},
    57  	}
    58  
    59  	// The next line would normally be:
    60  	//	http.Handle("/", h)
    61  	// but we wrap that HTTP handler h to cater for a special case.
    62  	//
    63  	// The propfind_invalid2 litmus test case expects an empty namespace prefix
    64  	// declaration to be an error. The FAQ in the webdav litmus test says:
    65  	//
    66  	// "What does the "propfind_invalid2" test check for?...
    67  	//
    68  	// If a request was sent with an XML body which included an empty namespace
    69  	// prefix declaration (xmlns:ns1=""), then the server must reject that with
    70  	// a "400 Bad Request" response, as it is invalid according to the XML
    71  	// Namespace specification."
    72  	//
    73  	// On the other hand, the Go standard library's encoding/xml package
    74  	// accepts an empty xmlns namespace, as per the discussion at
    75  	// https://github.com/golang/go/issues/8068
    76  	//
    77  	// Empty namespaces seem disallowed in the second (2006) edition of the XML
    78  	// standard, but allowed in a later edition. The grammar differs between
    79  	// http://www.w3.org/TR/2006/REC-xml-names-20060816/#ns-decl and
    80  	// http://www.w3.org/TR/REC-xml-names/#dt-prefix
    81  	//
    82  	// Thus, we assume that the propfind_invalid2 test is obsolete, and
    83  	// hard-code the 400 Bad Request response that the test expects.
    84  	http.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    85  		if r.Header.Get("X-Litmus") == "props: 3 (propfind_invalid2)" {
    86  			http.Error(w, "400 Bad Request", http.StatusBadRequest)
    87  			return
    88  		}
    89  		h.ServeHTTP(w, r)
    90  	}))
    91  
    92  	addr := fmt.Sprintf(":%d", *port)
    93  	log.Printf("Serving %v", addr)
    94  	log.Fatal(http.ListenAndServe(addr, nil))
    95  }