Skip to content
This repository has been archived by the owner on Apr 23, 2021. It is now read-only.

bugfix: invalid parsing of response #49

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions gcm.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@ package gcm
import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"log"
"mime"
"net/http"
"path/filepath"
"runtime"
"strings"
"sync"
"time"
Expand Down Expand Up @@ -70,7 +74,15 @@ var (
// Prints debug info if DebugMode is set.
func debug(m string, v interface{}) {
if DebugMode {
log.Printf(m+":%+v", v)
_, file, line, ok := runtime.Caller(1)
if !ok {
file = "???"
line = 0
} else {
file = filepath.Base(file)
}

log.Printf("(%s:%d) %s:%+v", file, line, m, v)
}
}

Expand Down Expand Up @@ -112,7 +124,7 @@ type HttpResponse struct {
Failure uint `json:"failure,omitempty"`
CanonicalIds uint `json:"canonical_ids,omitempty"`
Results []Result `json:"results,omitempty"`
MessageId int `json:"message_id,omitempty"`
MessageId int `json:"message_id,omitempty"`
Error string `json:"error,omitempty"`
}

Expand Down Expand Up @@ -202,6 +214,11 @@ func (c *httpGcmClient) send(apiKey string, m HttpMessage) (*HttpResponse, error
debug("received body", string(body))
err = json.Unmarshal(body, &gcmResp)
if err != nil {
contentType := httpResp.Header.Get("Content-Type")
mediatype, _, _ := mime.ParseMediaType(contentType)
if mediatype != "application/json" {
return gcmResp, errors.New(string(body))
}
return gcmResp, fmt.Errorf("error unmarshaling json from body: %v", err)
}
// TODO(silvano): this is assuming that the header contains seconds instead of a date, need to check
Expand Down