<commit_before>/*
 * Copyright 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uniqush

import (
    "os"
    "url"
    "http"
    "strings"
    "io/ioutil"
)

/* FIXME
 * Yes, it is http not https
 * Because:
 *  1) The certificate does not match the host name 
 *      android.apis.google.com
 *  2) Go does not support (until now) user defined
 *      verifier for TLS
 * The user defined verifier feature was submitted
 * and under reviewed:
 * http://codereview.appspot.com/4964043/
 *
 * However, even we can use a fake verifier, there
 * is still a security issue.
 *
 * Hope goole could fix the certificate problem
 * soon, or we have to use C2DM as an unsecure
 * service.
 */
const (
	service_url string = "http://android.apis.google.com/c2dm/send"
)

type C2DMPusher struct {
	ServiceType
}

func NewC2DMPusher() *C2DMPusher {
	ret := &C2DMPusher{ServiceType{SRVTYPE_C2DM}}
	return ret
}

func (n *Notification) toC2DMFormat() map[string]string {
	ret := make(map[string]string, len(n.Data)+6)
    for k, v := range n.Data {
        ret[k] = v
    }
	return ret
}

func (p *C2DMPusher) Push(sp *PushServiceProvider,
s *DeliveryPoint,
n *Notification) (string, os.Error) {
	if !p.IsCompatible(&s.OSType) {
		return "", &PushErrorIncompatibleOS{p.ServiceType, s.OSType}
	}

    /* Debug retry */

    if n.Data["msg"] == "retryme" {
        n.Data["msg"] = "No retry!!"
		reterr := NewRetryError(-1)
        return "", reterr
    }

    /* End Debug */
	msg := n.toC2DMFormat()
	data := url.Values{}

	data.Set("registration_id", s.RegistrationID())
	/* TODO better collapse key */
	data.Set("collapse_key", msg["msg"])

	for k, v := range msg {
		data.Set("data."+k, v)
	}

	req, err := http.NewRequest("POST", service_url, strings.NewReader(data.Encode()))
	if err != nil {
		return "", err
	}
	req.Header.Set("Authorization", "GoogleLogin auth="+sp.AuthToken())
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	r, e20 := http.DefaultClient.Do(req)
	if e20 != nil {
		return "", e20
	}
	refreshpsp := false
	new_auth_token := r.Header.Get("Update-Client-Auth")<commit_msg>Removed some comment<commit_after>/*
 * Copyright 2011 <NAME>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package uniqush

import (
    "os"
    "url"
    "http"
    "strings"
    "io/ioutil"
)

/* FIXME
 * Yes, it is http not https
 * Because:
 *  1) The certificate does not match the host name 
 *      android.apis.google.com
 *  2) Go does not support (until now) user defined
 *      verifier for TLS
 * The user defined verifier feature was submitted
 * and under reviewed:
 * http://codereview.appspot.com/4964043/
 *
 * However, even we can use a fake verifier, there
 * is still a security issue.
 *
 * Hope goole could fix the certificate problem
 * soon, or we have to use C2DM as an unsecure
 * service.
 */
const (
	service_url string = "http://android.apis.google.com/c2dm/send"
)

type C2DMPusher struct {
	ServiceType
}

func NewC2DMPusher() *C2DMPusher {
	ret := &C2DMPusher{ServiceType{SRVTYPE_C2DM}}
	return ret
}

func (n *Notification) toC2DMFormat() map[string]string {
	ret := make(map[string]string, len(n.Data)+6)
    for k, v := range n.Data {
        ret[k] = v
    }
	return ret
}

func (p *C2DMPusher) Push(sp *PushServiceProvider,
s *DeliveryPoint,
n *Notification) (string, os.Error) {
	if !p.IsCompatible(&s.OSType) {
		return "", &PushErrorIncompatibleOS{p.ServiceType, s.OSType}
	}

	msg := n.toC2DMFormat()
	data := url.Values{}

	data.Set("registration_id", s.RegistrationID())
	/* TODO better collapse key */
	data.Set("collapse_key", msg["msg"])

	for k, v := range msg {
		data.Set("data."+k, v)
	}

	req, err := http.NewRequest("POST", service_url, strings.NewReader(data.Encode()))
	if err != nil {
		return "", err
	}
	req.Header.Set("Authorization", "GoogleLogin auth="+sp.AuthToken())
	req.Header.Set("Content-Type", "application/x-www-form-urlencoded")
	r, e20 := http.DefaultClient.Do(req)
	if e20 != nil {
		return "", e20
	}
	refreshpsp := false
	new_auth_token := r.Header.Get("Update-Client-Auth")
{'fraction_non_alphanumeric': 0.12969483568075119, 'fraction_numerical': 0.013497652582159625, 'mean_word_length': 3.540852575488455, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4972998', 'n_tokens_mistral': 1741, 'n_tokens_neox': 1706, 'n_words': 529}