Skip to content

Commit

Permalink
Disable paid content delivery
Browse files Browse the repository at this point in the history
  • Loading branch information
anbsky committed Mar 19, 2019
1 parent 0a86193 commit 0e86bf8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 4 deletions.
11 changes: 10 additions & 1 deletion player/player.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,18 +126,27 @@ func (s *reflectedStream) resolve(client *ljsonrpc.Client) error {
if s.URI == "" {
return errors.Err("stream URI is not set")
}

response, err := client.Resolve(s.URI)
if err != nil {
return err
}
source := (*response)[s.URI].Claim.Value.Stream.Source

stream := (*response)[s.URI].Claim.Value.Stream
if stream.Metadata.Fee != nil && (*stream.Metadata.Fee.Amount > 0) {
return errors.Err("paid stream")
}

source := stream.Source
s.SDHash = source.Source
s.ContentType = source.GetContentType()

monitor.Logger.WithFields(log.Fields{
"sd_hash": fmt.Sprintf("%s", s.SDHash),
"uri": s.URI,
"content_type": s.ContentType,
}).Info("resolved uri")

return nil
}

Expand Down
10 changes: 7 additions & 3 deletions routes/handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ func Proxy(w http.ResponseWriter, req *http.Request) {
func stream(uri string, w http.ResponseWriter, req *http.Request) {
err := player.PlayURI(uri, w, req)
// Only output error if player has not pushed anything to the client yet
if err != nil && w.Header().Get("Content-Type") == "" {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "%v", err)
if err != nil {
if err.Error() == "paid stream" {
w.WriteHeader(http.StatusPaymentRequired)
} else if w.Header().Get("Content-Type") == "" {
w.WriteHeader(http.StatusServiceUnavailable)
fmt.Fprintf(w, "%v", err)
}
}
}

Expand Down
13 changes: 13 additions & 0 deletions routes/handlers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package routes
import (
"bytes"
"encoding/json"
"io"
"net/http"
"net/http/httptest"
"reflect"
Expand Down Expand Up @@ -175,3 +176,15 @@ func TestProxy(t *testing.T) {
decode(parsedResponse.Result, &resolveResponse)
assert.Equal(t, "what", resolveResponse["what"].Claim.Name)
}

func TestContentByURL_NoPayment(t *testing.T) {
req, _ := http.NewRequest("GET", "http://localhost:40080/content/url", nil)
req.URL.RawQuery = "pra-onde-vamos-em-2018-seguran-a-online#3a508cce1fda3b7c1a2502cb4323141d40a2cf0b"
req.Header.Add("Range", "bytes=0-1023")
rr := httptest.NewRecorder()
http.HandlerFunc(ContentByURL).ServeHTTP(rr, req)

assert.Equal(t, http.StatusPaymentRequired, rr.Code)
_, err := rr.Body.ReadByte()
assert.Equal(t, io.EOF, err)
}

0 comments on commit 0e86bf8

Please sign in to comment.