├── Godeps ├── Godeps.json └── Readme ├── LICENSE ├── demo.sh ├── fileio └── fileio.go ├── ldp ├── node.go └── settings.go ├── main.go ├── rdf ├── ast.go ├── ast_test.go ├── graph.go ├── graph_test.go ├── scanner.go ├── scanner_test.go ├── tokenizer.go ├── tokenizer_test.go ├── triple.go ├── triple_test.go ├── turtle.go ├── turtle_test.go ├── uris.go ├── w3cbase.nt └── w3ctest.nt ├── readme.md ├── server ├── minter.go ├── nonrdf.go ├── rdf.go ├── root.go ├── server.go └── server_test.go ├── textstore ├── textstore.go └── textstore_test.go ├── util ├── util.go └── util_test.go ├── vendor └── github.com │ └── hectorcorrea │ └── rdf │ ├── LICENSE │ ├── README.md │ ├── ast.go │ ├── graph.go │ ├── scanner.go │ ├── tokenizer.go │ ├── triple.go │ ├── turtle.go │ ├── uris.go │ ├── w3cbase.nt │ └── w3ctest.nt └── web ├── delete.go ├── get.go ├── misc.go ├── options.go ├── patch.go ├── post.go ├── postput.go ├── put.go └── web.go /Godeps/Godeps.json: -------------------------------------------------------------------------------- 1 | { 2 | "ImportPath": "ldpserver", 3 | "GoVersion": "go1.6", 4 | "GodepVersion": "v62", 5 | "Deps": [ 6 | { 7 | "ImportPath": "github.com/hectorcorrea/rdf", 8 | "Rev": "5a24f00a5314ea91d4c12391890668b18683faaf" 9 | } 10 | ] 11 | } 12 | -------------------------------------------------------------------------------- /Godeps/Readme: -------------------------------------------------------------------------------- 1 | This directory tree is generated automatically by godep. 2 | 3 | Please do not edit. 4 | 5 | See https://github.com/tools/godep for more information. 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Hector Correa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /demo.sh: -------------------------------------------------------------------------------- 1 | #!/bin/sh 2 | 3 | # This script shows how to create a blog with one entry 4 | # and two comments for the entry. 5 | # 6 | # The resulting URLs will look more or less as follows: 7 | # /node1 8 | # /node1/entry 9 | # /node1/entry/content 10 | # /node1/entry/comments 11 | # /node1/entry/comments/comment1 12 | # /node1/entry/comments/comment2 13 | # 14 | # Make sure the LDP server is running on localhost:9001 15 | # 16 | 17 | # Create a blog 18 | BLOG_URI="$(curl -X POST localhost:9001)" 19 | 20 | # Add an entry to the blog 21 | ENTRY_URI="$(curl -X POST --header "Content-Type: text/turtle" --header 'Slug: entry' -d '<> dc:title "blog one title" .' ${BLOG_URI})" 22 | 23 | # Add the content for the blog (non-RDF) 24 | CONTENT_URI="$(curl -X POST --header "Content-Type: text/plain" --header 'Slug: content' -d 'content of the blog entry' ${BLOG_URI})" 25 | 26 | # Create a direct container for comments 27 | # and bind it to the entry 28 | DC_TRIPLES="<> hasComment ; <${ENTRY_URI}> ." 29 | COMMENTS_URI="$(curl -X POST --header "Content-Type: text/turtle" --header 'Slug: comments' -d "${DC_TRIPLES}" ${ENTRY_URI})" 30 | 31 | # Add a couple of comments to the direct container 32 | COMMENT1_URI="$(curl -X POST --header "Content-Type: text/turtle" --header 'Slug: comment1' -d $'<> dc:description "this is a comment" .' ${COMMENTS_URI})" 33 | COMMENT2_URI="$(curl -X POST --header "Content-Type: text/turtle" --header "Slug: comment2" -d $'<> dc:description "this is another comment" .' ${COMMENTS_URI})" 34 | 35 | echo "** The following URIs were created:" 36 | echo " BLOG_URI = ${BLOG_URI}" 37 | echo " ENTRY_URI = ${ENTRY_URI}" 38 | echo " CONTENT_URI = ${CONTENT_URI}" 39 | echo " COMMENTS_URI = ${COMMENTS_URI}" 40 | echo " COMMENT1_URI = ${COMMENT1_URI}" 41 | echo " COMMENT2_URI = ${COMMENT2_URI}" 42 | 43 | echo "** Blog entry:" 44 | curl ${ENTRY_URI} 45 | 46 | echo "** Direct container:" 47 | curl ${COMMENTS_URI} 48 | -------------------------------------------------------------------------------- /fileio/fileio.go: -------------------------------------------------------------------------------- 1 | package fileio 2 | 3 | import "os" 4 | import "io" 5 | import "io/ioutil" 6 | import "path/filepath" 7 | 8 | // http://stackoverflow.com/a/18415935/446681 9 | var normalAccess os.FileMode = 0644 10 | 11 | func WriteFile(filename, content string) error { 12 | err := createPathForFilename(filename) 13 | if err != nil { 14 | return err 15 | } 16 | return ioutil.WriteFile(filename, []byte(content), normalAccess) 17 | } 18 | 19 | func CreateFile(filename string, content string) error { 20 | err := createPathForFilename(filename) 21 | if err != nil { 22 | return err 23 | } 24 | 25 | file, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_EXCL, normalAccess) 26 | if err != nil { 27 | return err 28 | } 29 | defer file.Close() 30 | _, err = file.WriteString(content) 31 | return err 32 | } 33 | 34 | func AppendToFile(filename, text string) error { 35 | file, err := os.OpenFile(filename, os.O_APPEND|os.O_WRONLY, 0600) 36 | if err != nil { 37 | return err 38 | } 39 | defer file.Close() 40 | _, err = file.WriteString(text) 41 | return err 42 | } 43 | 44 | func FileExists(filename string) bool { 45 | // http://stackoverflow.com/a/12518877/446681 46 | _, err := os.Stat(filename) 47 | return !os.IsNotExist(err) 48 | } 49 | 50 | func ReadFile(filename string) (content string, err error) { 51 | bytes, err := ioutil.ReadFile(filename) 52 | if err == nil { 53 | content = string(bytes) 54 | } 55 | return content, err 56 | } 57 | 58 | func ReaderToString(reader io.ReadCloser) (string, error) { 59 | bytes, err := ioutil.ReadAll(reader) 60 | if err != nil { 61 | return "", err 62 | } 63 | return string(bytes[:]), nil 64 | } 65 | 66 | func createPathForFilename(filename string) error { 67 | path := filepath.Dir(filename) 68 | if err := os.MkdirAll(path, 0777); err != nil { 69 | return err 70 | } 71 | return nil 72 | } 73 | -------------------------------------------------------------------------------- /ldp/node.go: -------------------------------------------------------------------------------- 1 | package ldp 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "github.com/hectorcorrea/rdf" 7 | "io" 8 | "ldpserver/textstore" 9 | "ldpserver/util" 10 | "log" 11 | "strings" 12 | "time" 13 | ) 14 | 15 | var NodeNotFoundError = errors.New("Node not found") 16 | var DuplicateNodeError = errors.New("Node already exists") 17 | var EtagMissingError = errors.New("Missing Etag") 18 | var EtagMismatchError = errors.New("Etag mismatch") 19 | var ServerManagedPropertyError = errors.New("Attempted to update server managed property") 20 | 21 | const etagPredicate = "<" + rdf.ServerETagUri + ">" 22 | const rdfTypePredicate = "<" + rdf.RdfTypeUri + ">" 23 | const contentTypePredicate = "<" + rdf.ServerContentTypeUri + ">" 24 | 25 | type PreferTriples struct { 26 | Containment bool 27 | Membership bool 28 | MinimalContainer bool 29 | } 30 | 31 | type Node struct { 32 | isRdf bool 33 | uri string // http://localhost/node1 34 | subject string // 35 | headers map[string][]string 36 | graph rdf.RdfGraph 37 | graphExtra rdf.RdfGraph // triples from included resources (see PreferTriples) 38 | binary string // should be []byte or reader 39 | 40 | settings Settings 41 | rootUri string // http://localhost/ 42 | store textstore.Store 43 | 44 | isBasicContainer bool 45 | isDirectContainer bool 46 | membershipResource string 47 | hasMemberRelation string 48 | // TODO isMemberOfRelation string 49 | } 50 | 51 | func (node Node) AddChild(child Node) error { 52 | triple := rdf.NewTriple(node.subject, "<"+rdf.LdpContainsUri+">", child.subject) 53 | err := node.store.AppendToMetaFile(triple.StringLn()) 54 | if err != nil { 55 | return err 56 | } 57 | 58 | if node.isDirectContainer { 59 | return node.addDirectContainerChild(child) 60 | } 61 | return nil 62 | } 63 | 64 | func (node Node) ContentPref(pref PreferTriples) string { 65 | if node.isRdf { 66 | var triples rdf.RdfGraph 67 | if pref.MinimalContainer { 68 | // All but ldpContains 69 | for _, triple := range node.graph { 70 | if !triple.Is("<" + rdf.LdpContainsUri + ">") { 71 | triples = append(triples, triple) 72 | } 73 | } 74 | } else { 75 | triples = node.graph 76 | } 77 | triplesStr := triples.String() 78 | if node.graphExtra != nil { 79 | triplesStr += "\n" + node.graphExtra.String() 80 | } 81 | return triplesStr 82 | } 83 | return node.binary 84 | } 85 | 86 | func (node Node) Content() string { 87 | if node.isRdf { 88 | return node.graph.String() 89 | } 90 | return node.binary 91 | } 92 | 93 | func (node Node) Metadata() string { 94 | return node.graph.String() 95 | } 96 | 97 | func (node Node) contentType() string { 98 | if node.isRdf { 99 | return rdf.TurtleContentType 100 | } 101 | triple, found := node.graph.FindPredicate(node.subject, contentTypePredicate) 102 | if !found { 103 | return "application/binary" 104 | } 105 | return util.RemoveQuotes(triple.Object()) 106 | } 107 | 108 | func (node Node) DebugString() string { 109 | if !node.isRdf { 110 | return fmt.Sprintf("Non-RDF: %s", node.uri) 111 | } 112 | 113 | triples := "" 114 | for i, triple := range node.graph { 115 | triples += fmt.Sprintf("%d %s\n", i, triple) 116 | } 117 | debugString := fmt.Sprintf("RDF: %s\n %s", node.uri, triples) 118 | return debugString 119 | } 120 | 121 | func (node *Node) Etag() string { 122 | etag, etagFound := node.graph.GetObject(node.subject, "<"+rdf.ServerETagUri+">") 123 | if !etagFound { 124 | panic(fmt.Sprintf("No etag found for node %s", node.uri)) 125 | } 126 | return etag 127 | } 128 | 129 | func (node Node) HasTriple(predicate, object string) bool { 130 | return node.graph.HasTriple(node.subject, predicate, object) 131 | } 132 | 133 | func (node Node) Headers() map[string][]string { 134 | return node.headers 135 | } 136 | 137 | func (node Node) IsBasicContainer() bool { 138 | return node.isBasicContainer 139 | } 140 | 141 | func (node Node) IsDirectContainer() bool { 142 | return node.isDirectContainer 143 | } 144 | 145 | func (node Node) IsRdf() bool { 146 | return node.isRdf 147 | } 148 | 149 | func (node Node) Uri() string { 150 | return node.uri 151 | } 152 | 153 | func (node *Node) Patch(triples string) error { 154 | if !node.isRdf { 155 | return errors.New("Cannot PATCH non-RDF Source") 156 | } 157 | 158 | userGraph, err := rdf.StringToGraph(triples, node.subject) 159 | if err != nil { 160 | return err 161 | } 162 | 163 | if hasServerManagedProperties(userGraph, node.subject) { 164 | return ServerManagedPropertyError 165 | } 166 | 167 | // This is pretty useless as-is since it does not allow to update 168 | // a triple. It always adds triples. 169 | node.graph.Append(userGraph) 170 | return node.save(node.graph, nil) 171 | } 172 | 173 | func (node Node) Path() string { 174 | return util.PathFromUri(node.rootUri, node.uri) 175 | } 176 | 177 | func (node Node) String() string { 178 | return node.uri 179 | } 180 | 181 | func (node *Node) appendTriple(predicate, object string) { 182 | node.graph.AppendTripleStr(node.subject, predicate, object) 183 | } 184 | 185 | func (node *Node) setETag() { 186 | node.graph.SetObject(node.subject, etagPredicate, calculateEtag()) 187 | } 188 | 189 | func (node *Node) Delete() error { 190 | return node.store.Delete() 191 | } 192 | 193 | func (node *Node) RemoveContainsUri(uri string) error { 194 | predicate := "<" + rdf.LdpContainsUri + ">" 195 | object := uri 196 | deleted := node.graph.DeleteTriple(node.subject, predicate, object) 197 | if !deleted { 198 | return errors.New("Failed to deleted the containment triple") 199 | } 200 | return node.save(node.graph, nil) 201 | } 202 | 203 | func getNode(settings Settings, path string) (Node, error) { 204 | return GetNode(settings, path, PreferTriples{}) 205 | } 206 | 207 | func GetNode(settings Settings, path string, pref PreferTriples) (Node, error) { 208 | node := newNode(settings, path) 209 | err := node.loadNode(true) 210 | 211 | if pref.Membership && node.IsDirectContainer() { 212 | // Fetch the triples from the membershipResource 213 | log.Printf("Fetching membershipResource's graph: %s", node.membershipResourcePath()) 214 | memberNode, err := getNode(settings, node.membershipResourcePath()) 215 | if err != nil { 216 | return node, err 217 | } 218 | node.graphExtra = memberNode.graph 219 | // TODO: set node.headers["Preference-Applied"] = "return=representation" 220 | } 221 | 222 | return node, err 223 | } 224 | 225 | func GetHead(settings Settings, path string) (Node, error) { 226 | node := newNode(settings, path) 227 | err := node.loadNode(false) 228 | return node, err 229 | } 230 | 231 | func NewRdfNode(settings Settings, triples string, path string) (Node, error) { 232 | node := newNode(settings, path) 233 | node.isRdf = true 234 | graph, err := rdf.StringToGraph(triples, node.subject) 235 | if err != nil { 236 | return Node{}, err 237 | } 238 | return node, node.save(graph, nil) 239 | } 240 | 241 | func NewNonRdfNode(settings Settings, reader io.ReadCloser, path, triples string) (Node, error) { 242 | node := newNode(settings, path) 243 | node.isRdf = false 244 | graph, err := rdf.StringToGraph(triples, node.subject) 245 | if err != nil { 246 | return Node{}, err 247 | } 248 | return node, node.save(graph, reader) 249 | } 250 | 251 | func ReplaceNonRdfNode(settings Settings, reader io.ReadCloser, path, etag, triples string) (Node, error) { 252 | node, err := GetHead(settings, path) 253 | if err != nil { 254 | return Node{}, err 255 | } 256 | 257 | if node.isRdf { 258 | return Node{}, errors.New("Cannot replace RDF source with a Non-RDF source") 259 | } 260 | 261 | if etag == "" { 262 | return Node{}, EtagMissingError 263 | } 264 | 265 | if node.Etag() != etag { 266 | // log.Printf("Cannot replace RDF source. Etag mismatch. Expected: %s. Found: %s", node.Etag(), etag) 267 | return Node{}, EtagMismatchError 268 | } 269 | 270 | var graph rdf.RdfGraph 271 | if triples != "" { 272 | graph, err = rdf.StringToGraph(triples, node.subject) 273 | if err != nil { 274 | return Node{}, err 275 | } 276 | } 277 | return node, node.save(graph, reader) 278 | } 279 | 280 | func ReplaceRdfNode(settings Settings, triples string, path string, etag string) (Node, error) { 281 | node, err := getNode(settings, path) 282 | if err != nil { 283 | return Node{}, err 284 | } 285 | 286 | if !node.isRdf { 287 | return Node{}, errors.New("Cannot replace non-RDF source with an RDF source") 288 | } 289 | 290 | if etag == "" { 291 | return Node{}, EtagMissingError 292 | } 293 | 294 | if node.Etag() != etag { 295 | // log.Printf("Cannot replace RDF source. Etag mismatch. Expected: %s. Found: %s", node.Etag(), etag) 296 | return Node{}, EtagMismatchError 297 | } 298 | 299 | graph, err := rdf.StringToGraph(triples, node.subject) 300 | if err != nil { 301 | return Node{}, err 302 | } 303 | 304 | if hasServerManagedProperties(graph, node.subject) { 305 | return Node{}, ServerManagedPropertyError 306 | } 307 | 308 | return node, node.save(graph, nil) 309 | } 310 | 311 | func (node Node) addDirectContainerChild(child Node) error { 312 | // TODO: account for isMemberOfRelation 313 | targetUri := util.RemoveAngleBrackets(node.membershipResource) 314 | targetPath := util.PathFromUri(node.rootUri, targetUri) 315 | 316 | targetNode, err := getNode(node.settings, targetPath) 317 | if err != nil { 318 | log.Printf("Could not find target node %s.", targetPath) 319 | return err 320 | } 321 | 322 | tripleForTarget := rdf.NewTriple("<"+targetNode.uri+">", node.hasMemberRelation, "<"+child.uri+">") 323 | 324 | err = targetNode.store.AppendToMetaFile(tripleForTarget.StringLn()) 325 | if err != nil { 326 | log.Printf("Error appending child %s to %s. %s", child.uri, targetNode.uri, err) 327 | return err 328 | } 329 | return nil 330 | } 331 | 332 | func (node *Node) loadNode(isIncludeBody bool) error { 333 | err := node.loadMeta() 334 | if err != nil { 335 | return err 336 | } 337 | 338 | if node.isRdf || isIncludeBody == false { 339 | return nil 340 | } 341 | 342 | return node.loadBinary() 343 | } 344 | 345 | func (node *Node) loadBinary() error { 346 | var err error 347 | node.binary, err = node.store.ReadDataFile() 348 | return err 349 | } 350 | 351 | func (node *Node) loadMeta() error { 352 | if !node.store.Exists() { 353 | return NodeNotFoundError 354 | } 355 | 356 | meta, err := node.store.ReadMetaFile() 357 | if err != nil { 358 | return err 359 | } 360 | 361 | node.graph, err = rdf.StringToGraph(meta, node.subject) 362 | if err != nil { 363 | return err 364 | } 365 | 366 | if node.graph.IsRdfSource(node.subject) { 367 | node.isRdf = true 368 | node.setAsRdf() 369 | } else { 370 | node.isRdf = false 371 | node.setAsNonRdf() 372 | } 373 | return nil 374 | } 375 | 376 | func (node *Node) save(graph rdf.RdfGraph, reader io.ReadCloser) error { 377 | node.graph = graph 378 | 379 | if node.graph.IsDirectContainer() { 380 | // TODO: we might need a different triple for DCs using isMemberOfRelation 381 | node.appendTriple("<"+rdf.LdpInsertedContentRelationUri+">", "<"+rdf.LdpMemberSubjectUri+">") 382 | } 383 | 384 | node.setETag() 385 | node.appendTriple(rdfTypePredicate, "<"+rdf.LdpResourceUri+">") 386 | if node.isRdf { 387 | node.appendTriple(rdfTypePredicate, "<"+rdf.LdpRdfSourceUri+">") 388 | node.appendTriple(rdfTypePredicate, "<"+rdf.LdpContainerUri+">") 389 | node.appendTriple(rdfTypePredicate, "<"+rdf.LdpBasicContainerUri+">") 390 | node.setAsRdf() 391 | } else { 392 | node.appendTriple(rdfTypePredicate, "<"+rdf.LdpNonRdfSourceUri+">") 393 | node.setAsNonRdf() 394 | } 395 | return node.writeToDisk(reader) 396 | } 397 | 398 | func (node *Node) writeToDisk(reader io.ReadCloser) error { 399 | // Write the RDF metadata 400 | err := node.store.SaveMetaFile(node.graph.String()) 401 | if err != nil { 402 | return err 403 | } 404 | 405 | if node.isRdf { 406 | return nil 407 | } 408 | 409 | // Write the binary... 410 | err = node.store.SaveDataFile(reader) 411 | if err != nil { 412 | return err 413 | } 414 | 415 | // ...update the copy in memory (this would get 416 | // tricky when we switch "node.binary" to a 417 | // reader. 418 | node.binary, err = node.store.ReadDataFile() 419 | return err 420 | } 421 | 422 | func (node *Node) setAsRdf() { 423 | node.headers = make(map[string][]string) 424 | node.headers["Content-Type"] = []string{node.contentType()} 425 | 426 | if node.graph.IsBasicContainer(node.subject) { 427 | node.headers["Allow"] = []string{"GET, HEAD, POST, PUT, PATCH"} 428 | } else { 429 | node.headers["Allow"] = []string{"GET, HEAD, PUT, PATCH"} 430 | } 431 | node.headers["Accept-Post"] = []string{rdf.TurtleContentType} 432 | node.headers["Accept-Patch"] = []string{rdf.TurtleContentType} 433 | 434 | node.headers["Etag"] = []string{node.Etag()} 435 | 436 | links := make([]string, 0) 437 | links = append(links, rdf.LdpResourceLink) 438 | if node.graph.IsBasicContainer(node.subject) { 439 | node.isBasicContainer = true 440 | links = append(links, rdf.LdpContainerLink) 441 | links = append(links, rdf.LdpBasicContainerLink) 442 | // TODO: validate membershipResource is a sub-URI of rootURI 443 | node.membershipResource, node.hasMemberRelation, node.isDirectContainer = node.graph.GetDirectContainerInfo() 444 | if node.isDirectContainer { 445 | links = append(links, rdf.LdpDirectContainerLink) 446 | } 447 | } 448 | node.headers["Link"] = links 449 | } 450 | 451 | func (node *Node) setAsNonRdf() { 452 | // TODO Figure out a way to pass the binary as a stream 453 | node.binary = "" 454 | node.headers = make(map[string][]string) 455 | 456 | describedByLink := fmt.Sprintf("<%s?metadata=yes>; rel=\"describedby\"; anchor=\"%s\"", node.uri, node.uri) 457 | node.headers["Link"] = []string{describedByLink, rdf.LdpResourceLink, rdf.LdpNonRdfSourceLink} 458 | 459 | node.headers["Allow"] = []string{"GET, HEAD, PUT"} 460 | node.headers["Content-Type"] = []string{node.contentType()} 461 | node.headers["Etag"] = []string{node.Etag()} 462 | } 463 | 464 | func (node *Node) membershipResourcePath() string { 465 | uri := util.RemoveAngleBrackets(node.membershipResource) 466 | return strings.Replace(uri, node.settings.rootUri, "", 1) 467 | } 468 | 469 | func hasServerManagedProperties(graph rdf.RdfGraph, subject string) bool { 470 | // TODO: What other server-managed properties should we handle? 471 | properties := []string{rdf.LdpResourceUri, rdf.LdpRdfSourceUri, rdf.LdpNonRdfSourceUri, 472 | rdf.LdpContainerUri, rdf.LdpBasicContainerUri, rdf.LdpDirectContainerUri, rdf.LdpContainsUri, 473 | rdf.LdpConstrainedBy} 474 | 475 | for _, property := range properties { 476 | if graph.HasPredicate(subject, "<"+property+">") { 477 | return true 478 | } 479 | } 480 | return false 481 | } 482 | 483 | func calculateEtag() string { 484 | // TODO: Come up with a more precise value. 485 | now := time.Now().Format(time.RFC3339) 486 | etag := strings.Replace(now, ":", "_", -1) 487 | return "\"" + etag + "\"" 488 | } 489 | 490 | func newNode(settings Settings, path string) Node { 491 | if strings.HasPrefix(path, "http://") { 492 | panic("newNode expects a path, received a URI: " + path) 493 | } 494 | var node Node 495 | node.settings = settings 496 | pathOnDisk := util.PathConcat(settings.dataPath, path) 497 | node.store = textstore.NewStore(pathOnDisk) 498 | node.rootUri = settings.RootUri() 499 | node.uri = util.UriConcat(node.rootUri, path) 500 | node.subject = "<" + node.uri + ">" 501 | return node 502 | } 503 | -------------------------------------------------------------------------------- /ldp/settings.go: -------------------------------------------------------------------------------- 1 | package ldp 2 | 3 | import "ldpserver/util" 4 | 5 | type Settings struct { 6 | dataPath string 7 | rootUri string 8 | idFile string 9 | } 10 | 11 | func SettingsNew(rootUri, datapath string) Settings { 12 | var sett Settings 13 | sett.rootUri = util.StripSlash(rootUri) 14 | sett.dataPath = util.PathConcat(datapath, "/") 15 | sett.idFile = util.PathConcat(sett.dataPath, "meta.rdf.id") 16 | return sett 17 | } 18 | 19 | func (settings Settings) DataPath() string { 20 | return settings.dataPath 21 | } 22 | 23 | func (settings Settings) RootUri() string { 24 | return settings.rootUri 25 | } 26 | 27 | func (settings Settings) IdFile() string { 28 | return settings.idFile 29 | } 30 | -------------------------------------------------------------------------------- /main.go: -------------------------------------------------------------------------------- 1 | package main 2 | 3 | import ( 4 | "flag" 5 | "ldpserver/web" 6 | "os" 7 | "path/filepath" 8 | ) 9 | 10 | func main() { 11 | rootFolder, err := filepath.Abs(filepath.Dir(os.Args[0]) + "/data") 12 | if err != nil { 13 | panic("Could not determine root folder") 14 | } 15 | 16 | var address = flag.String("address", "localhost:9001", "Address where server will listen for connections") 17 | var dataPath = flag.String("data", rootFolder, "Path where data will be saved") 18 | flag.Parse() 19 | 20 | web.Start(*address, *dataPath) 21 | } 22 | -------------------------------------------------------------------------------- /rdf/ast.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | type SubjectNode struct { 4 | value string 5 | predicates []*PredicateNode 6 | } 7 | 8 | type PredicateNode struct { 9 | value string 10 | objects []string 11 | } 12 | 13 | func NewSubjectNode(value string) SubjectNode { 14 | return SubjectNode{value: value} 15 | } 16 | 17 | func NewPredicateNode(value string) PredicateNode { 18 | return PredicateNode{value: value} 19 | } 20 | 21 | func (subject *SubjectNode) AddPredicate(value string) *PredicateNode { 22 | predicate := PredicateNode{value: value} 23 | subject.predicates = append(subject.predicates, &predicate) 24 | return &predicate 25 | } 26 | 27 | func (predicate *PredicateNode) AddObject(object string) { 28 | predicate.objects = append(predicate.objects, object) 29 | } 30 | 31 | func (subject *SubjectNode) Render() string { 32 | triples := "" 33 | for _, predicate := range subject.predicates { 34 | for _, object := range predicate.objects { 35 | triples += subject.value + " " + predicate.value + " " + object + " .\n" 36 | } 37 | } 38 | return triples 39 | } 40 | 41 | func (subject *SubjectNode) RenderTriples() []Triple { 42 | triples := []Triple{} 43 | for _, predicate := range subject.predicates { 44 | for _, object := range predicate.objects { 45 | triple := NewTriple(subject.value, predicate.value, object) 46 | triples = append(triples, triple) 47 | } 48 | } 49 | return triples 50 | } 51 | -------------------------------------------------------------------------------- /rdf/ast_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "testing" 4 | 5 | func TestTree(t *testing.T) { 6 | subject := NewSubjectNode("") 7 | predicate := subject.AddPredicate("

") 8 | predicate.AddObject("") 9 | predicate.AddObject("") 10 | predicate2 := subject.AddPredicate("") 11 | predicate2.AddObject("") 12 | text := subject.Render() 13 | if text != "

.\n

.\n .\n" { 14 | t.Errorf("Render gave unexpected value\n%s", text) 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /rdf/graph.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "log" 4 | 5 | type RdfGraph []Triple 6 | 7 | func (triples RdfGraph) String() string { 8 | theString := "" 9 | for _, triple := range triples { 10 | theString += triple.StringLn() 11 | } 12 | return theString 13 | } 14 | 15 | func StringToGraph(theString, rootUri string) (RdfGraph, error) { 16 | var err error 17 | var graph RdfGraph 18 | if len(theString) > 0 { 19 | parser := NewTurtleParser(theString) 20 | err = parser.Parse() 21 | if err == nil { 22 | for _, triple := range parser.Triples() { 23 | triple.ReplaceBlankUri(rootUri) 24 | graph = append(graph, triple) 25 | } 26 | } 27 | } 28 | return graph, err 29 | } 30 | 31 | func (graph RdfGraph) IsRdfSource(subject string) bool { 32 | return graph.HasTriple(subject, "a", "<"+LdpRdfSourceUri+">") 33 | } 34 | 35 | func (graph RdfGraph) IsBasicContainer(subject string) bool { 36 | return graph.HasTriple(subject, "a", "<"+LdpBasicContainerUri+">") 37 | } 38 | 39 | func (graph RdfGraph) IsDirectContainer() bool { 40 | _, _, isDirectContainer := graph.GetDirectContainerInfo() 41 | return isDirectContainer 42 | } 43 | 44 | func (graph RdfGraph) GetDirectContainerInfo() (string, string, bool) { 45 | // TODO: validate only one instance of each these predicates is found on the graph 46 | // (perhas the validation should only be when adding/updating triples) 47 | membershipResource := "" 48 | hasMemberRelation := "" 49 | for _, triple := range graph { 50 | switch triple.predicate { 51 | case "<" + LdpMembershipResource + ">": 52 | membershipResource = triple.object 53 | case "<" + LdpHasMemberRelation + ">": 54 | hasMemberRelation = triple.object 55 | } 56 | if membershipResource != "" && hasMemberRelation != "" { 57 | return membershipResource, hasMemberRelation, true 58 | } 59 | } 60 | return "", "", false 61 | } 62 | 63 | func (graph RdfGraph) HasPredicate(subject, predicate string) bool { 64 | _, found := graph.FindPredicate(subject, predicate) 65 | return found 66 | } 67 | 68 | func (graph *RdfGraph) FindPredicate(subject, predicate string) (*Triple, bool) { 69 | return graph.findPredicate(subject, predicate, true) 70 | } 71 | 72 | func (graph *RdfGraph) findPredicate(subject, predicate string, recurr bool) (*Triple, bool) { 73 | for i, triple := range *graph { 74 | if triple.subject == subject && triple.predicate == predicate { 75 | // return a reference to the original triple 76 | return &(*graph)[i], true 77 | } 78 | } 79 | if recurr { 80 | // "a" is an alias for RdfType 81 | // look to see if we can find it by alias 82 | switch { 83 | case predicate == "a": 84 | return graph.findPredicate(subject, "<"+RdfTypeUri+">", false) 85 | case predicate == "<"+RdfTypeUri+">": 86 | return graph.findPredicate(subject, "a", false) 87 | } 88 | } 89 | return nil, false 90 | } 91 | 92 | func (graph *RdfGraph) findTriple(subject, predicate, object string, recurr bool) (*Triple, bool) { 93 | for i, t := range *graph { 94 | if t.subject == subject && t.predicate == predicate && t.object == object { 95 | // return a reference to the original triple 96 | return &(*graph)[i], true 97 | } 98 | } 99 | // "a" is an alias for RdfType 100 | // look to see if we can find it by alias 101 | if recurr { 102 | switch { 103 | case predicate == "a": 104 | return graph.findTriple(subject, "<"+RdfTypeUri+">", object, false) 105 | case predicate == "<"+RdfTypeUri+">": 106 | return graph.findTriple(subject, "a", object, false) 107 | } 108 | } 109 | return nil, false 110 | } 111 | 112 | func (graph *RdfGraph) DeleteTriple(subject, predicate, object string) bool { 113 | var newGraph RdfGraph 114 | deleted := false 115 | for _, triple := range *graph { 116 | // This does not handle the predicate a vs RdfType like find does. Should it? 117 | if triple.subject == subject && triple.predicate == predicate && triple.object == object { 118 | // don't add it to the new graph 119 | deleted = true 120 | } else { 121 | newGraph = append(newGraph, triple) 122 | } 123 | } 124 | 125 | if deleted { 126 | *graph = newGraph 127 | } 128 | return deleted 129 | } 130 | 131 | func (graph *RdfGraph) appendTriple(subject, predicate, object string) bool { 132 | t, found := graph.findTriple(subject, predicate, object, true) 133 | if found { 134 | if t.predicate == "a" && predicate != "a" { 135 | t.predicate = predicate 136 | log.Printf("**> replaced a with %s", predicate) 137 | } 138 | // nothing to do 139 | return false 140 | } 141 | 142 | // Append the new triple 143 | newTriple := NewTriple(subject, predicate, object) 144 | *graph = append(*graph, newTriple) 145 | return true 146 | } 147 | 148 | func (graph *RdfGraph) Append(newGraph RdfGraph) { 149 | for _, triple := range newGraph { 150 | graph.AppendTriple(triple) 151 | } 152 | } 153 | 154 | func (graph *RdfGraph) AppendTriple(t Triple) bool { 155 | return graph.appendTriple(t.subject, t.predicate, t.object) 156 | } 157 | 158 | func (graph *RdfGraph) AppendTripleStr(subject, predicate, object string) bool { 159 | return graph.appendTriple(subject, predicate, object) 160 | } 161 | 162 | func (graph RdfGraph) HasTriple(subject, predicate, object string) bool { 163 | _, found := graph.findTriple(subject, predicate, object, true) 164 | return found 165 | } 166 | 167 | func (graph RdfGraph) GetObject(subject, predicate string) (string, bool) { 168 | triple, found := graph.FindPredicate(subject, predicate) 169 | if found { 170 | return triple.object, true 171 | } 172 | return "", false 173 | } 174 | 175 | // Set the object for a subject/predicate 176 | // This is only useful for subject/predicates that can appear only once 177 | // on the graph. If a subject/predicate can appear multiple times, this 178 | // method will find and overwrite the first instance only. 179 | func (graph *RdfGraph) SetObject(subject, predicate, object string) { 180 | triple, found := graph.FindPredicate(subject, predicate) 181 | if found { 182 | triple.object = object 183 | return 184 | } 185 | 186 | // Add a new triple to the graph with the subject/predicate/object 187 | newTriple := NewTriple(subject, predicate, object) 188 | newGraph := RdfGraph{newTriple} 189 | graph.Append(newGraph) 190 | } 191 | -------------------------------------------------------------------------------- /rdf/graph_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "testing" 4 | import "fmt" 5 | 6 | func TestGraphToString(t *testing.T) { 7 | triple1 := Triple{subject: "", predicate: "", object: ""} 8 | triple2 := Triple{subject: "", predicate: "", object: ""} 9 | var graph RdfGraph 10 | graph = append(graph, triple1, triple2) 11 | str := fmt.Sprintf("%s", graph) 12 | if str != " .\n .\n" { 13 | t.Errorf("Graph to string failed: %s", str) 14 | } 15 | } 16 | 17 | func TestStringToGraph(t *testing.T) { 18 | var graph RdfGraph 19 | var err error 20 | triple1 := " .\n" 21 | triple2 := " .\n" 22 | graph, err = StringToGraph(triple1+triple2, "") 23 | if err != nil || len(graph) != 2 { 24 | t.Errorf("Unexpected number of triples found: %d %s", len(graph), err) 25 | } 26 | 27 | graph, err = StringToGraph("\n"+triple1+"\n"+triple2+"\n", "") 28 | if err != nil || len(graph) != 2 { 29 | t.Errorf("Failed to remove empty lines %d %s", len(graph), err) 30 | } 31 | } 32 | 33 | func TestAppend(t *testing.T) { 34 | var graph2 RdfGraph 35 | t1 := Triple{subject: "s", predicate: "p", object: "o"} 36 | graph1 := RdfGraph{t1} 37 | for _, x := range graph1 { 38 | graph2 = append(graph2, x) 39 | } 40 | 41 | if len(graph2) != 1 { 42 | t.Errorf("Graph not appended: [%s]", graph2) 43 | } 44 | } 45 | 46 | func TestHasTriple(t *testing.T) { 47 | triple := Triple{subject: "s", predicate: "p", object: "o"} 48 | graph := RdfGraph{triple} 49 | 50 | if !graph.HasTriple("s", "p", "o") { 51 | t.Errorf("HasTriple test failed for graph [%s]", graph) 52 | } 53 | 54 | if graph.HasTriple("s", "x", "o") { 55 | t.Errorf("HasTriple test failed for graph [%s]", graph) 56 | } 57 | } 58 | 59 | func TestFindPredicate(t *testing.T) { 60 | triple := Triple{subject: "s", predicate: "a", object: "something"} 61 | graph := RdfGraph{triple, triple} 62 | 63 | if _, found := graph.FindPredicate("s", "a"); !found { 64 | t.Errorf("FindPredicate test failed for valid triple") 65 | } 66 | 67 | if _, found := graph.FindPredicate("s", "b"); found { 68 | t.Errorf("FindPredicate test failed for invalid triple") 69 | } 70 | } 71 | 72 | func TestFindPredicateAliasA(t *testing.T) { 73 | triple := Triple{subject: "s", predicate: "a", object: "something"} 74 | graph := RdfGraph{triple, triple} 75 | 76 | if _, found := graph.FindPredicate("s", "<"+RdfTypeUri+">"); !found { 77 | t.Errorf("FindPredicate test failed when using rdf type in fullname") 78 | } 79 | } 80 | 81 | func TestFindPredicateAliasRdfType(t *testing.T) { 82 | triple := Triple{subject: "s", predicate: "<" + RdfTypeUri + ">", object: "something"} 83 | graph := RdfGraph{triple, triple} 84 | 85 | if _, found := graph.FindPredicate("s", "a"); !found { 86 | t.Errorf("FindPredicate test failed when using 'a' rather than rdf type fullname") 87 | } 88 | } 89 | 90 | func TestSetObject(t *testing.T) { 91 | triple := Triple{subject: "s", predicate: "p", object: "o"} 92 | graph := RdfGraph{triple} 93 | 94 | graph.SetObject("s", "p", "o2") 95 | if graph.HasTriple("s", "p", "o") { 96 | t.Errorf("SetObject found the original triple (after it was replaced)") 97 | } 98 | 99 | if !graph.HasTriple("s", "p", "o2") { 100 | t.Errorf("SetObject did not find triple with new value") 101 | } 102 | 103 | graph.SetObject("s", "p2", "o3") 104 | if !graph.HasTriple("s", "p2", "o3") { 105 | t.Errorf("SetObject did not find new triple") 106 | } 107 | 108 | if !graph.HasTriple("s", "p", "o2") { 109 | t.Errorf("SetObject did not triple with new value (after adding new triple)") 110 | } 111 | } 112 | 113 | func TestDeleteTriple(t *testing.T) { 114 | t1 := Triple{subject: "s1", predicate: "p1", object: "o1"} 115 | t2 := Triple{subject: "s2", predicate: "p2", object: "o2"} 116 | t3 := Triple{subject: "s3", predicate: "p3", object: "o3"} 117 | graph := RdfGraph{t1, t2, t3} 118 | 119 | deleted := graph.DeleteTriple("s2", "p2", "o2") 120 | if !deleted { 121 | t.Errorf("Did not delete triple from graph") 122 | } 123 | 124 | if graph.HasTriple("s2", "p2", "o2") { 125 | t.Errorf("Deleted triple found in graph") 126 | } 127 | 128 | deleted = graph.DeleteTriple("s2", "p2", "o2") 129 | if deleted { 130 | t.Errorf("Delete triple deleted a non-existing triple") 131 | } 132 | } 133 | -------------------------------------------------------------------------------- /rdf/scanner.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "fmt" 4 | 5 | type Scanner struct { 6 | index int 7 | text string 8 | chars []rune 9 | length int 10 | row int 11 | col int 12 | } 13 | 14 | func NewScanner(text string) Scanner { 15 | // Convert the original string to an array of unicode runes. 16 | // This allows us to iterate on it as if it was an array 17 | // of ASCII chars even if there are Unicode characters on it 18 | // that use 2-4 bytes. 19 | chars := stringToRunes(text) 20 | scanner := Scanner{text: text, chars: chars, length: len(chars), row: 1, col: 1} 21 | return scanner 22 | } 23 | 24 | func (scanner Scanner) Index() int { 25 | return scanner.index 26 | } 27 | 28 | func (scanner Scanner) Substring(start, end int) string { 29 | return string(scanner.chars[start:end]) 30 | } 31 | 32 | func (scanner Scanner) SubstringFrom(start int) string { 33 | return string(scanner.chars[start:scanner.index]) 34 | } 35 | 36 | // Advances the index to the next character. 37 | func (scanner *Scanner) Advance() { 38 | if scanner.CanRead() { 39 | scanner.index++ 40 | if scanner.CanRead() && scanner.Char() == '\n' { 41 | scanner.row++ 42 | scanner.col = 1 43 | } else { 44 | scanner.col++ 45 | } 46 | } 47 | } 48 | 49 | func (scanner *Scanner) CanRead() bool { 50 | if scanner.length == 0 { 51 | return false 52 | } 53 | return scanner.index < scanner.length 54 | } 55 | 56 | func (scanner Scanner) Char() rune { 57 | return scanner.chars[scanner.index] 58 | } 59 | 60 | func (scanner Scanner) CharString() string { 61 | return string(scanner.chars[scanner.index]) 62 | } 63 | 64 | func (scanner *Scanner) Peek() (bool, rune) { 65 | if scanner.length > 0 && scanner.index < (scanner.length-1) { 66 | return true, scanner.chars[scanner.index+1] 67 | } 68 | return false, 0 69 | } 70 | 71 | func (scanner Scanner) Col() int { 72 | return scanner.col 73 | } 74 | 75 | func (scanner Scanner) Row() int { 76 | return scanner.row 77 | } 78 | 79 | func (scanner Scanner) Position() string { 80 | return fmt.Sprintf("(%d, %d)", scanner.row, scanner.col) 81 | } 82 | 83 | func stringToRunes(text string) []rune { 84 | var chars []rune 85 | for _, c := range text { 86 | chars = append(chars, c) 87 | } 88 | return chars 89 | } 90 | -------------------------------------------------------------------------------- /rdf/scanner_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "testing" 4 | 5 | func TestPeek(t *testing.T) { 6 | scanner := NewScanner("abc") 7 | if _, nextChar := scanner.Peek(); nextChar != 'b' { 8 | t.Errorf("Error on first peek") 9 | } 10 | scanner.Advance() 11 | if _, nextChar := scanner.Peek(); nextChar != 'c' { 12 | t.Errorf("Error on second peek") 13 | } 14 | scanner.Advance() 15 | if canPeek, _ := scanner.Peek(); canPeek == true { 16 | t.Errorf("Failed to detect that it cannot peek anymore") 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /rdf/tokenizer.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | // "log" 7 | ) 8 | 9 | type Tokenizer struct { 10 | scanner Scanner 11 | } 12 | 13 | func NewTokenizer(text string) Tokenizer { 14 | return Tokenizer{scanner: NewScanner(text)} 15 | } 16 | 17 | func (tokenizer *Tokenizer) GetNextToken() (string, error) { 18 | var err error 19 | var value string 20 | 21 | tokenizer.AdvanceWhiteSpace() 22 | tokenizer.AdvanceComments() 23 | if !tokenizer.scanner.CanRead() { 24 | return "", nil 25 | } 26 | 27 | firstChar := tokenizer.scanner.Char() 28 | switch { 29 | case firstChar == '.': 30 | value = "." 31 | case firstChar == ',': 32 | value = "," 33 | case firstChar == ';': 34 | value = ";" 35 | case firstChar == '@': 36 | value, err = tokenizer.parseDirective() 37 | case firstChar == '<': 38 | value, err = tokenizer.parseUri() 39 | case firstChar == '"': 40 | value, err = tokenizer.parseString() 41 | case tokenizer.isNamespacedChar(): 42 | value = tokenizer.parseNamespacedValue() 43 | default: 44 | return "", tokenizer.Error("Invalid first character") 45 | } 46 | 47 | if err != nil { 48 | return "", err 49 | } 50 | 51 | tokenizer.scanner.Advance() 52 | return value, nil 53 | } 54 | 55 | // Advances the index to the beginning of the next triple. 56 | func (tokenizer *Tokenizer) AdvanceTriple() error { 57 | for tokenizer.CanRead() { 58 | if tokenizer.scanner.Char() == '.' { 59 | break 60 | } 61 | if tokenizer.isWhiteSpaceChar() { 62 | tokenizer.scanner.Advance() 63 | continue 64 | } 65 | return tokenizer.Error("Triple did not end with a period.") 66 | } 67 | tokenizer.scanner.Advance() 68 | return nil 69 | } 70 | 71 | func (tokenizer *Tokenizer) CanRead() bool { 72 | return tokenizer.scanner.CanRead() 73 | } 74 | 75 | func (tokenizer *Tokenizer) AdvanceWhiteSpace() { 76 | for tokenizer.CanRead() { 77 | if !tokenizer.isWhiteSpaceChar() { 78 | break 79 | } 80 | tokenizer.scanner.Advance() 81 | } 82 | } 83 | 84 | func (tokenizer *Tokenizer) AdvanceComments() { 85 | if !tokenizer.CanRead() || tokenizer.scanner.Char() != '#' { 86 | return 87 | } 88 | 89 | for tokenizer.CanRead() { 90 | if tokenizer.scanner.Char() == '\n' { 91 | tokenizer.AdvanceWhiteSpace() 92 | if !tokenizer.CanRead() || tokenizer.scanner.Char() != '#' { 93 | break 94 | } 95 | } 96 | tokenizer.scanner.Advance() 97 | } 98 | } 99 | 100 | func (tokenizer Tokenizer) isLanguageChar() bool { 101 | char := tokenizer.scanner.Char() 102 | return (char >= 'a' && char <= 'z') || 103 | (char >= 'A' && char <= 'Z') || 104 | (char == '-') 105 | } 106 | 107 | func (tokenizer Tokenizer) isDirectiveChar() bool { 108 | char := tokenizer.scanner.Char() 109 | return (char >= 'a' && char <= 'z') || 110 | (char >= 'A' && char <= 'Z') || 111 | (char == '@') 112 | } 113 | 114 | func (tokenizer Tokenizer) isNamespacedChar() bool { 115 | char := tokenizer.scanner.Char() 116 | return (char >= 'a' && char <= 'z') || 117 | (char >= 'A' && char <= 'Z') || 118 | (char >= '0' && char <= '9') || 119 | (char == ':') || 120 | (char == '_') 121 | } 122 | 123 | func (tokenizer Tokenizer) isUriChar() bool { 124 | char := tokenizer.scanner.Char() 125 | return (char >= 'a' && char <= 'z') || 126 | (char >= 'A' && char <= 'Z') || 127 | (char >= '0' && char <= '9') || 128 | (char == ':') || (char == '/') || 129 | (char == '%') || (char == '#') || 130 | (char == '+') || (char == '-') || 131 | (char == '.') || (char == '_') 132 | } 133 | 134 | func (tokenizer Tokenizer) isWhiteSpaceChar() bool { 135 | char := tokenizer.scanner.Char() 136 | return char == ' ' || char == '\t' || char == '\n' || char == '\r' 137 | } 138 | 139 | // Extracts a value in the form xx:yy or xx 140 | func (tokenizer *Tokenizer) parseNamespacedValue() string { 141 | start := tokenizer.scanner.Index() 142 | tokenizer.scanner.Advance() 143 | for tokenizer.CanRead() { 144 | if tokenizer.isNamespacedChar() { 145 | tokenizer.scanner.Advance() 146 | continue 147 | } else { 148 | break 149 | } 150 | } 151 | return tokenizer.scanner.SubstringFrom(start) 152 | } 153 | 154 | func (tokenizer *Tokenizer) parseLanguage() string { 155 | start := tokenizer.scanner.Index() 156 | tokenizer.scanner.Advance() 157 | for tokenizer.CanRead() { 158 | if tokenizer.isLanguageChar() { 159 | tokenizer.scanner.Advance() 160 | } else { 161 | break 162 | } 163 | } 164 | // Should be indicate error if the language is empty? 165 | return tokenizer.scanner.SubstringFrom(start) 166 | } 167 | 168 | // Extracts a value in the form @hello 169 | func (tokenizer *Tokenizer) parseDirective() (string, error) { 170 | start := tokenizer.scanner.Index() 171 | tokenizer.scanner.Advance() 172 | for tokenizer.CanRead() { 173 | if tokenizer.isDirectiveChar() { 174 | tokenizer.scanner.Advance() 175 | } else { 176 | break 177 | } 178 | } 179 | 180 | directive := tokenizer.scanner.SubstringFrom(start) 181 | if directive == "" { 182 | return "", tokenizer.Error("Empty directive detected") 183 | } 184 | 185 | return directive, nil 186 | } 187 | 188 | // Extracts a value in quotes, for example 189 | // "hello" 190 | // "hello \"world\"" 191 | // "hello"@en-us 192 | // "hello"^^ 193 | func (tokenizer *Tokenizer) parseString() (string, error) { 194 | start := tokenizer.scanner.Index() 195 | lastChar := tokenizer.scanner.Char() 196 | tokenizer.scanner.Advance() 197 | for tokenizer.CanRead() { 198 | if tokenizer.scanner.Char() == '"' { 199 | if lastChar == '\\' { 200 | lastChar = tokenizer.scanner.Char() 201 | tokenizer.scanner.Advance() 202 | continue 203 | } 204 | str := tokenizer.scanner.Substring(start, tokenizer.scanner.Index()+1) 205 | lang := "" 206 | datatype := "" 207 | canPeek, nextChar := tokenizer.scanner.Peek() 208 | var err error 209 | if canPeek { 210 | switch nextChar { 211 | case '@': 212 | tokenizer.scanner.Advance() 213 | lang = tokenizer.parseLanguage() 214 | str += lang 215 | case '^': 216 | tokenizer.scanner.Advance() 217 | datatype, err = tokenizer.parseType() 218 | str += datatype 219 | } 220 | } 221 | return str, err 222 | } 223 | lastChar = tokenizer.scanner.Char() 224 | tokenizer.scanner.Advance() 225 | } 226 | return "", tokenizer.Error("String did not end with \"") 227 | } 228 | 229 | func (tokenizer *Tokenizer) parseType() (string, error) { 230 | canPeek, nextChar := tokenizer.scanner.Peek() 231 | if !canPeek || nextChar != '^' { 232 | return "", tokenizer.Error("Invalid type delimiter") 233 | } 234 | 235 | tokenizer.scanner.Advance() 236 | canPeek, nextChar = tokenizer.scanner.Peek() 237 | if !canPeek || nextChar != '<' { 238 | return "", tokenizer.Error("Invalid URI in type delimiter") 239 | } 240 | 241 | tokenizer.scanner.Advance() 242 | uri, err := tokenizer.parseUri() 243 | return "^^" + uri, err 244 | } 245 | 246 | // Extracts an URI in the form 247 | func (tokenizer *Tokenizer) parseUri() (string, error) { 248 | start := tokenizer.scanner.Index() 249 | tokenizer.scanner.Advance() 250 | for tokenizer.CanRead() { 251 | if tokenizer.scanner.Char() == '>' { 252 | uri := tokenizer.scanner.Substring(start, tokenizer.scanner.Index()+1) 253 | return uri, nil 254 | } 255 | if !tokenizer.isUriChar() { 256 | return "", tokenizer.Error("Invalid character in URI") 257 | } 258 | tokenizer.scanner.Advance() 259 | } 260 | return "", tokenizer.Error("URI did not end with >") 261 | } 262 | 263 | func (tokenizer *Tokenizer) Error(message string) error { 264 | lastChar := "" 265 | if tokenizer.CanRead() { 266 | lastChar = tokenizer.scanner.CharString() 267 | } 268 | errorMsg := fmt.Sprintf("%s. Character (%s) at %s.", message, lastChar, tokenizer.scanner.Position()) 269 | return errors.New(errorMsg) 270 | } 271 | -------------------------------------------------------------------------------- /rdf/tokenizer_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "testing" 4 | 5 | func TestGoodTokens(t *testing.T) { 6 | testA := []string{"", ""} 7 | testB := []string{" \t", ""} 8 | testC := []string{"", ""} 9 | testD := []string{"\"hello\"", "\"hello\""} 10 | testE := []string{"title+", "title"} // this is the correct test 11 | testF := []string{"title", "title"} 12 | testG := []string{"dc:title", "dc:title"} 13 | tests := [][]string{testA, testB, testC, testD, testE, testF, testG} 14 | for _, test := range tests { 15 | tokenizer := NewTokenizer(test[0]) 16 | token, err := tokenizer.GetNextToken() 17 | if err != nil { 18 | t.Errorf("Error parsing token: (%s). Error: %s.", test[0], err) 19 | } else if token != test[1] { 20 | t.Errorf("Token (%s) parsed incorrectly (%s)", test[1], token) 21 | } 22 | } 23 | } 24 | 25 | func TestBadTokens(t *testing.T) { 26 | tests := []string{"<", ">", "{", "}", `"aaa`} 27 | for _, test := range tests { 28 | tokenizer := NewTokenizer(test) 29 | token, err := tokenizer.GetNextToken() 30 | if err == nil { 31 | t.Errorf("Did not detect invalid token: (%s). Result: (%s)", test, token) 32 | } 33 | } 34 | } 35 | 36 | func TestGoodLanguage(t *testing.T) { 37 | test := "\"hello\"@en-us" 38 | tokenizer := NewTokenizer(test) 39 | token, err := tokenizer.GetNextToken() 40 | if err != nil { 41 | t.Errorf("Error parsing token with language: (%s). Error: %s.", test, err) 42 | } else if token != test { 43 | t.Errorf("Token with language (%s) parsed incorrectly (%s)", test, token) 44 | } 45 | } 46 | 47 | func TestBadLanguage(t *testing.T) { 48 | test := "\"hello\"@/en-us" 49 | tokenizer := NewTokenizer(test) 50 | token, err := tokenizer.GetNextToken() 51 | if err != nil { 52 | t.Errorf("Error parsing token with bad language: (%s). Error: %s.", test, err) 53 | } else if token != "\"hello\"@" { 54 | t.Errorf("Token with bad language (%s) parsed incorrectly (%s)", test, token) 55 | } 56 | } 57 | 58 | func TestGoodType(t *testing.T) { 59 | test := "\"hello\"^^" 60 | tokenizer := NewTokenizer(test) 61 | token, err := tokenizer.GetNextToken() 62 | if err != nil { 63 | t.Errorf("Error parsing token with type: (%s). Error: %s.", test, err) 64 | } else if token != test { 65 | t.Errorf("Token with type (%s) parsed incorrectly (%s)", test, token) 66 | } 67 | } 68 | 69 | func TestBadType(t *testing.T) { 70 | testA := "\"hello\"^" 71 | testB := "\"hello\"^^http://something>" 72 | testC := "\"hello\"^^" { 37 | triple.subject = blank 38 | } 39 | if triple.predicate == "<>" { 40 | triple.predicate = blank 41 | } 42 | if triple.object == "<>" { 43 | triple.object = blank 44 | } 45 | } 46 | 47 | func StringToTriples(text, blank string) ([]Triple, error) { 48 | var triples []Triple 49 | parser := NewTurtleParser(text) 50 | err := parser.Parse() 51 | if err != nil { 52 | return triples, err 53 | } 54 | for _, triple := range parser.Triples() { 55 | triple.ReplaceBlankUri(blank) 56 | } 57 | return triples, nil 58 | } 59 | -------------------------------------------------------------------------------- /rdf/triple_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "testing" 4 | import "fmt" 5 | 6 | func TestTripleToString(t *testing.T) { 7 | triple1 := NewTriple("", "

", "") 8 | str := fmt.Sprintf("%s", triple1) 9 | if str != "

." { 10 | t.Errorf("Triple to string failed: %s", str) 11 | } 12 | 13 | triple2 := NewTriple("", "

", `"o"`) 14 | str2 := fmt.Sprintf("%s", triple2) 15 | if str2 != `

"o" .` { 16 | t.Errorf("Triple to string failed: %s", str2) 17 | } 18 | } 19 | 20 | func TestStringToTriple(t *testing.T) { 21 | validTests := []string{` .`, ` "c" .`} 22 | for _, test := range validTests { 23 | _, err := StringToTriples(test, "") 24 | if err != nil { 25 | t.Errorf("Failed to parse valid triple %s. Err: %s", test, err) 26 | } 27 | } 28 | 29 | invalidTests := []string{` <3 \< 2> .`, ` <3 < 2> .\n`, ` <3 \> 2> .`} 30 | for _, test := range invalidTests { 31 | _, err := StringToTriples(test, "") 32 | if err == nil { 33 | t.Errorf("Failed to detect bad triple in %s.", test) 34 | } 35 | } 36 | } 37 | 38 | func TestReplaceBlank(t *testing.T) { 39 | testUri := "" 40 | triple := NewTriple("<>", "

", "") 41 | triple.ReplaceBlankUri(testUri) 42 | if triple.subject != testUri || triple.predicate != "

" || triple.object != "" { 43 | t.Error("Blank subject handled incorretly") 44 | } 45 | 46 | triple = NewTriple("", "<>", "") 47 | triple.ReplaceBlankUri(testUri) 48 | if triple.subject != "" || triple.predicate != testUri || triple.object != "" { 49 | t.Error("Blank predicate handled incorretly") 50 | } 51 | 52 | triple = NewTriple("", "

", "<>") 53 | triple.ReplaceBlankUri(testUri) 54 | if triple.subject != "" || triple.predicate != "

" || triple.object != testUri { 55 | t.Error("Blank object handled incorretly") 56 | } 57 | } 58 | -------------------------------------------------------------------------------- /rdf/turtle.go: -------------------------------------------------------------------------------- 1 | // A basic RDF Turtle parser 2 | // http://www.w3.org/TR/turtle/ 3 | // 4 | // TurtleParser is the parser which uses Tokenizer to 5 | // break down the text into meaningful tokens (URIs, strings, 6 | // separators, et cetera.) 7 | 8 | // Tokenizer in turn uses Scanner to handle the character by 9 | // character operations. 10 | // 11 | // TurtleParser uses a tree-like structure (via SubjectNode 12 | // and PredicateNode) to keep track of the subject, predicate, 13 | // and object values as they are parsed. This structure allows 14 | // us to parse multi-predicate (;) and multi-object (,) triples. 15 | // 16 | // Sample usage: 17 | // parser := NewTurtleParser(" , ; .") 18 | // err := parser.Parse() 19 | // for i, triple := range parser.Triples() { 20 | // log.Printf("Triple %d: %s", i, triple) 21 | // } 22 | // Gives: 23 | // Triple 0: . 24 | // Triple 1: . 25 | // Triple 2: . 26 | // 27 | package rdf 28 | 29 | import ( 30 | "errors" 31 | // "log" 32 | "strings" 33 | ) 34 | 35 | type Directive struct { 36 | name string 37 | value string 38 | } 39 | 40 | type TurtleParser struct { 41 | tokenizer Tokenizer 42 | triples []Triple 43 | directives []Directive 44 | } 45 | 46 | func NewTurtleParser(text string) TurtleParser { 47 | tokenizer := NewTokenizer(text) 48 | parser := TurtleParser{tokenizer: tokenizer} 49 | return parser 50 | } 51 | 52 | func (parser *TurtleParser) Parse() error { 53 | for parser.tokenizer.CanRead() { 54 | err := parser.parseNextTriples() 55 | if err != nil { 56 | return err 57 | } 58 | parser.tokenizer.AdvanceWhiteSpace() 59 | } 60 | 61 | parser.applyBaseDirective() 62 | return nil 63 | } 64 | 65 | func (parser TurtleParser) Triples() []Triple { 66 | return parser.triples 67 | } 68 | 69 | func (parser *TurtleParser) applyBaseDirective() { 70 | if len(parser.directives) == 0 { 71 | return 72 | } 73 | 74 | if parser.directives[0].name != "@base" { 75 | // unknown directive 76 | return 77 | } 78 | 79 | base := parser.directives[0] 80 | for i, triple := range parser.triples { 81 | if triple.subject == "<>" { 82 | parser.triples[i].subject = base.value 83 | } 84 | if triple.object == "<>" { 85 | parser.triples[i].object = base.value 86 | } 87 | } 88 | } 89 | 90 | func (parser *TurtleParser) parseNextTriples() error { 91 | var err error 92 | var token string 93 | 94 | for err == nil && parser.tokenizer.CanRead() { 95 | token, err = parser.tokenizer.GetNextToken() 96 | if err != nil || token == "" { 97 | break 98 | } 99 | 100 | isDirective := strings.HasPrefix(token, "@") 101 | if isDirective { 102 | err = parser.parseNextDirective(token) 103 | continue 104 | } 105 | 106 | // triples 107 | subject := NewSubjectNode(token) 108 | err = parser.parsePredicates(&subject) 109 | if err == nil { 110 | for _, triple := range subject.RenderTriples() { 111 | parser.triples = append(parser.triples, triple) 112 | } 113 | } 114 | 115 | } 116 | return err 117 | } 118 | 119 | func (parser *TurtleParser) parseNextDirective(name string) error { 120 | if !parser.tokenizer.CanRead() { 121 | return errors.New("No value found for directive (" + name + ")") 122 | } 123 | 124 | value, err := parser.tokenizer.GetNextToken() 125 | if err != nil { 126 | return err 127 | } 128 | 129 | token, err := parser.tokenizer.GetNextToken() 130 | if token != "." { 131 | return errors.New("Could not find end of directive (" + name + ")") 132 | } 133 | 134 | if err == nil { 135 | directive := Directive{name: name, value: value} 136 | parser.directives = append(parser.directives, directive) 137 | } 138 | return err 139 | } 140 | 141 | func (parser *TurtleParser) parsePredicates(subject *SubjectNode) error { 142 | var err error 143 | var token string 144 | 145 | for err == nil && parser.tokenizer.CanRead() { 146 | token, err = parser.tokenizer.GetNextToken() 147 | if err != nil || token == "." { 148 | // we are done 149 | break 150 | } 151 | predicate := subject.AddPredicate(token) 152 | token, err = parser.parseObjects(predicate) 153 | if err != nil { 154 | break 155 | } else if token == "." { 156 | // we are done, next triple will be for a different subject 157 | break 158 | } else if token == ";" { 159 | // next triple will be for the same subject 160 | continue 161 | } else { 162 | err = errors.New("Unexpected token parsing predicates (" + token + ")") 163 | } 164 | } 165 | return err 166 | } 167 | 168 | func (parser *TurtleParser) parseObjects(predicate *PredicateNode) (string, error) { 169 | var err error 170 | var token string 171 | for parser.tokenizer.CanRead() { 172 | token, err = parser.tokenizer.GetNextToken() 173 | if err != nil || token == "." || token == ";" { 174 | // we are done 175 | break 176 | } else if token == "," { 177 | // the next token will be for the same 178 | // subject + predicate 179 | continue 180 | } else { 181 | // it's a object, add it to the predicate 182 | predicate.AddObject(token) 183 | } 184 | } 185 | return token, err 186 | } 187 | -------------------------------------------------------------------------------- /rdf/turtle_test.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import ( 4 | // "fmt" 5 | "io/ioutil" 6 | "testing" 7 | ) 8 | 9 | func TestOneTriple(t *testing.T) { 10 | parser := NewTurtleParser("

.") 11 | parser.Parse() 12 | if len(parser.Triples()) != 1 { 13 | t.Errorf("Error parsing triples") 14 | } 15 | } 16 | 17 | func TestTwoTriples(t *testing.T) { 18 | parser := NewTurtleParser("

. .") 19 | parser.Parse() 20 | if len(parser.Triples()) != 2 { 21 | t.Errorf("Error parsing triples %d", len(parser.Triples())) 22 | } 23 | } 24 | 25 | func TestTwoTriplesWithComments(t *testing.T) { 26 | parser := NewTurtleParser("#one comment \n

.\n# second comment \n . #last comment") 27 | err := parser.Parse() 28 | if err != nil { 29 | t.Errorf("Error parsing triples %s", err) 30 | } 31 | if len(parser.Triples()) != 2 { 32 | t.Errorf("Unexpected number of triples found: %d", len(parser.Triples())) 33 | } 34 | } 35 | 36 | func TestTriplesWithComma(t *testing.T) { 37 | test := `

, .` 38 | parser := NewTurtleParser(test) 39 | err := parser.Parse() 40 | if err != nil { 41 | t.Errorf("Error parsing comma: %s", err) 42 | } 43 | if len(parser.Triples()) != 2 { 44 | t.Errorf("Incorrect number of triples: %d", len(parser.Triples())) 45 | } 46 | 47 | t1 := parser.Triples()[0].String() 48 | if t1 != "

." { 49 | t.Errorf("Triple 1 is incorrect: %s", t1) 50 | } 51 | 52 | t2 := parser.Triples()[1].String() 53 | if t2 != "

." { 54 | t.Errorf("Triple 2 is incorrect: %s", t1) 55 | } 56 | } 57 | 58 | func TestTriplesWithSemicolon(t *testing.T) { 59 | test := ` ; .` 60 | parser := NewTurtleParser(test) 61 | err := parser.Parse() 62 | if err != nil { 63 | t.Errorf("Error parsing semicolon: %s", err) 64 | } 65 | if len(parser.Triples()) != 2 { 66 | t.Errorf("Incorrect number of triples: %d", len(parser.Triples())) 67 | } 68 | 69 | t0 := parser.Triples()[0].String() 70 | if t0 != " ." { 71 | t.Errorf("Triple 1 is incorrect: %s", t0) 72 | } 73 | 74 | t1 := parser.Triples()[1].String() 75 | if t1 != " ." { 76 | t.Errorf("Triple 2 is incorrect: %s", t1) 77 | } 78 | } 79 | 80 | func TestTriplesWithCommaAndSemicolon(t *testing.T) { 81 | test := `<> a , ; 82 | "High" ; 83 | "Issues that need to be fixed." ; 84 | ; 85 | "Another bug to test." .` 86 | parser := NewTurtleParser(test) 87 | err := parser.Parse() 88 | if err != nil { 89 | t.Errorf("Error parsing text: %s", err) 90 | } 91 | 92 | if len(parser.Triples()) != 6 { 93 | t.Errorf("Incorrect number of triples: %d", len(parser.Triples())) 94 | } 95 | 96 | t0 := parser.Triples()[0].String() 97 | if t0 != "<> a ." { 98 | t.Errorf("Triple 1 is incorrect: %s", t0) 99 | } 100 | 101 | t5 := parser.Triples()[5].String() 102 | if t5 != `<> "Another bug to test." .` { 103 | t.Errorf("Triple 6 is incorrect: %s", t5) 104 | } 105 | } 106 | 107 | func TestW3CFile(t *testing.T) { 108 | filename := "./w3ctest.nt" 109 | bytes, err := ioutil.ReadFile(filename) 110 | if err != nil { 111 | t.Errorf("Error reading w3ctest.nt file: %s", err) 112 | } 113 | 114 | text := string(bytes) 115 | parser := NewTurtleParser(text) 116 | err = parser.Parse() 117 | if err != nil { 118 | t.Errorf("Error parsing W3C ntriples text: %s", err) 119 | } 120 | } 121 | 122 | func TestBaseDirective(t *testing.T) { 123 | filename := "./w3cbase.nt" 124 | bytes, err := ioutil.ReadFile(filename) 125 | if err != nil { 126 | t.Errorf("Error reading w3cbase.nt file: %s", err) 127 | } 128 | 129 | text := string(bytes) 130 | parser := NewTurtleParser(text) 131 | err = parser.Parse() 132 | if err != nil { 133 | t.Errorf("Error parsing W3C base triples: %s", err) 134 | } 135 | 136 | for _, triple := range parser.Triples() { 137 | if triple.subject != "" { 138 | t.Errorf("Base not replaced correctly for %s", triple) 139 | } 140 | } 141 | } 142 | -------------------------------------------------------------------------------- /rdf/uris.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | const ( 4 | RdfTypeUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" 5 | ) 6 | 7 | const ( 8 | LdpResourceUri = "http://www.w3.org/ns/ldp#Resource" 9 | LdpRdfSourceUri = "http://www.w3.org/ns/ldp#RDFSource" 10 | LdpNonRdfSourceUri = "http://www.w3.org/ns/ldp#NonRDFSource" 11 | LdpContainerUri = "http://www.w3.org/ns/ldp#Container" 12 | LdpBasicContainerUri = "http://www.w3.org/ns/ldp#BasicContainer" 13 | LdpDirectContainerUri = "http://www.w3.org/ns/ldp#DirectContainer" 14 | LdpContainsUri = "http://www.w3.org/ns/ldp#contains" 15 | LdpInsertedContentRelationUri = "http://www.w3.org/ns/ldp#insertedContentRelation" 16 | LdpMemberSubjectUri = "http://www.w3.org/ns/ldp#MemberSubject" 17 | LdpMembershipResource = "http://www.w3.org/ns/ldp#membershipResource" 18 | LdpHasMemberRelation = "http://www.w3.org/ns/ldp#hasMemberRelation" 19 | LdpConstrainedBy = "http://www.w3.org/ns/ldp#constrainedBy" 20 | ) 21 | 22 | const ( 23 | // HTTP header links 24 | LdpResourceLink = "<" + LdpResourceUri + ">; rel=\"type\"" 25 | LdpNonRdfSourceLink = "<" + LdpNonRdfSourceUri + ">; rel=\"type\"" 26 | LdpContainerLink = "<" + LdpContainerUri + ">; rel=\"type\"" 27 | LdpBasicContainerLink = "<" + LdpBasicContainerUri + ">; rel=\"type\"" 28 | LdpDirectContainerLink = "<" + LdpDirectContainerUri + ">; rel=\"type\"" 29 | ) 30 | 31 | const ( 32 | DcTitleUri = "http://purl.org/dc/terms/title" 33 | DcCreatedUri = "http://purl.org/dc/terms/created" 34 | ) 35 | 36 | const ( 37 | ServerETagUri = "http://hectorcorrea.com/ldpserver/ns/etag" 38 | ServerContentTypeUri = "http://hectorcorrea.com/ldpserver/ns/contentType" 39 | ) 40 | const ( 41 | TurtleContentType = "text/turtle" 42 | ) 43 | -------------------------------------------------------------------------------- /rdf/w3cbase.nt: -------------------------------------------------------------------------------- 1 | @base . 2 | <> a , , ; 3 | 4 | "modified" ; 5 | 6 | "2015-10-17T23_34_00-04_00" ; 7 | 8 | "2015-10-17T23:34:00-04:00" ; 9 | 10 | "This is a new entry" ; 11 | 12 | , , , , , , , , , . 13 | -------------------------------------------------------------------------------- /rdf/w3ctest.nt: -------------------------------------------------------------------------------- 1 | # Source: http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt 2 | # 3 | # 4 | # Copyright World Wide Web Consortium, (Massachusetts Institute of 5 | # Technology, Institut National de Recherche en Informatique et en 6 | # Automatique, Keio University). 7 | # 8 | # All Rights Reserved. 9 | # 10 | # Please see the full Copyright clause at 11 | # 12 | # 13 | # Test file with a variety of legal N-Triples 14 | # 15 | # Dave Beckett - http://purl.org/net/dajobe/ 16 | # 17 | # $Id: test.nt,v 1.7 2003/10/06 15:52:19 dbeckett2 Exp $ 18 | # 19 | ##################################################################### 20 | 21 | # comment lines 22 | # comment line after whitespace 23 | # empty blank line, then one with spaces and tabs 24 | 25 | 26 | . 27 | _:anon . 28 | _:anon . 29 | # spaces and tabs throughout: 30 | . 31 | 32 | # line ending with CR NL (ASCII 13, ASCII 10) 33 | . 34 | 35 | # 2 statement lines separated by single CR (ASCII 10) 36 | . 37 | . 38 | 39 | 40 | # All literal escapes 41 | "simple literal" . 42 | "backslash:\\" . 43 | "dquote:\"" . 44 | "newline:\n" . 45 | "return\r" . 46 | "tab:\t" . 47 | 48 | # Space is optional before final . 49 | . 50 | "x". 51 | _:anon. 52 | 53 | # \u and \U escapes 54 | # latin small letter e with acute symbol \u00E9 - 3 UTF-8 bytes #xC3 #A9 55 | "\u00E9" . 56 | # Euro symbol \u20ac - 3 UTF-8 bytes #xE2 #x82 #xAC 57 | "\u20AC" . 58 | # resource18 test removed 59 | # resource19 test removed 60 | # resource20 test removed 61 | 62 | # XML Literals as Datatyped Literals 63 | ""^^ . 64 | " "^^ . 65 | "x"^^ . 66 | "\""^^ . 67 | ""^^ . 68 | "a "^^ . 69 | "a c"^^ . 70 | "a\n\nc"^^ . 71 | "chat"^^ . 72 | # resource28 test removed 2003-08-03 73 | # resource29 test removed 2003-08-03 74 | 75 | # Plain literals with languages 76 | "chat"@fr . 77 | "chat"@en . 78 | 79 | # Typed Literals 80 | "abc"^^ . 81 | # resource33 test removed 2003-08-03 82 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | This is a mini LDP Server in Go. 2 | 3 | Linked Data Platform (LDP) is a W3C recommendation that defines rules for how to 4 | implement an HTTP API for read-write Linked Data. The official recommendation can 5 | be found [here](http://www.w3.org/TR/ldp/). 6 | You can also find a more gentle introduction to LDP in 7 | [my blog](http://hectorcorrea.com/blog/introduction-to-ldp/67). 8 | 9 | *Warning*: This is my sandbox project as I learn both Go and LDP. The code in this repo very likely does not follow Go's best practices and it certainly does not conform to the LDP spec (yet). 10 | 11 | 12 | ## Compile and run the server 13 | If Go is installed on your machine: 14 | 15 | cd ~/src 16 | git clone git@github.com:hectorcorrea/ldpserver.git 17 | cd ldpserver 18 | go build 19 | ./ldpserver 20 | 21 | If you are new to Go follow these steps instead: 22 | 23 | # Download and install Go from: http://golang.org/doc/install 24 | # 25 | # Go is very picky about the location of the code (e.g. the code must be 26 | # inside an src folder.) Here is a setup that will work with minimal effort 27 | # and configuration on your part. You can skip the first step if you 28 | # already have an ~/src folder. 29 | # 30 | mkdir ~/src 31 | export GOPATH=~/ 32 | cd ~/src 33 | git clone git@github.com:hectorcorrea/ldpserver.git 34 | cd ldpserver 35 | go build 36 | ./ldpserver 37 | 38 | If you don't care about the source code, the fastest way to get started is to [download the executable for your platform](https://github.com/hectorcorrea/ldpserver/releases) from the release tab, make it an executable on your box, and run it. 39 | 40 | 41 | ## Operations supported 42 | With the server running, you can use `cURL` to submit requests to it. For example, to fetch the root node 43 | 44 | curl localhost:9001 45 | 46 | POST to the root (the Slug defaults to "node" + a sequential number) 47 | 48 | curl -X POST localhost:9001 49 | 50 | Fetch the node created 51 | 52 | curl localhost:9001/node1 53 | 54 | POST a non-RDF to the root 55 | 56 | curl -X POST --header "Content-Type: text/plain" --data "hello world" localhost:9001 57 | 58 | curl -X POST --header "Content-Type: image/jpeg" --data-binary "@filename.jpg" localhost:9001 59 | 60 | Fetch the non-RDF created 61 | 62 | curl localhost:9001/node2 63 | 64 | HTTP HEAD operations are supported 65 | 66 | curl -I localhost:9001/ 67 | curl -I localhost:9001/node1 68 | curl -I localhost:9001/node2 69 | 70 | Add an RDF source to add a child node (you can only add to RDF sources) 71 | 72 | curl -X POST localhost:9001/node1 73 | 74 | See that the child was added 75 | 76 | curl localhost:9001/node1 77 | 78 | Fetch the child 79 | 80 | curl localhost:9001/node1/node3 81 | 82 | Create a node with a custom Slug 83 | 84 | curl -X POST --header "Slug: demo" localhost:9001 85 | 86 | Fetch node created 87 | 88 | curl localhost:9001/demo 89 | 90 | Create an *LDP Direct Container* `/dc1` that uses `/node1` as its `membershipResource` and `someRel` as the member relation... 91 | 92 | curl -X POST --header "Content-Type: text/turtle" --header "Slug: dc1" -d "<> someRel ; ." localhost:9001 93 | 94 | ...add a node to the direct container 95 | 96 | curl -X POST --header "Slug: child1" localhost:9001/dc1 97 | 98 | ...fetch `/node1` and notice that it references `/dc1/child1` with the predicate `someRel` that we defined in the direct container: 99 | 100 | curl localhost:9001/dc1 101 | 102 | ## Demo 103 | Take a look at `demo.sh` file for an example of a shell script that executes some of the operations supported. To run this demo make sure the LDP Server is running in a separate terminal window, for example: 104 | 105 | # Run the LDP Server in one terminal window 106 | ./ldpserver 107 | 108 | # Run the demo script in a separate terminal window 109 | ./demo.sh 110 | 111 | 112 | ## Storage 113 | Every resource (RDF or non-RDF) is saved in a folder inside the data folder. 114 | 115 | Every RDF source is saved on its own folder with single file inside of it. This file is always `meta.rdf` and it has the triples of the node. 116 | 117 | Non-RDF are also saved on their own folder and with a `meta.rdf` file for their metadata but also a file `data.bin` with the non-RDF content. 118 | 119 | For example, if we have two nodes (blog1 and blog2) and blog1 is an RDF node and blog2 is a non-RDF then the data structure would look as follow: 120 | 121 | /data/meta.rdf (root node) 122 | /data/blog1/meta.rdf (RDF for blog1) 123 | /data/blog2/meta.rdf (RDF for blog2) 124 | /data/blog2/data.bin (binary for blog2) 125 | 126 | 127 | ## Overview of the Code 128 | 129 | * `main.go` is the launcher program. It's only job is to kick off the web server. 130 | * `web/web.go` is the web server. It's job is to handle HTTP requests and responses. This is the only part of the code that is aware of the web. 131 | * `server/server.go` handles most of the operations like creating new nodes and fetching existing ones. 132 | * `ldp/node.go` handles operations at the individual node level (fetching and saving.) 133 | * `rdf/` contains utilities to parse and update RDF triples and graphs. 134 | 135 | 136 | ## TODO 137 | A lot. 138 | 139 | * Add validation to make sure the data in the root node matches the URL (host:port) where the server is running. 140 | 141 | * Support isMemberOfRelation in Direct Containers. 142 | 143 | * Support Indirect Containers. 144 | 145 | 146 | ## LDP Test Suite 147 | The W3C provides a test suite to make sure LDP server implementations meet a minimum criteria. The test suite can be found at http://w3c.github.io/ldp-testsuite/ 148 | 149 | In order to run the suite against this repo you need to do the following: 150 | 151 | 1. Clone the ldp-testsuite repo: `git clone https://github.com/w3c/ldp-testsuite` 152 | 1. Download maven from http://maven.apache.org/download.cgi 153 | 1. Unzip maven to the `ldp-testsuite` folder 154 | 1. `cd ldp-testsuite` 155 | 1. Run `bin/mvn package` 156 | 157 | ...and then you can run the following command against the LDP Server to execute an individual test: 158 | 159 | java -jar target/ldp-testsuite-0.2.0-SNAPSHOT-shaded.jar --server http://localhost:9001 --test name_of_test_goes_here --basic 160 | 161 | ...or as follow to run all basic container tests (including support for non-RDF): 162 | 163 | java -jar target/ldp-testsuite-0.2.0-SNAPSHOT-shaded.jar --server http://localhost:9001 --basic --non-rdf 164 | 165 | As of 1/9/2016 these are the results of all basic container tests (including support for non-RDF): 166 | 167 | LDP Test Suite 168 | Total tests run: 112, Failures: 4, Skips: 28 169 | 170 | 171 | TODO: Document how to test DC and the results 97/5/27 172 | -------------------------------------------------------------------------------- /server/minter.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import "fmt" 4 | import "strconv" 5 | import "ldpserver/fileio" 6 | 7 | // TODO: Move the handling of the IdFile to its own class. 8 | func (server Server) createIdFile() { 9 | idFile := server.settings.IdFile() 10 | if fileio.FileExists(idFile) { 11 | return 12 | } 13 | 14 | err := fileio.CreateFile(idFile, "0") 15 | if err != nil { 16 | panic(fmt.Sprintf("Could not create ID file: %s", err.Error())) 17 | } 18 | } 19 | 20 | func CreateMinter(idFile string) chan string { 21 | nextId := make(chan string) 22 | go func(idFile string) { 23 | for { 24 | nextId <- mintNextId(idFile) 25 | } 26 | }(idFile) 27 | return nextId 28 | } 29 | 30 | // Uses a synchronous channel to force sequential process 31 | // of this code. 32 | func MintNextUri(slug string, minter chan string) string { 33 | nextId := <-minter 34 | return slug + nextId 35 | } 36 | 37 | func mintNextId(idFile string) string { 38 | lastText, err := fileio.ReadFile(idFile) 39 | if err != nil { 40 | errorMsg := fmt.Sprintf("Could not read last id from [%s]. Error: %s", idFile, err) 41 | panic(errorMsg) 42 | } 43 | 44 | lastId, err := strconv.ParseInt(lastText, 10, 0) 45 | if err != nil { 46 | errorMsg := fmt.Sprintf("Could not calculate last id from [%s]", idFile) 47 | panic(errorMsg) 48 | } 49 | 50 | nextId := strconv.Itoa(int(lastId + 1)) 51 | err = fileio.WriteFile(idFile, nextId) 52 | if err != nil { 53 | errorMsg := fmt.Sprintf("Error writing next id to [%s]", idFile) 54 | panic(errorMsg) 55 | } 56 | return nextId 57 | } 58 | -------------------------------------------------------------------------------- /server/nonrdf.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | "ldpserver/ldp" 7 | "ldpserver/textstore" 8 | "ldpserver/util" 9 | ) 10 | 11 | // POST 12 | func (server Server) CreateNonRdfSource(reader io.ReadCloser, parentPath, slug, triples string) (ldp.Node, error) { 13 | path, err := server.newPathFromSlug(parentPath, slug) 14 | if err != nil { 15 | return ldp.Node{}, err 16 | } 17 | 18 | resource := server.createResource(path) 19 | err = resource.Error() 20 | if err != nil && err != textstore.AlreadyExistsError && err != textstore.CreateDeletedError { 21 | return ldp.Node{}, resource.Error() 22 | } 23 | 24 | if err == textstore.AlreadyExistsError || err == textstore.CreateDeletedError { 25 | if slug == "" { 26 | // We generated a duplicate node. 27 | return ldp.Node{}, ldp.DuplicateNodeError 28 | } 29 | 30 | // The user provided slug is duplicated. 31 | // Let's try with one of our own. 32 | return server.CreateNonRdfSource(reader, parentPath, "", triples) 33 | } 34 | 35 | // Create new node 36 | node, err := ldp.NewNonRdfNode(server.settings, reader, path, triples) 37 | if err != nil { 38 | return node, err 39 | } 40 | 41 | if path != "/" { 42 | err = server.addNodeToContainer(node, parentPath) 43 | } 44 | 45 | return node, err 46 | } 47 | 48 | // PUT 49 | func (server Server) ReplaceNonRdfSource(reader io.ReadCloser, path, etag, triples string) (ldp.Node, error) { 50 | if isRootPath(path) { 51 | return ldp.Node{}, errors.New("Cannot replace root node with an Non-RDF source") 52 | } 53 | 54 | resource := server.createResource(path) 55 | if resource.Error() != nil && resource.Error() != textstore.AlreadyExistsError { 56 | return ldp.Node{}, resource.Error() 57 | } 58 | 59 | if resource.Error() == textstore.AlreadyExistsError { 60 | // Replace existing node 61 | return ldp.ReplaceNonRdfNode(server.settings, reader, path, etag, triples) 62 | } 63 | 64 | // Create new node 65 | node, err := ldp.NewNonRdfNode(server.settings, reader, path, triples) 66 | if err != nil { 67 | return ldp.Node{}, err 68 | } 69 | 70 | parentPath := util.ParentUriPath(path) 71 | err = server.addNodeToContainer(node, parentPath) 72 | return node, err 73 | } 74 | -------------------------------------------------------------------------------- /server/rdf.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "ldpserver/ldp" 5 | "ldpserver/textstore" 6 | ) 7 | 8 | // POST 9 | func (server Server) CreateRdfSource(triples string, parentPath string, slug string) (ldp.Node, error) { 10 | path, err := server.newPathFromSlug(parentPath, slug) 11 | if err != nil { 12 | return ldp.Node{}, err 13 | } 14 | 15 | resource := server.createResource(path) 16 | err = resource.Error() 17 | if err != nil && err != textstore.AlreadyExistsError && err != textstore.CreateDeletedError { 18 | return ldp.Node{}, resource.Error() 19 | } 20 | 21 | if err == textstore.AlreadyExistsError || err == textstore.CreateDeletedError { 22 | if slug == "" { 23 | // We generated a duplicate node. 24 | return ldp.Node{}, ldp.DuplicateNodeError 25 | } 26 | 27 | // The user provided slug is duplicated. 28 | // Let's try with one of our own. 29 | return server.CreateRdfSource(triples, parentPath, "") 30 | } 31 | 32 | // Create new node 33 | node, err := ldp.NewRdfNode(server.settings, triples, path) 34 | if err != nil { 35 | return ldp.Node{}, err 36 | } 37 | 38 | if path != "/" { 39 | err = server.addNodeToContainer(node, parentPath) 40 | } 41 | 42 | return node, err 43 | } 44 | 45 | // PUT 46 | func (server Server) ReplaceRdfSource(triples string, parentPath string, slug string, etag string) (ldp.Node, error) { 47 | path, err := server.newPathFromSlug(parentPath, slug) 48 | if err != nil { 49 | return ldp.Node{}, err 50 | } 51 | 52 | resource := server.createResource(path) 53 | if resource.Error() != nil && resource.Error() != textstore.AlreadyExistsError { 54 | return ldp.Node{}, resource.Error() 55 | } 56 | 57 | if resource.Error() == textstore.AlreadyExistsError { 58 | // Replace existing node 59 | return ldp.ReplaceRdfNode(server.settings, triples, path, etag) 60 | } 61 | 62 | // Create new node 63 | node, err := ldp.NewRdfNode(server.settings, triples, path) 64 | if err != nil { 65 | return ldp.Node{}, err 66 | } 67 | 68 | if path != "/" { 69 | err = server.addNodeToContainer(node, parentPath) 70 | } 71 | 72 | return node, err 73 | } 74 | -------------------------------------------------------------------------------- /server/root.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "fmt" 5 | "ldpserver/ldp" 6 | "log" 7 | ) 8 | 9 | func (server Server) createRoot() { 10 | _, err := server.GetHead("/") 11 | if err == nil { 12 | return 13 | } 14 | 15 | if err != ldp.NodeNotFoundError { 16 | panic(fmt.Sprintf("Error reading root node: %s", err.Error())) 17 | } 18 | 19 | _, err = server.CreateRdfSource("", ".", ".") 20 | if err != nil { 21 | panic(fmt.Sprintf("Could not create root node: %s", err.Error())) 22 | } 23 | 24 | log.Printf("Root node created on disk at : %s\n", server.settings.DataPath()) 25 | } 26 | -------------------------------------------------------------------------------- /server/server.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | "ldpserver/ldp" 7 | "ldpserver/textstore" 8 | "ldpserver/util" 9 | // "log" 10 | ) 11 | 12 | const defaultSlug string = "node" 13 | 14 | type Server struct { 15 | settings ldp.Settings 16 | minter chan string 17 | // this should use an interface so it's not tied to "textStore" 18 | nextResource chan textstore.Store 19 | } 20 | 21 | func NewServer(rootUri string, dataPath string) Server { 22 | settings := ldp.SettingsNew(rootUri, dataPath) 23 | var server Server 24 | server.settings = settings 25 | server.createIdFile() 26 | server.minter = CreateMinter(server.settings.IdFile()) 27 | server.nextResource = make(chan textstore.Store) 28 | server.createRoot() 29 | return server 30 | } 31 | 32 | func (server Server) GetNode(path string, pref ldp.PreferTriples) (ldp.Node, error) { 33 | return ldp.GetNode(server.settings, path, pref) 34 | } 35 | 36 | func (server Server) GetHead(path string) (ldp.Node, error) { 37 | return ldp.GetHead(server.settings, path) 38 | } 39 | 40 | func (server Server) PatchNode(path string, triples string) error { 41 | node, err := ldp.GetNode(server.settings, path, ldp.PreferTriples{}) 42 | if err != nil { 43 | return err 44 | } 45 | return node.Patch(triples) 46 | } 47 | 48 | func (server Server) DeleteNode(path string) error { 49 | if isRootPath(path) { 50 | return errors.New("Cannot delete root node") 51 | } 52 | 53 | node, err := ldp.GetNode(server.settings, path, ldp.PreferTriples{}) 54 | if err != nil { 55 | return err 56 | } 57 | 58 | parentPath := util.ParentUriPath(path) 59 | parent, err := server.getContainer(parentPath) 60 | if err != nil { 61 | return err 62 | } 63 | 64 | // First remove the reference to the node to be deleted... 65 | err = parent.RemoveContainsUri("<" + node.Uri() + ">") 66 | if err != nil { 67 | return err 68 | } 69 | 70 | // ...then delete the requested node 71 | return node.Delete() 72 | } 73 | 74 | func (server Server) addNodeToContainer(node ldp.Node, path string) error { 75 | container, err := server.getContainer(path) 76 | if err != nil { 77 | return err 78 | } 79 | return container.AddChild(node) 80 | } 81 | 82 | func (server Server) newPathFromSlug(parentPath string, slug string) (string, error) { 83 | isRootNode := (parentPath == ".") && (slug == ".") 84 | if isRootNode { 85 | return "/", nil // special case 86 | } 87 | 88 | if slug == "" { 89 | // Generate a new server URI (e.g. node34) 90 | slug = MintNextUri(defaultSlug, server.minter) 91 | } 92 | 93 | if !util.IsValidSlug(slug) { 94 | return "", fmt.Errorf("Invalid Slug (%s)", slug) 95 | } 96 | return util.UriConcat(parentPath, slug), nil 97 | } 98 | 99 | func (server Server) createResource(path string) textstore.Store { 100 | pathOnDisk := util.PathConcat(server.settings.DataPath(), path) 101 | // Queue up the creation of a new resource 102 | go func(pathOnDisk string) { 103 | server.nextResource <- textstore.CreateStore(pathOnDisk) 104 | }(pathOnDisk) 105 | 106 | // Wait for the new resource to be available. 107 | resource := <-server.nextResource 108 | return resource 109 | } 110 | 111 | func (server Server) getContainer(path string) (ldp.Node, error) { 112 | 113 | if isRootPath(path) { 114 | // Shortcut. We know for sure this is a container 115 | return ldp.GetHead(server.settings, "/") 116 | } 117 | 118 | node, err := ldp.GetNode(server.settings, path, ldp.PreferTriples{}) 119 | if err != nil { 120 | return node, err 121 | } else if !node.IsBasicContainer() { 122 | errorMsg := fmt.Sprintf("%s is not a container", path) 123 | return node, errors.New(errorMsg) 124 | } 125 | return node, nil 126 | } 127 | 128 | func (server Server) getContainerUri(parentPath string) (string, error) { 129 | if isRootPath(parentPath) { 130 | return server.settings.RootUri(), nil 131 | } 132 | 133 | // Make sure the parent node exists and it's a container 134 | parentNode, err := ldp.GetNode(server.settings, parentPath, ldp.PreferTriples{}) 135 | if err != nil { 136 | return "", err 137 | } else if !parentNode.IsBasicContainer() { 138 | return "", errors.New("Parent is not a container") 139 | } 140 | return parentNode.Uri(), nil 141 | } 142 | 143 | func isRootPath(path string) bool { 144 | return path == "" || path == "/" 145 | } 146 | -------------------------------------------------------------------------------- /server/server_test.go: -------------------------------------------------------------------------------- 1 | package server 2 | 3 | import ( 4 | "fmt" 5 | "github.com/hectorcorrea/rdf" 6 | "ldpserver/ldp" 7 | "ldpserver/util" 8 | "log" 9 | "os" 10 | "path/filepath" 11 | "strings" 12 | "testing" 13 | ) 14 | 15 | var dataPath string 16 | var theServer Server 17 | var rootUrl = "http://localhost:9001/" 18 | var emptySlug = "" 19 | 20 | func init() { 21 | dataPath, _ := filepath.Abs(filepath.Dir(os.Args[0])) 22 | theServer = NewServer(rootUrl, dataPath) 23 | } 24 | 25 | func TestBadSlug(t *testing.T) { 26 | _, err := theServer.CreateRdfSource("", "/", "/invalid/") 27 | if err == nil { 28 | t.Error("Failed to detect an invalid slug") 29 | } 30 | } 31 | 32 | func TestCreateRdf(t *testing.T) { 33 | _, err := theServer.CreateRdfSource("", "/", "slugA") 34 | if err != nil { 35 | t.Errorf("Error creating RDF. Error: %s", err) 36 | } 37 | 38 | node, err := theServer.GetNode("/slugA", ldp.PreferTriples{}) 39 | if err != nil { 40 | t.Errorf("Error fetching new node /slugA. Error: %s", err) 41 | } 42 | 43 | if !node.IsRdf() { 44 | t.Errorf("RDF source was created but not as RDF") 45 | } 46 | 47 | node2, err := theServer.CreateRdfSource("", "/", "slugA") 48 | if err != nil { 49 | t.Errorf("Error %s while attemping to create duplicate node", err) 50 | } 51 | 52 | if node2.Path() == "/slugA" { 53 | t.Errorf("A duplicate node was created") 54 | } 55 | } 56 | 57 | func TestReplaceRdf(t *testing.T) { 58 | triples := "<> xx:version \"version1\" ." 59 | node, err := theServer.ReplaceRdfSource(triples, "/", "rdf-test", "ignore-etag") 60 | log.Printf("1. %s", node.Content()) 61 | if err != nil { 62 | t.Errorf("Error creating a new RDF node with replace: %s", err) 63 | } 64 | 65 | path := node.Path()[1:] 66 | etag := node.Etag() 67 | triples = "<> xx:version \"version2\" ." 68 | node, err = theServer.ReplaceRdfSource(triples, "/", path, etag) 69 | log.Printf("2. %s", node.Content()) 70 | if err != nil { 71 | t.Errorf("Error replacing RDF node: %s", err) 72 | } 73 | 74 | if !node.HasTriple("xx:version", "\"version2\"") { 75 | t.Errorf("Error replacing RDF node. Updated triple not found") 76 | } 77 | 78 | _, err = theServer.ReplaceRdfSource(triples, "/", path, "bad-etag") 79 | if err != ldp.EtagMismatchError { 80 | t.Errorf("Failed to detect etag mismatch: %s", err) 81 | } 82 | 83 | _, err = theServer.ReplaceRdfSource(triples, "/", path, "") 84 | if err != ldp.EtagMissingError { 85 | t.Errorf("Failed to detect missing etag: %s", err) 86 | } 87 | } 88 | 89 | func TestCreateDirectContainer(t *testing.T) { 90 | // Create a helper node 91 | helperNode, err := theServer.CreateRdfSource("", "/", "other") 92 | 93 | // Create the direct container (pointing to the helper node) 94 | dcTriple1 := fmt.Sprintf("<> <%s> <%s> .\n", rdf.LdpMembershipResource, helperNode.Uri()) 95 | dcTriple2 := fmt.Sprintf("<> <%s> .\n", rdf.LdpHasMemberRelation) 96 | dcTriples := dcTriple1 + dcTriple2 97 | dcNode, err := theServer.CreateRdfSource(dcTriples, "/", "dc") 98 | if err != nil { 99 | t.Errorf("Error creating direct container", err) 100 | } 101 | 102 | dcNode, err = theServer.GetNode(dcNode.Path(), ldp.PreferTriples{}) 103 | if err != nil { 104 | t.Errorf("Error fetching direct container", err) 105 | } 106 | 107 | if !dcNode.IsBasicContainer() { 108 | t.Errorf("Direct container fetched but not marked as a BASIC container %s", dcNode.Content()) 109 | } 110 | if !dcNode.IsDirectContainer() { 111 | t.Errorf("Direct container fetched but not marked as a DIRECT container %s", dcNode.Content()) 112 | } 113 | 114 | // Add a child to the direct container 115 | childNode, err := theServer.CreateRdfSource("", dcNode.Path(), "child") 116 | if err != nil { 117 | t.Errorf("Error adding child to Direct Container %s", err) 118 | } 119 | 120 | // Reload our helper node and make sure the child is referenced on it. 121 | helperNode, err = theServer.GetNode(helperNode.Path(), ldp.PreferTriples{}) 122 | if !helperNode.HasTriple("", "<"+childNode.Uri()+">") { 123 | t.Error("Helper node did not get new triple when adding to a Direct Container") 124 | } 125 | } 126 | 127 | func TestCreateChildRdf(t *testing.T) { 128 | parentNode, _ := theServer.CreateRdfSource("", "/", emptySlug) 129 | 130 | rdfNode, err := theServer.CreateRdfSource("", parentNode.Path(), emptySlug) 131 | if err != nil { 132 | t.Errorf("Error creating child RDF node under %s", err, parentNode.Uri()) 133 | } 134 | 135 | if !strings.HasPrefix(rdfNode.Uri(), parentNode.Uri()) || rdfNode.Uri() == parentNode.Uri() { 136 | t.Errorf("Child URI %s does not seem to be under the parent URI %s", rdfNode.Uri(), parentNode.Uri()) 137 | } 138 | 139 | invalidPath := parentNode.Path() + "/invalid" 140 | invalidNode, err := theServer.CreateRdfSource("", invalidPath, emptySlug) 141 | if err == nil { 142 | t.Errorf("A node was added to an invalid path %s %s", err, invalidNode.Uri()) 143 | } 144 | 145 | reader := util.FakeReaderCloser{Text: "HELLO"} 146 | nonRdfNode, err := theServer.CreateNonRdfSource(reader, parentNode.Path(), emptySlug, "") 147 | if err != nil { 148 | t.Errorf("Error creating child non-RDF node under %s. Error: %s", parentNode.Uri(), err) 149 | } 150 | 151 | if !strings.HasPrefix(nonRdfNode.Uri(), parentNode.Uri()) || nonRdfNode.Uri() == parentNode.Uri() { 152 | t.Errorf("Child URI %s does not seem to be under the parent URI %s", nonRdfNode.Uri(), parentNode.Uri()) 153 | } 154 | 155 | _, err = theServer.CreateRdfSource("", nonRdfNode.Path(), emptySlug) 156 | if err == nil { 157 | t.Errorf("A child was added to a non-RDF node! %s", nonRdfNode.Uri()) 158 | } 159 | } 160 | 161 | func TestCreateRdfWithTriples(t *testing.T) { 162 | triples := "<> .\n .\n" 163 | node, err := theServer.CreateRdfSource(triples, "/", emptySlug) 164 | if err != nil || !node.IsRdf() { 165 | t.Errorf("Error creating RDF") 166 | } 167 | 168 | node, err = theServer.GetNode(node.Path(), ldp.PreferTriples{}) 169 | if err != nil || node.Uri() != util.UriConcat(rootUrl, node.Path()) { 170 | t.Errorf("err %v, uri %s", err, node.Uri()) 171 | } 172 | 173 | if !node.HasTriple("", "") { 174 | t.Errorf("Blank node not handled correctly %s", node.Uri()) 175 | t.Errorf(node.DebugString()) 176 | } 177 | 178 | if node.HasTriple("x", "z") { 179 | t.Errorf("Unexpected tripled for new subject %s", node.Uri()) 180 | } 181 | } 182 | 183 | func TestCreateNonRdf(t *testing.T) { 184 | reader := util.FakeReaderCloser{Text: "HELLO"} 185 | _, err := theServer.CreateNonRdfSource(reader, "/", "hello", "") 186 | if err != nil { 187 | t.Errorf("Error creating Non RDF") 188 | } 189 | 190 | node, err := theServer.GetNode("hello", ldp.PreferTriples{}) 191 | if err != nil { 192 | t.Errorf("Could not read new Non-RDF node: %s", err) 193 | } 194 | 195 | if node.IsRdf() { 196 | t.Errorf("Node created as RDF instead of Non-RDF") 197 | } 198 | 199 | if node.Content() != "HELLO" { 200 | t.Errorf("Non-RDF content is not the expected one, %s", node.Content()) 201 | } 202 | 203 | node, err = theServer.CreateNonRdfSource(reader, "/", "hello", "") 204 | if err != nil { 205 | t.Errorf("Error when attempting to create a duplicate node. Error: %s", err) 206 | } 207 | 208 | if node.Path() == "/hello" { 209 | t.Errorf("Failed to generate a new slug for the duplicate node") 210 | } 211 | } 212 | 213 | func TestReplaceNonRdf(t *testing.T) { 214 | path := "/non-rdf-test" 215 | reader := util.FakeReaderCloser{Text: "HELLO"} 216 | node, err := theServer.ReplaceNonRdfSource(reader, path, "ignored-etag", "") 217 | if err != nil { 218 | t.Errorf("Error creating a new non-RDF node with replace: %s", err) 219 | } 220 | 221 | reader2 := util.FakeReaderCloser{Text: "BYE"} 222 | node, err = theServer.ReplaceNonRdfSource(reader2, path, node.Etag(), "") 223 | if err != nil { 224 | t.Errorf("Error replacing Non-RDF node: %s", err) 225 | } 226 | 227 | if node.Content() != "BYE" { 228 | t.Errorf("Non-RDF content was not replaced. %s", node.Content()) 229 | } 230 | 231 | _, err = theServer.ReplaceNonRdfSource(reader, path, "bad-etag", "") 232 | if err != ldp.EtagMismatchError { 233 | t.Errorf("Failed to detect etag mismatch: %s", err) 234 | } 235 | 236 | _, err = theServer.ReplaceNonRdfSource(reader, path, "", "") 237 | if err != ldp.EtagMissingError { 238 | t.Errorf("Failed to detect missing etag: %s", err) 239 | } 240 | } 241 | 242 | func TestPatchRdf(t *testing.T) { 243 | triples := "<> .\n<> .\n" 244 | node, _ := theServer.CreateRdfSource(triples, "/", emptySlug) 245 | node, _ = theServer.GetNode(node.Path(), ldp.PreferTriples{}) 246 | if !node.HasTriple("", "") || !node.HasTriple("", "") { 247 | t.Errorf("Expected triple not found %s", node.Content()) 248 | } 249 | 250 | newTriples := "<> .\n" 251 | err := node.Patch(newTriples) 252 | if err != nil { 253 | t.Errorf("Error during Patch %s", err) 254 | } else if !node.HasTriple("", "") || 255 | !node.HasTriple("", "") || 256 | !node.HasTriple("", "") { 257 | t.Errorf("Expected triple not after patch found %s", node.Content()) 258 | } 259 | } 260 | 261 | func TestPatchNonRdf(t *testing.T) { 262 | reader1 := util.FakeReaderCloser{Text: "HELLO"} 263 | node, _ := theServer.CreateNonRdfSource(reader1, "/", emptySlug, "") 264 | node, _ = theServer.GetNode(node.Path(), ldp.PreferTriples{}) 265 | if node.Content() != "HELLO" { 266 | t.Errorf("Unexpected non-RDF content found %s", node.Content()) 267 | } 268 | 269 | if err := node.Patch("whatever"); err == nil { 270 | t.Errorf("Shouldn't be able to patch non-RDF") 271 | } 272 | } 273 | -------------------------------------------------------------------------------- /textstore/textstore.go: -------------------------------------------------------------------------------- 1 | package textstore 2 | 3 | import ( 4 | "errors" 5 | "io" 6 | "ldpserver/fileio" 7 | "ldpserver/util" 8 | "os" 9 | ) 10 | 11 | var AlreadyExistsError = errors.New("Already exists") 12 | var CreateDeletedError = errors.New("Attempting to create a store that has been previously deleted") 13 | 14 | const metaFile string = "meta.rdf" 15 | const dataFile string = "data.bin" 16 | const deletedMarkFile string = "deleted" 17 | 18 | type Store struct { 19 | folder string 20 | err error 21 | } 22 | 23 | func NewStore(folder string) Store { 24 | return Store{folder: folder} 25 | } 26 | 27 | func CreateStore(folder string) Store { 28 | store := NewStore(folder) 29 | switch { 30 | case store.Exists(): 31 | store.err = AlreadyExistsError 32 | case store.isDeleted(): 33 | store.err = CreateDeletedError 34 | default: 35 | store.err = store.SaveMetaFile("") 36 | } 37 | return store 38 | } 39 | 40 | func (store Store) Exists() bool { 41 | return storeExists(store.folder) 42 | } 43 | 44 | func (store Store) Error() error { 45 | return store.err 46 | } 47 | 48 | func (store Store) Delete() error { 49 | // delete the metafile 50 | metaFileFullPath := util.PathConcat(store.folder, metaFile) 51 | err := os.Remove(metaFileFullPath) 52 | if err != nil { 53 | return err 54 | } 55 | 56 | // delete the data file 57 | dataFileFullPath := util.PathConcat(store.folder, dataFile) 58 | if fileio.FileExists(dataFileFullPath) { 59 | err = os.Remove(dataFileFullPath) 60 | if err != nil { 61 | return err 62 | } 63 | } 64 | 65 | return store.markAsDeleted() 66 | } 67 | 68 | func (store Store) SaveMetaFile(content string) error { 69 | fullFilename := util.PathConcat(store.folder, metaFile) 70 | return fileio.WriteFile(fullFilename, content) 71 | } 72 | 73 | func (store Store) AppendToMetaFile(content string) error { 74 | fullFilename := util.PathConcat(store.folder, metaFile) 75 | return fileio.AppendToFile(fullFilename, content) 76 | } 77 | 78 | func (store Store) SaveDataFile(reader io.ReadCloser) error { 79 | fullFilename := util.PathConcat(store.folder, dataFile) 80 | out, err := os.Create(fullFilename) 81 | if err != nil { 82 | return err 83 | } 84 | defer out.Close() 85 | io.Copy(out, reader) 86 | return out.Close() 87 | } 88 | 89 | // Should this return a reader? 90 | func (store Store) ReadMetaFile() (string, error) { 91 | fullFilename := util.PathConcat(store.folder, metaFile) 92 | return fileio.ReadFile(fullFilename) 93 | } 94 | 95 | // Should this return a reader? 96 | func (store Store) ReadDataFile() (string, error) { 97 | fullFilename := util.PathConcat(store.folder, dataFile) 98 | return fileio.ReadFile(fullFilename) 99 | } 100 | 101 | func (store Store) isDeleted() bool { 102 | deletedFile := util.PathConcat(store.folder, deletedMarkFile) 103 | return fileio.FileExists(deletedFile) 104 | } 105 | 106 | func (store Store) markAsDeleted() error { 107 | fullFilename := util.PathConcat(store.folder, deletedMarkFile) 108 | return fileio.WriteFile(fullFilename, "deleted") 109 | } 110 | 111 | func storeExists(folder string) bool { 112 | // we consider that it exists as long as there is a 113 | // metadata file on it. 114 | // Notice that we don't look for the Deleted Mark File 115 | // when determining if it exists or not. 116 | metaRdf := util.PathConcat(folder, metaFile) 117 | return fileio.FileExists(metaRdf) 118 | } 119 | -------------------------------------------------------------------------------- /textstore/textstore_test.go: -------------------------------------------------------------------------------- 1 | package textstore 2 | 3 | import ( 4 | "ldpserver/util" 5 | "os" 6 | "path/filepath" 7 | "testing" 8 | ) 9 | 10 | var dataPath string 11 | 12 | func init() { 13 | dataPath, _ = filepath.Abs(filepath.Dir(os.Args[0])) 14 | } 15 | 16 | func TestTextStore(t *testing.T) { 17 | store := NewStore(dataPath) 18 | if store.Exists() { 19 | t.Errorf("Found an unexpected text store at %s", dataPath) 20 | } 21 | 22 | store = CreateStore(dataPath) 23 | if !store.Exists() { 24 | t.Errorf("Error creating text store at %s", dataPath) 25 | } 26 | 27 | reader := util.FakeReaderCloser{Text: "hello"} 28 | if err := store.SaveDataFile(reader); err != nil { 29 | t.Errorf("Error %s saving text to data file at %s", err, dataPath) 30 | } 31 | 32 | text, err := store.ReadDataFile() 33 | if err != nil { 34 | t.Errorf("Error %s reading text from data file at %s", err, dataPath) 35 | } 36 | 37 | if text != "hello" { 38 | t.Errorf("Unexpected text %s found when reading store at %s", err, dataPath) 39 | } 40 | 41 | store = CreateStore(dataPath) 42 | if store.Error() == nil { 43 | t.Errorf("Failed to detect override on create") 44 | } 45 | } 46 | -------------------------------------------------------------------------------- /util/util.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import ( 4 | "io" 5 | "path" 6 | "regexp" 7 | "strings" 8 | ) 9 | 10 | func PathConcat(path1, path2 string) string { 11 | if strings.HasSuffix(path1, "/") { 12 | if strings.HasPrefix(path2, "/") { 13 | return path1 + path2[1:] 14 | } else { 15 | return path1 + path2 16 | } 17 | } 18 | 19 | if strings.HasPrefix(path2, "/") { 20 | return path1 + path2 21 | } 22 | 23 | return path1 + "/" + path2 24 | } 25 | 26 | func UriConcat(path1, path2 string) string { 27 | return StripSlash(PathConcat(path1, path2)) 28 | } 29 | 30 | func StripSlash(path string) string { 31 | if strings.HasSuffix(path, "/") { 32 | return path[0 : len(path)-1] 33 | } 34 | return path 35 | } 36 | 37 | func PathFromUri(rootUri, uri string) string { 38 | if strings.HasPrefix(uri, rootUri) { 39 | return uri[len(rootUri):] 40 | } 41 | return uri 42 | } 43 | 44 | func ParentUriPath(fullPath string) string { 45 | parent, _ := DirBasePath(fullPath) 46 | if parent == "" || parent == "." { 47 | return "/" 48 | } 49 | return parent 50 | } 51 | 52 | func DirBasePath(fullPath string) (string, string) { 53 | var dir string 54 | var base string 55 | 56 | if strings.HasSuffix(fullPath, "/") { 57 | dir = path.Dir(strings.TrimSuffix(fullPath, "/")) 58 | base = path.Base(strings.TrimSuffix(fullPath, "/")) 59 | } else { 60 | dir = path.Dir(fullPath) 61 | base = path.Base(fullPath) 62 | } 63 | return dir, base 64 | } 65 | 66 | // For our purposes slugs must be alpha-numerical and can include 67 | // -, _, and (non-contiguous) periods. 68 | func IsValidSlug(slug string) bool { 69 | if slug == "." || strings.Contains(slug, "..") || path.Clean(slug) != slug { 70 | return false 71 | } 72 | 73 | // Source: https://www.socketloop.com/tutorials/golang-regular-expression-alphanumeric-underscore 74 | re := regexp.MustCompile(`^[a-zA-Z0-9_\.-]*$`) 75 | return re.MatchString(slug) 76 | } 77 | 78 | func RemoveAngleBrackets(text string) string { 79 | if strings.HasPrefix(text, "<") && strings.HasSuffix(text, ">") { 80 | return text[1 : len(text)-1] 81 | } 82 | return text 83 | } 84 | 85 | func RemoveQuotes(text string) string { 86 | if strings.HasPrefix(text, "\"") && strings.HasSuffix(text, "\"") { 87 | return text[1 : len(text)-1] 88 | } 89 | return text 90 | } 91 | 92 | // Used for testing 93 | type FakeReaderCloser struct { 94 | Text string 95 | } 96 | 97 | func (reader FakeReaderCloser) Read(buffer []byte) (int, error) { 98 | bytes := []byte(reader.Text) 99 | for i, b := range bytes { 100 | buffer[i] = b 101 | } 102 | return len(bytes), io.EOF 103 | } 104 | 105 | func (reader FakeReaderCloser) Close() error { 106 | return nil 107 | } 108 | -------------------------------------------------------------------------------- /util/util_test.go: -------------------------------------------------------------------------------- 1 | package util 2 | 3 | import "testing" 4 | 5 | func TestPathConcat(t *testing.T) { 6 | if test := PathConcat("a", "b"); test != "a/b" { 7 | t.Errorf("PathConcat failed: %s", test) 8 | } 9 | 10 | if test := PathConcat("a/", "b"); test != "a/b" { 11 | t.Errorf("PathConcat failed: %s", test) 12 | } 13 | 14 | if test := PathConcat("a", "/b"); test != "a/b" { 15 | t.Errorf("PathConcat failed: %s", test) 16 | } 17 | 18 | if test := PathConcat("a/", "/b"); test != "a/b" { 19 | t.Errorf("PathConcat failed: %s", test) 20 | } 21 | 22 | if test := PathConcat("a/", "/"); test != "a/" { 23 | t.Errorf("PathConcat failed: %s", test) 24 | } 25 | 26 | if test := PathConcat("/", "/b"); test != "/b" { 27 | t.Errorf("PathConcat failed: %s", test) 28 | } 29 | 30 | if test := PathConcat("/", "/"); test != "/" { 31 | t.Errorf("PathConcat failed: %s", test) 32 | } 33 | } 34 | 35 | func TestUriConcat(t *testing.T) { 36 | if test := UriConcat("localhost", "/"); test != "localhost" { 37 | t.Errorf("UriConcat failed: %s", test) 38 | } 39 | 40 | if test := UriConcat("localhost", ""); test != "localhost" { 41 | t.Errorf("UriConcat failed: %s", test) 42 | } 43 | 44 | if test := UriConcat("localhost/", ""); test != "localhost" { 45 | t.Errorf("UriConcat failed: %s", test) 46 | } 47 | 48 | if test := UriConcat("localhost/", "/"); test != "localhost" { 49 | t.Errorf("UriConcat failed: %s", test) 50 | } 51 | } 52 | 53 | func TestStripSlash(t *testing.T) { 54 | if test := StripSlash("abc/"); test != "abc" { 55 | t.Errorf("PathConcat failed: %s", test) 56 | } 57 | 58 | if test := StripSlash("abc"); test != "abc" { 59 | t.Errorf("PathConcat failed: %s", test) 60 | } 61 | 62 | if test := StripSlash("abc//"); test != "abc/" { 63 | t.Errorf("PathConcat failed: %s", test) 64 | } 65 | 66 | if test := StripSlash("/abc/"); test != "/abc" { 67 | t.Errorf("PathConcat failed: %s", test) 68 | } 69 | 70 | if test := StripSlash("/"); test != "" { 71 | t.Errorf("PathConcat failed: %s", test) 72 | } 73 | } 74 | 75 | func TestIsValidSlug(t *testing.T) { 76 | validTests := []string{"123", "abc", "123abc", "abc123", "abc_123", "abc-123", "-", "_", "hello.jpg"} 77 | for _, test := range validTests { 78 | if !IsValidSlug(test) { 79 | t.Errorf("IsValidSlug failed: %s", test) 80 | } 81 | } 82 | 83 | invalidTests := []string{"/123", "a/bc", "abc..xyz", "a?", "a:", ".", "..", "a\\bc", `a"bc`} 84 | for _, test := range invalidTests { 85 | if IsValidSlug(test) { 86 | t.Errorf("IsValidSlug failed: %s", test) 87 | } 88 | } 89 | } 90 | 91 | func TestPathFromUri(t *testing.T) { 92 | rootUri := "http://somewhere.com/" 93 | 94 | testA := "http://somewhere.com/hello/world" 95 | resultA := PathFromUri(rootUri, testA) 96 | if resultA != "hello/world" { 97 | t.Errorf("PathFromUri failed for: %s, %s", testA, resultA) 98 | } 99 | 100 | testB := "http://different.com/hello/world" 101 | resultB := PathFromUri(rootUri, testB) 102 | if resultB != testB { 103 | t.Errorf("PathFromUri failed for: %s, %s", testB, resultB) 104 | } 105 | } 106 | 107 | func TestDirBasePath(t *testing.T) { 108 | testA := []string{"a/b/c", "a/b", "c"} 109 | testB := []string{"a/b/c/", "a/b", "c"} 110 | testC := []string{"/", ".", "."} 111 | testD := []string{"", ".", "."} 112 | testE := []string{"a", ".", "a"} 113 | testF := []string{"a/", ".", "a"} 114 | testG := []string{"/a/", "/", "a"} 115 | testH := []string{"/a", "/", "a"} 116 | tests := [][]string{testA, testB, testC, testD, testE, testF, testG, testH} 117 | for _, test := range tests { 118 | dir, base := DirBasePath(test[0]) 119 | if dir != test[1] || base != test[2] { 120 | t.Errorf("DirBasePath failed for: %s %s %s", test[0], dir, base) 121 | } 122 | } 123 | } 124 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Hector Correa 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/README.md: -------------------------------------------------------------------------------- 1 | Utilities to work with RDF in Go 2 | 3 | A good place to start is with turtle_test.go 4 | 5 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/ast.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | type SubjectNode struct { 4 | value string 5 | predicates []*PredicateNode 6 | } 7 | 8 | type PredicateNode struct { 9 | value string 10 | objects []string 11 | } 12 | 13 | func NewSubjectNode(value string) SubjectNode { 14 | return SubjectNode{value: value} 15 | } 16 | 17 | func NewPredicateNode(value string) PredicateNode { 18 | return PredicateNode{value: value} 19 | } 20 | 21 | func (subject *SubjectNode) AddPredicate(value string) *PredicateNode { 22 | predicate := PredicateNode{value: value} 23 | subject.predicates = append(subject.predicates, &predicate) 24 | return &predicate 25 | } 26 | 27 | func (predicate *PredicateNode) AddObject(object string) { 28 | predicate.objects = append(predicate.objects, object) 29 | } 30 | 31 | func (subject *SubjectNode) Render() string { 32 | triples := "" 33 | for _, predicate := range subject.predicates { 34 | for _, object := range predicate.objects { 35 | triples += subject.value + " " + predicate.value + " " + object + " .\n" 36 | } 37 | } 38 | return triples 39 | } 40 | 41 | func (subject *SubjectNode) RenderTriples() []Triple { 42 | triples := []Triple{} 43 | for _, predicate := range subject.predicates { 44 | for _, object := range predicate.objects { 45 | triple := NewTriple(subject.value, predicate.value, object) 46 | triples = append(triples, triple) 47 | } 48 | } 49 | return triples 50 | } 51 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/graph.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "log" 4 | 5 | type RdfGraph []Triple 6 | 7 | func (triples RdfGraph) String() string { 8 | theString := "" 9 | for _, triple := range triples { 10 | theString += triple.StringLn() 11 | } 12 | return theString 13 | } 14 | 15 | func StringToGraph(theString, rootUri string) (RdfGraph, error) { 16 | var err error 17 | var graph RdfGraph 18 | if len(theString) > 0 { 19 | parser := NewTurtleParser(theString) 20 | err = parser.Parse() 21 | if err == nil { 22 | for _, triple := range parser.Triples() { 23 | triple.ReplaceBlankUri(rootUri) 24 | graph = append(graph, triple) 25 | } 26 | } 27 | } 28 | return graph, err 29 | } 30 | 31 | func (graph RdfGraph) IsRdfSource(subject string) bool { 32 | return graph.HasTriple(subject, "a", "<"+LdpRdfSourceUri+">") 33 | } 34 | 35 | func (graph RdfGraph) IsBasicContainer(subject string) bool { 36 | return graph.HasTriple(subject, "a", "<"+LdpBasicContainerUri+">") 37 | } 38 | 39 | func (graph RdfGraph) IsDirectContainer() bool { 40 | _, _, isDirectContainer := graph.GetDirectContainerInfo() 41 | return isDirectContainer 42 | } 43 | 44 | func (graph RdfGraph) GetDirectContainerInfo() (string, string, bool) { 45 | // TODO: validate only one instance of each these predicates is found on the graph 46 | // (perhas the validation should only be when adding/updating triples) 47 | membershipResource := "" 48 | hasMemberRelation := "" 49 | for _, triple := range graph { 50 | switch triple.predicate { 51 | case "<" + LdpMembershipResource + ">": 52 | membershipResource = triple.object 53 | case "<" + LdpHasMemberRelation + ">": 54 | hasMemberRelation = triple.object 55 | } 56 | if membershipResource != "" && hasMemberRelation != "" { 57 | return membershipResource, hasMemberRelation, true 58 | } 59 | } 60 | return "", "", false 61 | } 62 | 63 | func (graph RdfGraph) HasPredicate(subject, predicate string) bool { 64 | _, found := graph.FindPredicate(subject, predicate) 65 | return found 66 | } 67 | 68 | func (graph *RdfGraph) FindPredicate(subject, predicate string) (*Triple, bool) { 69 | return graph.findPredicate(subject, predicate, true) 70 | } 71 | 72 | func (graph *RdfGraph) findPredicate(subject, predicate string, recurr bool) (*Triple, bool) { 73 | for i, triple := range *graph { 74 | if triple.subject == subject && triple.predicate == predicate { 75 | // return a reference to the original triple 76 | return &(*graph)[i], true 77 | } 78 | } 79 | if recurr { 80 | // "a" is an alias for RdfType 81 | // look to see if we can find it by alias 82 | switch { 83 | case predicate == "a": 84 | return graph.findPredicate(subject, "<"+RdfTypeUri+">", false) 85 | case predicate == "<"+RdfTypeUri+">": 86 | return graph.findPredicate(subject, "a", false) 87 | } 88 | } 89 | return nil, false 90 | } 91 | 92 | func (graph *RdfGraph) findTriple(subject, predicate, object string, recurr bool) (*Triple, bool) { 93 | for i, t := range *graph { 94 | if t.subject == subject && t.predicate == predicate && t.object == object { 95 | // return a reference to the original triple 96 | return &(*graph)[i], true 97 | } 98 | } 99 | // "a" is an alias for RdfType 100 | // look to see if we can find it by alias 101 | if recurr { 102 | switch { 103 | case predicate == "a": 104 | return graph.findTriple(subject, "<"+RdfTypeUri+">", object, false) 105 | case predicate == "<"+RdfTypeUri+">": 106 | return graph.findTriple(subject, "a", object, false) 107 | } 108 | } 109 | return nil, false 110 | } 111 | 112 | func (graph *RdfGraph) DeleteTriple(subject, predicate, object string) bool { 113 | var newGraph RdfGraph 114 | deleted := false 115 | for _, triple := range *graph { 116 | // This does not handle the predicate a vs RdfType like find does. Should it? 117 | if triple.subject == subject && triple.predicate == predicate && triple.object == object { 118 | // don't add it to the new graph 119 | deleted = true 120 | } else { 121 | newGraph = append(newGraph, triple) 122 | } 123 | } 124 | 125 | if deleted { 126 | *graph = newGraph 127 | } 128 | return deleted 129 | } 130 | 131 | func (graph *RdfGraph) appendTriple(subject, predicate, object string) bool { 132 | t, found := graph.findTriple(subject, predicate, object, true) 133 | if found { 134 | if t.predicate == "a" && predicate != "a" { 135 | t.predicate = predicate 136 | log.Printf("**> replaced a with %s", predicate) 137 | } 138 | // nothing to do 139 | return false 140 | } 141 | 142 | // Append the new triple 143 | newTriple := NewTriple(subject, predicate, object) 144 | *graph = append(*graph, newTriple) 145 | return true 146 | } 147 | 148 | func (graph *RdfGraph) Append(newGraph RdfGraph) { 149 | for _, triple := range newGraph { 150 | graph.AppendTriple(triple) 151 | } 152 | } 153 | 154 | func (graph *RdfGraph) AppendTriple(t Triple) bool { 155 | return graph.appendTriple(t.subject, t.predicate, t.object) 156 | } 157 | 158 | func (graph *RdfGraph) AppendTripleStr(subject, predicate, object string) bool { 159 | return graph.appendTriple(subject, predicate, object) 160 | } 161 | 162 | func (graph RdfGraph) HasTriple(subject, predicate, object string) bool { 163 | _, found := graph.findTriple(subject, predicate, object, true) 164 | return found 165 | } 166 | 167 | func (graph RdfGraph) GetObject(subject, predicate string) (string, bool) { 168 | triple, found := graph.FindPredicate(subject, predicate) 169 | if found { 170 | return triple.object, true 171 | } 172 | return "", false 173 | } 174 | 175 | // Set the object for a subject/predicate 176 | // This is only useful for subject/predicates that can appear only once 177 | // on the graph. If a subject/predicate can appear multiple times, this 178 | // method will find and overwrite the first instance only. 179 | func (graph *RdfGraph) SetObject(subject, predicate, object string) { 180 | triple, found := graph.FindPredicate(subject, predicate) 181 | if found { 182 | triple.object = object 183 | return 184 | } 185 | 186 | // Add a new triple to the graph with the subject/predicate/object 187 | newTriple := NewTriple(subject, predicate, object) 188 | newGraph := RdfGraph{newTriple} 189 | graph.Append(newGraph) 190 | } 191 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/scanner.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "fmt" 4 | 5 | type Scanner struct { 6 | index int 7 | text string 8 | chars []rune 9 | length int 10 | row int 11 | col int 12 | } 13 | 14 | func NewScanner(text string) Scanner { 15 | // Convert the original string to an array of unicode runes. 16 | // This allows us to iterate on it as if it was an array 17 | // of ASCII chars even if there are Unicode characters on it 18 | // that use 2-4 bytes. 19 | chars := stringToRunes(text) 20 | scanner := Scanner{text: text, chars: chars, length: len(chars), row: 1, col: 1} 21 | return scanner 22 | } 23 | 24 | func (scanner Scanner) Index() int { 25 | return scanner.index 26 | } 27 | 28 | func (scanner Scanner) Substring(start, end int) string { 29 | return string(scanner.chars[start:end]) 30 | } 31 | 32 | func (scanner Scanner) SubstringFrom(start int) string { 33 | return string(scanner.chars[start:scanner.index]) 34 | } 35 | 36 | // Advances the index to the next character. 37 | func (scanner *Scanner) Advance() { 38 | if scanner.CanRead() { 39 | scanner.index++ 40 | if scanner.CanRead() && scanner.Char() == '\n' { 41 | scanner.row++ 42 | scanner.col = 1 43 | } else { 44 | scanner.col++ 45 | } 46 | } 47 | } 48 | 49 | func (scanner *Scanner) CanRead() bool { 50 | if scanner.length == 0 { 51 | return false 52 | } 53 | return scanner.index < scanner.length 54 | } 55 | 56 | func (scanner Scanner) Char() rune { 57 | return scanner.chars[scanner.index] 58 | } 59 | 60 | func (scanner Scanner) CharString() string { 61 | return string(scanner.chars[scanner.index]) 62 | } 63 | 64 | func (scanner *Scanner) Peek() (bool, rune) { 65 | if scanner.length > 0 && scanner.index < (scanner.length-1) { 66 | return true, scanner.chars[scanner.index+1] 67 | } 68 | return false, 0 69 | } 70 | 71 | func (scanner Scanner) Col() int { 72 | return scanner.col 73 | } 74 | 75 | func (scanner Scanner) Row() int { 76 | return scanner.row 77 | } 78 | 79 | func (scanner Scanner) Position() string { 80 | return fmt.Sprintf("(%d, %d)", scanner.row, scanner.col) 81 | } 82 | 83 | func stringToRunes(text string) []rune { 84 | var chars []rune 85 | for _, c := range text { 86 | chars = append(chars, c) 87 | } 88 | return chars 89 | } 90 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/tokenizer.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import ( 4 | "errors" 5 | "fmt" 6 | // "log" 7 | ) 8 | 9 | type Tokenizer struct { 10 | scanner Scanner 11 | } 12 | 13 | func NewTokenizer(text string) Tokenizer { 14 | return Tokenizer{scanner: NewScanner(text)} 15 | } 16 | 17 | func (tokenizer *Tokenizer) GetNextToken() (string, error) { 18 | var err error 19 | var value string 20 | 21 | tokenizer.AdvanceWhiteSpace() 22 | tokenizer.AdvanceComments() 23 | if !tokenizer.scanner.CanRead() { 24 | return "", nil 25 | } 26 | 27 | firstChar := tokenizer.scanner.Char() 28 | switch { 29 | case firstChar == '.': 30 | value = "." 31 | case firstChar == ',': 32 | value = "," 33 | case firstChar == ';': 34 | value = ";" 35 | case firstChar == '@': 36 | value, err = tokenizer.parseDirective() 37 | case firstChar == '<': 38 | value, err = tokenizer.parseUri() 39 | case firstChar == '"': 40 | value, err = tokenizer.parseString() 41 | case tokenizer.isNamespacedChar(): 42 | value = tokenizer.parseNamespacedValue() 43 | default: 44 | return "", tokenizer.Error("Invalid first character") 45 | } 46 | 47 | if err != nil { 48 | return "", err 49 | } 50 | 51 | tokenizer.scanner.Advance() 52 | return value, nil 53 | } 54 | 55 | // Advances the index to the beginning of the next triple. 56 | func (tokenizer *Tokenizer) AdvanceTriple() error { 57 | for tokenizer.CanRead() { 58 | if tokenizer.scanner.Char() == '.' { 59 | break 60 | } 61 | if tokenizer.isWhiteSpaceChar() { 62 | tokenizer.scanner.Advance() 63 | continue 64 | } 65 | return tokenizer.Error("Triple did not end with a period.") 66 | } 67 | tokenizer.scanner.Advance() 68 | return nil 69 | } 70 | 71 | func (tokenizer *Tokenizer) CanRead() bool { 72 | return tokenizer.scanner.CanRead() 73 | } 74 | 75 | func (tokenizer *Tokenizer) AdvanceWhiteSpace() { 76 | for tokenizer.CanRead() { 77 | if !tokenizer.isWhiteSpaceChar() { 78 | break 79 | } 80 | tokenizer.scanner.Advance() 81 | } 82 | } 83 | 84 | func (tokenizer *Tokenizer) AdvanceComments() { 85 | if !tokenizer.CanRead() || tokenizer.scanner.Char() != '#' { 86 | return 87 | } 88 | 89 | for tokenizer.CanRead() { 90 | if tokenizer.scanner.Char() == '\n' { 91 | tokenizer.AdvanceWhiteSpace() 92 | if !tokenizer.CanRead() || tokenizer.scanner.Char() != '#' { 93 | break 94 | } 95 | } 96 | tokenizer.scanner.Advance() 97 | } 98 | } 99 | 100 | func (tokenizer Tokenizer) isLanguageChar() bool { 101 | char := tokenizer.scanner.Char() 102 | return (char >= 'a' && char <= 'z') || 103 | (char >= 'A' && char <= 'Z') || 104 | (char == '-') 105 | } 106 | 107 | func (tokenizer Tokenizer) isDirectiveChar() bool { 108 | char := tokenizer.scanner.Char() 109 | return (char >= 'a' && char <= 'z') || 110 | (char >= 'A' && char <= 'Z') || 111 | (char == '@') 112 | } 113 | 114 | func (tokenizer Tokenizer) isNamespacedChar() bool { 115 | char := tokenizer.scanner.Char() 116 | return (char >= 'a' && char <= 'z') || 117 | (char >= 'A' && char <= 'Z') || 118 | (char >= '0' && char <= '9') || 119 | (char == ':') || 120 | (char == '_') 121 | } 122 | 123 | func (tokenizer Tokenizer) isUriChar() bool { 124 | char := tokenizer.scanner.Char() 125 | return (char >= 'a' && char <= 'z') || 126 | (char >= 'A' && char <= 'Z') || 127 | (char >= '0' && char <= '9') || 128 | (char == ':') || (char == '/') || 129 | (char == '%') || (char == '#') || 130 | (char == '+') || (char == '-') || 131 | (char == '.') || (char == '_') 132 | } 133 | 134 | func (tokenizer Tokenizer) isWhiteSpaceChar() bool { 135 | char := tokenizer.scanner.Char() 136 | return char == ' ' || char == '\t' || char == '\n' || char == '\r' 137 | } 138 | 139 | // Extracts a value in the form xx:yy or xx 140 | func (tokenizer *Tokenizer) parseNamespacedValue() string { 141 | start := tokenizer.scanner.Index() 142 | tokenizer.scanner.Advance() 143 | for tokenizer.CanRead() { 144 | if tokenizer.isNamespacedChar() { 145 | tokenizer.scanner.Advance() 146 | continue 147 | } else { 148 | break 149 | } 150 | } 151 | return tokenizer.scanner.SubstringFrom(start) 152 | } 153 | 154 | func (tokenizer *Tokenizer) parseLanguage() string { 155 | start := tokenizer.scanner.Index() 156 | tokenizer.scanner.Advance() 157 | for tokenizer.CanRead() { 158 | if tokenizer.isLanguageChar() { 159 | tokenizer.scanner.Advance() 160 | } else { 161 | break 162 | } 163 | } 164 | // Should be indicate error if the language is empty? 165 | return tokenizer.scanner.SubstringFrom(start) 166 | } 167 | 168 | // Extracts a value in the form @hello 169 | func (tokenizer *Tokenizer) parseDirective() (string, error) { 170 | start := tokenizer.scanner.Index() 171 | tokenizer.scanner.Advance() 172 | for tokenizer.CanRead() { 173 | if tokenizer.isDirectiveChar() { 174 | tokenizer.scanner.Advance() 175 | } else { 176 | break 177 | } 178 | } 179 | 180 | directive := tokenizer.scanner.SubstringFrom(start) 181 | if directive == "" { 182 | return "", tokenizer.Error("Empty directive detected") 183 | } 184 | 185 | return directive, nil 186 | } 187 | 188 | // Extracts a value in quotes, for example 189 | // "hello" 190 | // "hello \"world\"" 191 | // "hello"@en-us 192 | // "hello"^^ 193 | func (tokenizer *Tokenizer) parseString() (string, error) { 194 | start := tokenizer.scanner.Index() 195 | lastChar := tokenizer.scanner.Char() 196 | tokenizer.scanner.Advance() 197 | for tokenizer.CanRead() { 198 | if tokenizer.scanner.Char() == '"' { 199 | if lastChar == '\\' { 200 | lastChar = tokenizer.scanner.Char() 201 | tokenizer.scanner.Advance() 202 | continue 203 | } 204 | str := tokenizer.scanner.Substring(start, tokenizer.scanner.Index()+1) 205 | lang := "" 206 | datatype := "" 207 | canPeek, nextChar := tokenizer.scanner.Peek() 208 | var err error 209 | if canPeek { 210 | switch nextChar { 211 | case '@': 212 | tokenizer.scanner.Advance() 213 | lang = tokenizer.parseLanguage() 214 | str += lang 215 | case '^': 216 | tokenizer.scanner.Advance() 217 | datatype, err = tokenizer.parseType() 218 | str += datatype 219 | } 220 | } 221 | return str, err 222 | } 223 | lastChar = tokenizer.scanner.Char() 224 | tokenizer.scanner.Advance() 225 | } 226 | return "", tokenizer.Error("String did not end with \"") 227 | } 228 | 229 | func (tokenizer *Tokenizer) parseType() (string, error) { 230 | canPeek, nextChar := tokenizer.scanner.Peek() 231 | if !canPeek || nextChar != '^' { 232 | return "", tokenizer.Error("Invalid type delimiter") 233 | } 234 | 235 | tokenizer.scanner.Advance() 236 | canPeek, nextChar = tokenizer.scanner.Peek() 237 | if !canPeek || nextChar != '<' { 238 | return "", tokenizer.Error("Invalid URI in type delimiter") 239 | } 240 | 241 | tokenizer.scanner.Advance() 242 | uri, err := tokenizer.parseUri() 243 | return "^^" + uri, err 244 | } 245 | 246 | // Extracts an URI in the form 247 | func (tokenizer *Tokenizer) parseUri() (string, error) { 248 | start := tokenizer.scanner.Index() 249 | tokenizer.scanner.Advance() 250 | for tokenizer.CanRead() { 251 | if tokenizer.scanner.Char() == '>' { 252 | uri := tokenizer.scanner.Substring(start, tokenizer.scanner.Index()+1) 253 | return uri, nil 254 | } 255 | if !tokenizer.isUriChar() { 256 | return "", tokenizer.Error("Invalid character in URI") 257 | } 258 | tokenizer.scanner.Advance() 259 | } 260 | return "", tokenizer.Error("URI did not end with >") 261 | } 262 | 263 | func (tokenizer *Tokenizer) Error(message string) error { 264 | lastChar := "" 265 | if tokenizer.CanRead() { 266 | lastChar = tokenizer.scanner.CharString() 267 | } 268 | errorMsg := fmt.Sprintf("%s. Character (%s) at %s.", message, lastChar, tokenizer.scanner.Position()) 269 | return errors.New(errorMsg) 270 | } 271 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/triple.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | import "fmt" 4 | 5 | type Triple struct { 6 | subject string 7 | predicate string 8 | object string 9 | } 10 | 11 | func NewTriple(subject, predicate, object string) Triple { 12 | return Triple{subject: subject, predicate: predicate, object: object} 13 | } 14 | 15 | func (t Triple) String() string { 16 | return fmt.Sprintf("%s %s %s .", t.subject, t.predicate, t.object) 17 | } 18 | 19 | func (t Triple) StringLn() string { 20 | return fmt.Sprintf("%s %s %s .\n", t.subject, t.predicate, t.object) 21 | } 22 | 23 | func (t Triple) Predicate() string { 24 | return t.predicate 25 | } 26 | 27 | func (t Triple) Object() string { 28 | return t.object 29 | } 30 | 31 | func (t Triple) Is(predicate string) bool { 32 | return t.predicate == predicate 33 | } 34 | 35 | func (triple *Triple) ReplaceBlankUri(blank string) { 36 | if triple.subject == "<>" { 37 | triple.subject = blank 38 | } 39 | if triple.predicate == "<>" { 40 | triple.predicate = blank 41 | } 42 | if triple.object == "<>" { 43 | triple.object = blank 44 | } 45 | } 46 | 47 | func StringToTriples(text, blank string) ([]Triple, error) { 48 | var triples []Triple 49 | parser := NewTurtleParser(text) 50 | err := parser.Parse() 51 | if err != nil { 52 | return triples, err 53 | } 54 | for _, triple := range parser.Triples() { 55 | triple.ReplaceBlankUri(blank) 56 | } 57 | return triples, nil 58 | } 59 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/turtle.go: -------------------------------------------------------------------------------- 1 | // A basic RDF Turtle parser 2 | // http://www.w3.org/TR/turtle/ 3 | // 4 | // TurtleParser is the parser which uses Tokenizer to 5 | // break down the text into meaningful tokens (URIs, strings, 6 | // separators, et cetera.) 7 | 8 | // Tokenizer in turn uses Scanner to handle the character by 9 | // character operations. 10 | // 11 | // TurtleParser uses a tree-like structure (via SubjectNode 12 | // and PredicateNode) to keep track of the subject, predicate, 13 | // and object values as they are parsed. This structure allows 14 | // us to parse multi-predicate (;) and multi-object (,) triples. 15 | // 16 | // Sample usage: 17 | // parser := NewTurtleParser(" , ; .") 18 | // err := parser.Parse() 19 | // for i, triple := range parser.Triples() { 20 | // log.Printf("Triple %d: %s", i, triple) 21 | // } 22 | // Gives: 23 | // Triple 0: . 24 | // Triple 1: . 25 | // Triple 2: . 26 | // 27 | package rdf 28 | 29 | import ( 30 | "errors" 31 | // "log" 32 | "strings" 33 | ) 34 | 35 | type Directive struct { 36 | name string 37 | value string 38 | } 39 | 40 | type TurtleParser struct { 41 | tokenizer Tokenizer 42 | triples []Triple 43 | directives []Directive 44 | } 45 | 46 | func NewTurtleParser(text string) TurtleParser { 47 | tokenizer := NewTokenizer(text) 48 | parser := TurtleParser{tokenizer: tokenizer} 49 | return parser 50 | } 51 | 52 | func (parser *TurtleParser) Parse() error { 53 | for parser.tokenizer.CanRead() { 54 | err := parser.parseNextTriples() 55 | if err != nil { 56 | return err 57 | } 58 | parser.tokenizer.AdvanceWhiteSpace() 59 | } 60 | 61 | parser.applyBaseDirective() 62 | return nil 63 | } 64 | 65 | func (parser TurtleParser) Triples() []Triple { 66 | return parser.triples 67 | } 68 | 69 | func (parser *TurtleParser) applyBaseDirective() { 70 | if len(parser.directives) == 0 { 71 | return 72 | } 73 | 74 | if parser.directives[0].name != "@base" { 75 | // unknown directive 76 | return 77 | } 78 | 79 | base := parser.directives[0] 80 | for i, triple := range parser.triples { 81 | if triple.subject == "<>" { 82 | parser.triples[i].subject = base.value 83 | } 84 | if triple.object == "<>" { 85 | parser.triples[i].object = base.value 86 | } 87 | } 88 | } 89 | 90 | func (parser *TurtleParser) parseNextTriples() error { 91 | var err error 92 | var token string 93 | 94 | for err == nil && parser.tokenizer.CanRead() { 95 | token, err = parser.tokenizer.GetNextToken() 96 | if err != nil || token == "" { 97 | break 98 | } 99 | 100 | isDirective := strings.HasPrefix(token, "@") 101 | if isDirective { 102 | err = parser.parseNextDirective(token) 103 | continue 104 | } 105 | 106 | // triples 107 | subject := NewSubjectNode(token) 108 | err = parser.parsePredicates(&subject) 109 | if err == nil { 110 | for _, triple := range subject.RenderTriples() { 111 | parser.triples = append(parser.triples, triple) 112 | } 113 | } 114 | 115 | } 116 | return err 117 | } 118 | 119 | func (parser *TurtleParser) parseNextDirective(name string) error { 120 | if !parser.tokenizer.CanRead() { 121 | return errors.New("No value found for directive (" + name + ")") 122 | } 123 | 124 | value, err := parser.tokenizer.GetNextToken() 125 | if err != nil { 126 | return err 127 | } 128 | 129 | token, err := parser.tokenizer.GetNextToken() 130 | if token != "." { 131 | return errors.New("Could not find end of directive (" + name + ")") 132 | } 133 | 134 | if err == nil { 135 | directive := Directive{name: name, value: value} 136 | parser.directives = append(parser.directives, directive) 137 | } 138 | return err 139 | } 140 | 141 | func (parser *TurtleParser) parsePredicates(subject *SubjectNode) error { 142 | var err error 143 | var token string 144 | 145 | for err == nil && parser.tokenizer.CanRead() { 146 | token, err = parser.tokenizer.GetNextToken() 147 | if err != nil || token == "." { 148 | // we are done 149 | break 150 | } 151 | predicate := subject.AddPredicate(token) 152 | token, err = parser.parseObjects(predicate) 153 | if err != nil { 154 | break 155 | } else if token == "." { 156 | // we are done, next triple will be for a different subject 157 | break 158 | } else if token == ";" { 159 | // next triple will be for the same subject 160 | continue 161 | } else { 162 | err = errors.New("Unexpected token parsing predicates (" + token + ")") 163 | } 164 | } 165 | return err 166 | } 167 | 168 | func (parser *TurtleParser) parseObjects(predicate *PredicateNode) (string, error) { 169 | var err error 170 | var token string 171 | for parser.tokenizer.CanRead() { 172 | token, err = parser.tokenizer.GetNextToken() 173 | if err != nil || token == "." || token == ";" { 174 | // we are done 175 | break 176 | } else if token == "," { 177 | // the next token will be for the same 178 | // subject + predicate 179 | continue 180 | } else { 181 | // it's a object, add it to the predicate 182 | predicate.AddObject(token) 183 | } 184 | } 185 | return token, err 186 | } 187 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/uris.go: -------------------------------------------------------------------------------- 1 | package rdf 2 | 3 | const ( 4 | RdfTypeUri = "http://www.w3.org/1999/02/22-rdf-syntax-ns#type" 5 | ) 6 | 7 | const ( 8 | LdpResourceUri = "http://www.w3.org/ns/ldp#Resource" 9 | LdpRdfSourceUri = "http://www.w3.org/ns/ldp#RDFSource" 10 | LdpNonRdfSourceUri = "http://www.w3.org/ns/ldp#NonRDFSource" 11 | LdpContainerUri = "http://www.w3.org/ns/ldp#Container" 12 | LdpBasicContainerUri = "http://www.w3.org/ns/ldp#BasicContainer" 13 | LdpDirectContainerUri = "http://www.w3.org/ns/ldp#DirectContainer" 14 | LdpContainsUri = "http://www.w3.org/ns/ldp#contains" 15 | LdpInsertedContentRelationUri = "http://www.w3.org/ns/ldp#insertedContentRelation" 16 | LdpMemberSubjectUri = "http://www.w3.org/ns/ldp#MemberSubject" 17 | LdpMembershipResource = "http://www.w3.org/ns/ldp#membershipResource" 18 | LdpHasMemberRelation = "http://www.w3.org/ns/ldp#hasMemberRelation" 19 | LdpConstrainedBy = "http://www.w3.org/ns/ldp#constrainedBy" 20 | ) 21 | 22 | const ( 23 | // HTTP header links 24 | LdpResourceLink = "<" + LdpResourceUri + ">; rel=\"type\"" 25 | LdpNonRdfSourceLink = "<" + LdpNonRdfSourceUri + ">; rel=\"type\"" 26 | LdpContainerLink = "<" + LdpContainerUri + ">; rel=\"type\"" 27 | LdpBasicContainerLink = "<" + LdpBasicContainerUri + ">; rel=\"type\"" 28 | LdpDirectContainerLink = "<" + LdpDirectContainerUri + ">; rel=\"type\"" 29 | ) 30 | 31 | const ( 32 | DcTitleUri = "http://purl.org/dc/terms/title" 33 | DcCreatedUri = "http://purl.org/dc/terms/created" 34 | ) 35 | 36 | const ( 37 | ServerETagUri = "http://hectorcorrea.com/ldpserver/ns/etag" 38 | ServerContentTypeUri = "http://hectorcorrea.com/ldpserver/ns/contentType" 39 | ) 40 | const ( 41 | TurtleContentType = "text/turtle" 42 | ) 43 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/w3cbase.nt: -------------------------------------------------------------------------------- 1 | @base . 2 | <> a , , ; 3 | 4 | "modified" ; 5 | 6 | "2015-10-17T23_34_00-04_00" ; 7 | 8 | "2015-10-17T23:34:00-04:00" ; 9 | 10 | "This is a new entry" ; 11 | 12 | , , , , , , , , , . 13 | -------------------------------------------------------------------------------- /vendor/github.com/hectorcorrea/rdf/w3ctest.nt: -------------------------------------------------------------------------------- 1 | # Source: http://www.w3.org/2000/10/rdf-tests/rdfcore/ntriples/test.nt 2 | # 3 | # 4 | # Copyright World Wide Web Consortium, (Massachusetts Institute of 5 | # Technology, Institut National de Recherche en Informatique et en 6 | # Automatique, Keio University). 7 | # 8 | # All Rights Reserved. 9 | # 10 | # Please see the full Copyright clause at 11 | # 12 | # 13 | # Test file with a variety of legal N-Triples 14 | # 15 | # Dave Beckett - http://purl.org/net/dajobe/ 16 | # 17 | # $Id: test.nt,v 1.7 2003/10/06 15:52:19 dbeckett2 Exp $ 18 | # 19 | ##################################################################### 20 | 21 | # comment lines 22 | # comment line after whitespace 23 | # empty blank line, then one with spaces and tabs 24 | 25 | 26 | . 27 | _:anon . 28 | _:anon . 29 | # spaces and tabs throughout: 30 | . 31 | 32 | # line ending with CR NL (ASCII 13, ASCII 10) 33 | . 34 | 35 | # 2 statement lines separated by single CR (ASCII 10) 36 | . 37 | . 38 | 39 | 40 | # All literal escapes 41 | "simple literal" . 42 | "backslash:\\" . 43 | "dquote:\"" . 44 | "newline:\n" . 45 | "return\r" . 46 | "tab:\t" . 47 | 48 | # Space is optional before final . 49 | . 50 | "x". 51 | _:anon. 52 | 53 | # \u and \U escapes 54 | # latin small letter e with acute symbol \u00E9 - 3 UTF-8 bytes #xC3 #A9 55 | "\u00E9" . 56 | # Euro symbol \u20ac - 3 UTF-8 bytes #xE2 #x82 #xAC 57 | "\u20AC" . 58 | # resource18 test removed 59 | # resource19 test removed 60 | # resource20 test removed 61 | 62 | # XML Literals as Datatyped Literals 63 | ""^^ . 64 | " "^^ . 65 | "x"^^ . 66 | "\""^^ . 67 | ""^^ . 68 | "a "^^ . 69 | "a c"^^ . 70 | "a\n\nc"^^ . 71 | "chat"^^ . 72 | # resource28 test removed 2003-08-03 73 | # resource29 test removed 2003-08-03 74 | 75 | # Plain literals with languages 76 | "chat"@fr . 77 | "chat"@en . 78 | 79 | # Typed Literals 80 | "abc"^^ . 81 | # resource33 test removed 2003-08-03 82 | -------------------------------------------------------------------------------- /web/delete.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "net/http" 5 | ) 6 | 7 | func handleDelete(resp http.ResponseWriter, req *http.Request) { 8 | path := safePath(req.URL.Path) 9 | err := theServer.DeleteNode(path) 10 | if err != nil { 11 | handleCommonErrors(resp, req, err) 12 | return 13 | } 14 | 15 | resp.WriteHeader(http.StatusOK) 16 | } 17 | -------------------------------------------------------------------------------- /web/get.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "fmt" 5 | "ldpserver/ldp" 6 | "log" 7 | "net/http" 8 | ) 9 | 10 | func handleGet(includeBody bool, resp http.ResponseWriter, req *http.Request) { 11 | var node ldp.Node 12 | var err error 13 | var pref ldp.PreferTriples 14 | 15 | path := safePath(req.URL.Path) 16 | 17 | if includeBody { 18 | log.Printf("GET request %s", path) 19 | pref = ldp.PreferTriples{ 20 | Membership: isPreferMembership(req.Header), 21 | MinimalContainer: isPreferMinimalContainer(req.Header)} 22 | node, err = theServer.GetNode(path, pref) 23 | } else { 24 | log.Printf("HEAD request %s", path) 25 | node, err = theServer.GetHead(path) 26 | } 27 | 28 | if err != nil { 29 | handleCommonErrors(resp, req, err) 30 | return 31 | } 32 | 33 | if etag := requestIfNoneMatch(req.Header); etag != "" { 34 | if etag == node.Etag() { 35 | resp.WriteHeader(http.StatusNotModified) 36 | return 37 | } 38 | } 39 | 40 | if !node.IsRdf() && isNonRdfMetadataOnlyRequest(req) { 41 | setResponseHeadersMetadataOnly(resp, node) 42 | fmt.Fprint(resp, node.Metadata()) 43 | return 44 | } 45 | 46 | setResponseHeaders(resp, node) 47 | fmt.Fprint(resp, node.ContentPref(pref)) 48 | } 49 | -------------------------------------------------------------------------------- /web/misc.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "github.com/hectorcorrea/rdf" 5 | "ldpserver/ldp" 6 | "log" 7 | "net/http" 8 | "strings" 9 | ) 10 | 11 | func handleCommonErrors(resp http.ResponseWriter, req *http.Request, err error) { 12 | if err == nil { 13 | panic("No error to handle") 14 | } 15 | 16 | if err == ldp.NodeNotFoundError { 17 | log.Printf("Not found %s", req.URL.Path) 18 | http.NotFound(resp, req) 19 | return 20 | } 21 | 22 | log.Printf("Error %s", err) 23 | http.Error(resp, "Error processing request", http.StatusInternalServerError) 24 | } 25 | 26 | func isRdfRequest(header http.Header) bool { 27 | contentType := requestContentType(header) 28 | if contentType == "" { 29 | return true 30 | } 31 | return strings.HasPrefix(contentType, rdf.TurtleContentType) 32 | } 33 | 34 | func isNonRdfRequest(header http.Header) bool { 35 | return !isRdfRequest(header) 36 | } 37 | 38 | func safePath(rawPath string) string { 39 | if strings.HasSuffix(rawPath, "/") { 40 | return rawPath 41 | } 42 | return rawPath + "/" 43 | } 44 | 45 | func setResponseHeaders(resp http.ResponseWriter, node ldp.Node) { 46 | for key, header := range node.Headers() { 47 | for _, value := range header { 48 | resp.Header().Add(key, value) 49 | } 50 | } 51 | } 52 | 53 | func setResponseHeadersMetadataOnly(resp http.ResponseWriter, node ldp.Node) { 54 | resp.Header().Add("Content-Type", rdf.TurtleContentType) 55 | resp.Header().Add("Allow", "GET") 56 | resp.Header().Add("Allow", "HEAD") 57 | resp.Header().Add("Etag", node.Etag()) 58 | } 59 | 60 | func requestSlug(header http.Header) string { 61 | return headerValue(header, "Slug") 62 | } 63 | 64 | func requestContentType(header http.Header) string { 65 | value := headerValue(header, "Content-Type") 66 | // TODO: remove this horrible hack 67 | if strings.HasSuffix(value, "; charset=ISO-8859-1") { 68 | return strings.Replace(value, "; charset=ISO-8859-1", "", 1) 69 | } 70 | return value 71 | } 72 | 73 | func requestIfNoneMatch(header http.Header) string { 74 | return headerValue(header, "If-None-Match") 75 | } 76 | 77 | func requestIfMatch(header http.Header) string { 78 | return headerValue(header, "If-Match") 79 | } 80 | 81 | func isPreferMembership(header http.Header) bool { 82 | // TODO: A more strict parsing of the header to make sure is in the form 83 | // return=representation; include="http://www.w3.org/ns/ldp#PreferMembership" 84 | // and handle more than one include URI and the existance of omit URIs. 85 | prefHeader := headerValue(header, "Prefer") 86 | isInclude := strings.Contains(prefHeader, "include=") 87 | if isInclude && strings.Contains(prefHeader, "http://www.w3.org/ns/ldp#PreferMembership") { 88 | return true 89 | } 90 | return false 91 | } 92 | 93 | func isPreferMinimalContainer(header http.Header) bool { 94 | // TODO: ditto 95 | prefHeader := headerValue(header, "Prefer") 96 | isInclude := strings.Contains(prefHeader, "include=") 97 | if isInclude && strings.Contains(prefHeader, "http://www.w3.org/ns/ldp#PreferMinimalContainer") { 98 | return true 99 | } 100 | isOmit := strings.Contains(prefHeader, "omit=") 101 | if isOmit && strings.Contains(prefHeader, "http://www.w3.org/ns/ldp#PreferContainment") { 102 | return true 103 | } 104 | return false 105 | } 106 | 107 | func headerValue(header http.Header, name string) string { 108 | for _, value := range header[name] { 109 | return value 110 | } 111 | return "" 112 | } 113 | 114 | func isNonRdfMetadataOnlyRequest(req *http.Request) bool { 115 | return req.URL.Query().Get("metadata") == "yes" 116 | } 117 | 118 | func defaultNonRdfTriples(header http.Header) string { 119 | triples := "" 120 | contentType := requestContentType(header) 121 | if contentType != "" { 122 | triples = "<> <" + rdf.ServerContentTypeUri + "> \"" + contentType + "\" ." 123 | } 124 | // TODO: We should also try to read the file name from the header (if available) 125 | return triples 126 | } 127 | 128 | func logHeaders(req *http.Request) { 129 | log.Printf("==> HTTP Headers %s %s", req.Method, req.URL.Path) 130 | for header, values := range req.Header { 131 | for _, value := range values { 132 | log.Printf("\t\t %s %s", header, value) 133 | } 134 | } 135 | } 136 | 137 | func logReqError(req *http.Request, message string, code int) { 138 | log.Printf("Error %d on %s %s: %s", code, req.Method, req.URL.Path, message) 139 | } 140 | -------------------------------------------------------------------------------- /web/options.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "ldpserver/ldp" 5 | "net/http" 6 | ) 7 | 8 | func handleOptions(resp http.ResponseWriter, req *http.Request) { 9 | path := safePath(req.URL.Path) 10 | node, err := theServer.GetNode(path, ldp.PreferTriples{}) 11 | if err != nil { 12 | handleCommonErrors(resp, req, err) 13 | return 14 | } 15 | setResponseHeaders(resp, node) 16 | } 17 | -------------------------------------------------------------------------------- /web/patch.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "fmt" 5 | "ldpserver/fileio" 6 | "log" 7 | "net/http" 8 | ) 9 | 10 | func handlePatch(resp http.ResponseWriter, req *http.Request) { 11 | if !isRdfRequest(req.Header) { 12 | errorMsg := fmt.Sprintf("Invalid Content-Type (%s) received", requestContentType(req.Header)) 13 | logReqError(req, errorMsg, http.StatusBadRequest) 14 | http.Error(resp, errorMsg, http.StatusBadRequest) 15 | return 16 | } 17 | 18 | path := safePath(req.URL.Path) 19 | log.Printf("Patching %s", path) 20 | 21 | triples, err := fileio.ReaderToString(req.Body) 22 | if err != nil { 23 | errorMsg := fmt.Sprintf("Invalid body received. Error: %s", err.Error()) 24 | logReqError(req, errorMsg, http.StatusBadRequest) 25 | http.Error(resp, errorMsg, http.StatusBadRequest) 26 | return 27 | } 28 | 29 | err = theServer.PatchNode(path, triples) 30 | if err != nil { 31 | handleCommonErrors(resp, req, err) 32 | return 33 | } 34 | 35 | fmt.Fprint(resp, req.URL.Path) 36 | } 37 | -------------------------------------------------------------------------------- /web/post.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "ldpserver/fileio" 5 | "ldpserver/ldp" 6 | "log" 7 | "net/http" 8 | ) 9 | 10 | func handlePost(resp http.ResponseWriter, req *http.Request) { 11 | slug := requestSlug(req.Header) 12 | path := safePath(req.URL.Path) 13 | node, err := doPost(resp, req, path, slug) 14 | if err != nil { 15 | handlePostPutError(resp, req, err) 16 | return 17 | } 18 | 19 | handlePostPutSuccess(resp, node) 20 | } 21 | 22 | func doPost(resp http.ResponseWriter, req *http.Request, path string, slug string) (ldp.Node, error) { 23 | if isNonRdfRequest(req.Header) { 24 | log.Printf("Creating Non-RDF Source at %s", path) 25 | triples := defaultNonRdfTriples(req.Header) 26 | return theServer.CreateNonRdfSource(req.Body, path, slug, triples) 27 | } 28 | 29 | log.Printf("Creating RDF Source %s at %s", slug, path) 30 | triples, err := fileio.ReaderToString(req.Body) 31 | if err != nil { 32 | return ldp.Node{}, err 33 | } 34 | return theServer.CreateRdfSource(triples, path, slug) 35 | } 36 | -------------------------------------------------------------------------------- /web/postput.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "fmt" 5 | "github.com/hectorcorrea/rdf" 6 | "ldpserver/ldp" 7 | "log" 8 | "net/http" 9 | ) 10 | 11 | func handlePostPutSuccess(resp http.ResponseWriter, node ldp.Node) { 12 | setResponseHeaders(resp, node) 13 | location := node.Uri() 14 | resp.Header().Add("Location", location) 15 | resp.WriteHeader(http.StatusCreated) 16 | log.Printf("Resource created at %s", node.Uri()) 17 | fmt.Fprint(resp, node.Uri()) 18 | } 19 | 20 | func handlePostPutError(resp http.ResponseWriter, req *http.Request, err error) { 21 | path := req.URL.Path 22 | slug := requestSlug(req.Header) 23 | msg := err.Error() 24 | code := http.StatusBadRequest 25 | 26 | switch err { 27 | case ldp.NodeNotFoundError: 28 | msg = "Parent container [" + path + "] not found." 29 | code = http.StatusNotFound 30 | case ldp.DuplicateNodeError: 31 | msg = fmt.Sprintf("Resource already exists. Path: %s Slug: %s", path, slug) 32 | code = http.StatusConflict 33 | case ldp.EtagMissingError: 34 | msg = fmt.Sprintf("Etag missing. Path: %s Slug: %s", path, slug) 35 | code = 428 // precondition required 36 | case ldp.EtagMismatchError: 37 | msg = fmt.Sprintf("Etag mismatch. Path: %s Slug: %s", path, slug) 38 | code = http.StatusPreconditionFailed 39 | case ldp.ServerManagedPropertyError: 40 | msg = fmt.Sprintf("Cannot overwrite server-managed property") 41 | code = http.StatusConflict 42 | constrainedBy := "<" + req.URL.Path + ">; rel=\"" + rdf.LdpConstrainedBy + "\"" 43 | resp.Header().Add("Link", constrainedBy) 44 | } 45 | 46 | logReqError(req, msg, code) 47 | http.Error(resp, msg, code) 48 | } 49 | -------------------------------------------------------------------------------- /web/put.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "errors" 5 | "ldpserver/fileio" 6 | "ldpserver/ldp" 7 | "ldpserver/util" 8 | "log" 9 | "net/http" 10 | ) 11 | 12 | func handlePut(resp http.ResponseWriter, req *http.Request) { 13 | node, err := doPut(resp, req) 14 | if err != nil { 15 | handlePostPutError(resp, req, err) 16 | return 17 | } 18 | 19 | handlePostPutSuccess(resp, node) 20 | } 21 | 22 | func doPut(resp http.ResponseWriter, req *http.Request) (ldp.Node, error) { 23 | if requestSlug(req.Header) != "" { 24 | return ldp.Node{}, errors.New("Slug is not accepted on PUT requests") 25 | } 26 | 27 | etag := requestIfMatch(req.Header) 28 | 29 | if isNonRdfRequest(req.Header) { 30 | path := req.URL.Path 31 | log.Printf("Creating Non-RDF Source at %s", path) 32 | triples := defaultNonRdfTriples(req.Header) 33 | return theServer.ReplaceNonRdfSource(req.Body, path, etag, triples) 34 | } 35 | 36 | path, slug := util.DirBasePath(safePath(req.URL.Path)) 37 | log.Printf("Creating RDF Source %s at %s", slug, path) 38 | triples, err := fileio.ReaderToString(req.Body) 39 | if err != nil { 40 | return ldp.Node{}, errors.New("Invalid request body received") 41 | } 42 | return theServer.ReplaceRdfSource(triples, path, slug, etag) 43 | } 44 | -------------------------------------------------------------------------------- /web/web.go: -------------------------------------------------------------------------------- 1 | package web 2 | 3 | import ( 4 | "ldpserver/server" 5 | "log" 6 | "net/http" 7 | ) 8 | 9 | var theServer server.Server 10 | 11 | func Start(address, dataPath string) { 12 | theServer = server.NewServer("http://"+address, dataPath) 13 | log.Printf("Listening for requests at %s\n", "http://"+address) 14 | log.Printf("Data folder: %s\n", dataPath) 15 | http.HandleFunc("/", homePage) 16 | err := http.ListenAndServe(address, nil) 17 | if err != nil { 18 | log.Fatal("Failed to start the web server: ", err) 19 | } 20 | } 21 | 22 | func homePage(resp http.ResponseWriter, req *http.Request) { 23 | logHeaders(req) 24 | if req.Method == "GET" { 25 | handleGet(true, resp, req) 26 | } else if req.Method == "HEAD" { 27 | handleGet(false, resp, req) 28 | } else if req.Method == "POST" { 29 | handlePost(resp, req) 30 | } else if req.Method == "PUT" { 31 | handlePut(resp, req) 32 | } else if req.Method == "PATCH" { 33 | handlePatch(resp, req) 34 | } else if req.Method == "OPTIONS" { 35 | handleOptions(resp, req) 36 | } else if req.Method == "xxDELETE" { 37 | handleDelete(resp, req) 38 | } else { 39 | log.Printf("Unknown request type %s", req.Method) 40 | } 41 | } 42 | --------------------------------------------------------------------------------