// sent a request off to twitter. Returns the response's body or an error.
func send(url, method string, form map[string][]string, client *Client, body string) (result string, err os.Error) {
	req := new(http.Request)
	req.Method = method
	req.RawURL = url
	req.Host = URLHost
	req.Referer = "none"
	req.UserAgent = HTTPUserAgent
	req.Form = form
	req.Header = map[string]string{
		"Connection":    "Keep Alive",
		"Authorization": getAuthHeader(client),
	}
	req.Body = strings.NewReader(body)
	req.URL, err = http.ParseURL(req.RawURL)
	if err != nil {
		return "", err
	}

	// send request
	resp := new(http.Response)
	resp, err = http.Send(req)
	if err != nil {
		return "", err
	}
	result = getResponseBody(resp)
	return result, nil
}
Exemple #2
0
func handleReplaceForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, kind := path.Split(r.URL.Path)
	ser := DefaultMaster.delegator.Delegate(kind).(SerializerFormParser)
	s := ser.NewFromForm(r.Form)
	if s == nil {
		r.Form = nil
		w.SetHeader("Location", r.Referer)
		w.WriteHeader(302)
		return
	}
	ser.Replace(s)
	DefaultMaster.Save(s)
	out := bytes.NewBufferString("")
	host := s.Host()
	if host != "" {
		DefaultMaster.save(out, s)
		http.Post("http://"+host+DefaultMaster.replacePattern+kind, "application/octet-stream", out)
	} else {
		bc := DefaultMaster.delegator.(DelegatorBroadcaster)
		for _, h := range bc.Hosts() {
			out = bytes.NewBufferString("")
			DefaultMaster.save(out, s)
			http.Post("http://"+h+DefaultMaster.replacePattern+kind, "application/octet-stream", out)
		}
	}
	DefaultMaster.Logger.Printf("%v erfolgreich modifiziert", s.Log())
	redir := "http://" + r.Host + r.FormValue("Redir")
	w.SetHeader("Location", redir)
	w.WriteHeader(302)
}
Exemple #3
0
func handleInsertForm(w http.ResponseWriter, r *http.Request) {
	r.ParseForm()
	_, kind := path.Split(r.URL.Path)
	ser := DefaultMaster.delegator.Delegate(kind).(SerializerFormParser)
	s := ser.NewFromForm(r.Form)
	if s == nil {
		r.Form = nil
		w.SetHeader("Location", r.Referer)
		w.WriteHeader(302)
		return
	}
	ser.All(ser.Insert(s))
	err := DefaultMaster.Save(s)
	if err != nil {
		DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Hauptserver fehlgeschlagen", s.Log())
		w.WriteHeader(500)
	}
	out := bytes.NewBufferString("")
	out = bytes.NewBufferString("")
	DefaultMaster.save(out, s)

	host := s.Host()
	switch host {
	case "":
		bc := DefaultMaster.delegator.(DelegatorBroadcaster)
		for _, h := range bc.Hosts() {
			resp, err := http.Post("http://"+h+DefaultMaster.insertPattern+kind, "application/octet-stream", out)
			if resp.StatusCode == 500 || err != nil {
				DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Server %s fehlgeschlagen", s.Log(), h)
			}
		}
	case "NO":
		break
	default:
		resp, err := http.Post("http://"+host+DefaultMaster.insertPattern+kind, "application/octet-stream", out)
		if resp.StatusCode == 500 || err != nil {
			DefaultMaster.Logger.Printf("Problem: Einfügen von %s auf Server %s fehlgeschlagen", s.Log(), host)
		}
	}
	DefaultMaster.Logger.Printf("%v erfolgreich angelegt", s.Log())
	redir := "http://" + r.Host + r.FormValue("Redir") + strconv.Itoa(s.Key())
	w.SetHeader("Location", redir)
	w.WriteHeader(302)
}
Exemple #4
0
// Write a request and read the response;
// This function will also fix-up req.Form for 'GET's
func (self *Conn) Request(req *http.Request) (resp *http.Response, err os.Error) {
	err = self.dial()
	if err == nil {
		if req.Form != nil && req.Method == "GET" {
			if req.URL.RawQuery != "" {
				req.URL.RawQuery += "&"
			}
			req.URL.RawQuery += req.Form.Encode()
			req.Form = nil
		}
		err = self.c.Write(req)
		if err == nil {
			resp, err = self.c.Read(req)
		}
	}
	if err != nil {
		if err == http.ErrPersistEOF {
			err = nil
		}
		self.Close()
	}
	return
}
Exemple #5
0
func AdminController(w http.ResponseWriter, r *http.Request) {
	View.Blog = 0
	View.Rubric = 0
	View.Article = 0
	View.Server = 0
	View.Theme = 0
	View.Global = 0
	View.Resource = 0

	rd, err := r.MultipartReader()
	if err == nil {
		r.Form = make(map[string][]string)
		for {
			pt, _ := rd.NextPart()
			if pt == nil {
				break
			}
			var fh [1]string
			fd, _ := ioutil.ReadAll(pt)
			fh[0] = strings.TrimSpace(string(fd))
			r.Form[strings.TrimSpace(pt.FormName())] = fh[0:1]
		}
	}
	// Originalpfad der Url zwischenspeichern und nach Redirect widerherstellen
	route := r.FormValue("Route")
	if route != "" {
		orig := r.URL.Path
		r.URL.Path = route
		View.Master.HandleForm(route, w, r)
		r.URL.Path = orig
	}

	dir, file := path.Split(r.URL.Path)
	ids := strings.Split(file, ",", -1)

	if !sort.IsSorted(View.Blogs) {
		sort.Sort(View.Blogs)
	}
	if !sort.IsSorted(View.Themes) {
		sort.Sort(View.Themes)
	}
	if !sort.IsSorted(View.Globals) {
		sort.Sort(View.Globals)
	}

	kind := strings.Replace(dir, "/", "", -1)
	switch kind {
	case "blogs", "newrubrics":
		View.Blog, _ = strconv.Atoi(ids[0])
	case "rubrics", "newarticles":
		View.Blog, _ = strconv.Atoi(ids[0])
		View.Rubric, _ = strconv.Atoi(ids[1])
	case "articles":
		View.Blog, _ = strconv.Atoi(ids[0])
		View.Rubric, _ = strconv.Atoi(ids[1])
		View.Article, _ = strconv.Atoi(ids[2])
	case "servers":
		View.Server, _ = strconv.Atoi(ids[0])
	case "globals":
		View.Global, _ = strconv.Atoi(ids[0])
	case "themes", "newresources":
		View.Theme, _ = strconv.Atoi(ids[0])
	case "resources":
		View.Theme, _ = strconv.Atoi(ids[0])
		View.Resource, _ = strconv.Atoi(ids[1])
	case "":
		kind = "admin"
	}

	w.SetHeader("Content-Type", "text/html; charset=utf-8")
	w.SetHeader("Content-Encoding", "gzip")
	AdminDispatch(w, kind)
}