litmus_test_server.go 2.7 KB

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