http_client.go 3.79 KB
Newer Older
Lukas Matt's avatar
Lukas Matt committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
package federation
//
// GangGo Diaspora Federation Library
// Copyright (C) 2017 Lukas Matt <lukas@zauberstuhl.de>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
//

import (
  "time"
  "net/http"
  "encoding/json"
Lukas Matt's avatar
Lukas Matt committed
24
  "github.com/Zauberstuhl/go-xml"
Lukas Matt's avatar
Lukas Matt committed
25 26 27 28 29
  "errors"
  "io"
  "strings"
)

Lukas Matt's avatar
Lukas Matt committed
30 31 32
const (
  PROTO_HTTP = "http://"
  PROTO_HTTPS = "https://"
Lukas Matt's avatar
Lukas Matt committed
33 34
  CONTENT_TYPE_ENVELOPE = "application/magic-envelope+xml"
  CONTENT_TYPE_JSON = "application/json"
Lukas Matt's avatar
Lukas Matt committed
35 36
)

Lukas Matt's avatar
Lukas Matt committed
37 38
var timeout = time.Duration(10 * time.Second)

39
func PushToPrivate(host, guid string, body io.Reader) error {
Lukas Matt's avatar
Lukas Matt committed
40
  return push(host, "/receive/users/" + guid, PROTO_HTTPS, CONTENT_TYPE_JSON, body)
Lukas Matt's avatar
Lukas Matt committed
41 42
}

43
func PushToPublic(host string, body io.Reader) error {
Lukas Matt's avatar
Lukas Matt committed
44
  return push(host, "/receive/public", PROTO_HTTPS, CONTENT_TYPE_ENVELOPE, body)
Lukas Matt's avatar
Lukas Matt committed
45
}
Lukas Matt's avatar
Lukas Matt committed
46

Lukas Matt's avatar
Lukas Matt committed
47
func push(host, endpoint, proto, contentType string, body io.Reader) error {
Lukas Matt's avatar
Lukas Matt committed
48
  req, err := http.NewRequest("POST", proto + host + endpoint, body)
Lukas Matt's avatar
Lukas Matt committed
49 50 51
  if err != nil {
    return err
  }
Lukas Matt's avatar
Lukas Matt committed
52
  req.Header.Add("Content-Type", contentType)
53 54

  client := &http.Client{Timeout: timeout}
Lukas Matt's avatar
Lukas Matt committed
55 56
  resp, err := client.Do(req)
  if err != nil {
Lukas Matt's avatar
Lukas Matt committed
57
    if proto == PROTO_HTTPS {
58
      logger.Info("Retry with", PROTO_HTTP, "on", host, err)
Lukas Matt's avatar
Lukas Matt committed
59
      return push(host, endpoint, PROTO_HTTP, contentType, body)
Lukas Matt's avatar
Lukas Matt committed
60 61 62 63 64 65
    }
    return err
  }
  defer resp.Body.Close()

  if !(resp.StatusCode == 200 || resp.StatusCode == 202) {
66
    return errors.New("push results in: " + resp.Status)
Lukas Matt's avatar
Lukas Matt committed
67 68 69 70 71
  }
  return nil
}

func FetchJson(method, url string, body io.Reader, result interface{}) error {
72 73 74 75 76
  var proto string
  if !strings.HasPrefix(url, "http") {
    proto = "https://"
  }
  req, err := http.NewRequest(method, proto + url, body)
Lukas Matt's avatar
Lukas Matt committed
77 78 79 80 81 82 83 84 85 86
  if err != nil {
    return err
  }
  req.Header.Set("Content-Type", "application/json")

  client := &http.Client{
    Timeout: timeout,
  }
  resp, err := client.Do(req)
  if err != nil {
87 88 89
    if !strings.HasPrefix(url, "http") {
      return FetchJson(method, "http://" + url, body, result)
    }
Lukas Matt's avatar
Lukas Matt committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
    return err
  }

  err = json.NewDecoder(resp.Body).Decode(result)
  if err != nil {
    return err
  }
  return nil
}

func FetchXml(method, url string, body io.Reader, result interface{}) error {
  var proto string
  if !strings.HasPrefix(url, "http") {
    proto = "https://"
  }
  req, err := http.NewRequest(method, proto + url, body)
  if err != nil {
    return err
  }
  req.Header.Set("Content-Type", "application/xrd+xml")

  client := &http.Client{
    Timeout: timeout,
  }
  resp, err := client.Do(req)
  if err != nil {
    if !strings.HasPrefix(url, "http") {
      return FetchXml(method, "http://" + url, body, result)
    }
    return err
  }
  err = xml.NewDecoder(resp.Body).Decode(result)
  if err != nil {
    return err
  }
  return nil
}

func FetchHtml(method, url string, body io.Reader) (resp *http.Response, err error) {
  var proto string
  if !strings.HasPrefix(url, "http") {
    proto = "https://"
  }
  req, err := http.NewRequest(method, proto + url, body)
  if err != nil {
    return nil, err
  }
  req.Header.Set("Content-Type", "application/xrd+xml")

  client := &http.Client{
    Timeout: timeout,
  }
  resp, err = client.Do(req)
  if err != nil {
    if !strings.HasPrefix(url, "http") {
      return FetchHtml(method, "http://" + url, body)
    }
    return nil, err
  }
  return
}