github.com/slspeek/camlistore_namedsearch@v0.0.0-20140519202248-ed6f70f7721a/old/camwebdav/response.go (about)

     1  // +build THIS_IS_BROKEN
     2  
     3  package main
     4  
     5  import (
     6  	"bytes"
     7  	"fmt"
     8  	"net/http"
     9  	"net/url"
    10  	"text/template"
    11  	"time"
    12  )
    13  
    14  type xmler interface {
    15  	XML(b *bytes.Buffer)
    16  }
    17  
    18  // See: http://www.webdav.org/specs/rfc4918.html
    19  
    20  // 14.7 href XML Element
    21  type href url.URL
    22  
    23  func (h *href) XML(b *bytes.Buffer) {
    24  	b.WriteString("<href>" + template.HTMLEscapeString((*url.URL)(h).String()) + "</href>")
    25  }
    26  
    27  // 14.16 multistatus XML Element
    28  type multistatus []*response
    29  
    30  func (m multistatus) XML(b *bytes.Buffer) {
    31  	b.WriteString("<multistatus xmlns='DAV:'>")
    32  	for _, r := range m {
    33  		r.XML(b)
    34  	}
    35  	b.WriteString("</multistatus>")
    36  }
    37  
    38  // 14.24 response XML Element
    39  type response struct {
    40  	href *href
    41  	body xmler // hrefsstatus OR propstats
    42  }
    43  
    44  func (r *response) XML(b *bytes.Buffer) {
    45  	b.WriteString("<response>")
    46  	r.href.XML(b)
    47  	r.body.XML(b)
    48  	b.WriteString("</response>")
    49  }
    50  
    51  // part of 14.24 response XML element
    52  
    53  type hrefsstatus struct {
    54  	hrefs  []*href
    55  	status status
    56  }
    57  
    58  func (hs *hrefsstatus) XML(b *bytes.Buffer) {
    59  	for _, h := range hs.hrefs {
    60  		h.XML(b)
    61  	}
    62  	hs.status.XML(b)
    63  }
    64  
    65  // part of 14.24 response element
    66  
    67  type propstats []propstat
    68  
    69  func (p propstats) XML(b *bytes.Buffer) {
    70  	b.WriteString("<propstat>")
    71  	for _, prop := range p {
    72  		prop.XML(b)
    73  	}
    74  	b.WriteString("</propstat>")
    75  }
    76  
    77  // 14.22 propstat XML Element
    78  type propstat struct {
    79  	props  []xmler
    80  	status status
    81  }
    82  
    83  func (p *propstat) XML(b *bytes.Buffer) {
    84  	b.WriteString("<prop>")
    85  	for _, prop := range p.props {
    86  		prop.XML(b)
    87  	}
    88  	b.WriteString("</prop>")
    89  	p.status.XML(b)
    90  }
    91  
    92  // 14.28 status XML element
    93  type status int
    94  
    95  func (s status) XML(b *bytes.Buffer) {
    96  	b.WriteString(fmt.Sprintf("<status>HTTP/1.1 %d %s</status>", s, template.HTMLEscapeString(http.StatusText(int(s)))))
    97  }
    98  
    99  // 15.1 creationdate Property
   100  type creationdate uint64 // seconds from unix epoch
   101  
   102  func (c creationdate) XML(b *bytes.Buffer) {
   103  	b.WriteString("<creationdate>")
   104  	b.WriteString(epochToXMLTime(int64(c)))
   105  	b.WriteString("</creationdate>")
   106  }
   107  
   108  // 15.4 getcontentlength Property
   109  type getcontentlength uint64
   110  
   111  func (l getcontentlength) XML(b *bytes.Buffer) {
   112  
   113  	b.WriteString("<getcontentlength>")
   114  	b.WriteString(fmt.Sprint(l))
   115  	b.WriteString("</getcontentlength>")
   116  }
   117  
   118  // 15.7 getlastmodified Property
   119  type getlastmodified uint64 // seconds from unix epoch
   120  func (g getlastmodified) XML(b *bytes.Buffer) {
   121  	b.WriteString("<getlastmodified>")
   122  	b.WriteString(epochToXMLTime(int64(g)))
   123  	b.WriteString("</getlastmodified>")
   124  }
   125  
   126  // 15.9 resourcetype Property
   127  type resourcetype bool // true if collection (directory), false otherwise
   128  
   129  func (r resourcetype) XML(b *bytes.Buffer) {
   130  	b.WriteString("<resourcetype>")
   131  	if r {
   132  		b.WriteString("<collection/>")
   133  	}
   134  	b.WriteString("</resourcetype>")
   135  }
   136  
   137  // helpers
   138  func epochToXMLTime(sec int64) string {
   139  	return template.HTMLEscapeString(time.Unix(sec, 0).UTC().Format(time.RFC3339))
   140  }