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