Exemplo n.º 1
1
func UnmarshalStream(url string, reader io.Reader) (feed *Feed, err error) {
	// Read the stream into memory (we'll need to parse it twice)
	var contentReader *bytes.Reader
	var content []byte
	if content, err = ioutil.ReadAll(reader); err == nil {
		contentReader = bytes.NewReader(content)

		genericFeed := GenericFeed{}

		decoder := xml.NewDecoder(contentReader)
		decoder.CharsetReader = charset.NewReader

		// First pass - parse the feed as-is
		if err = decoder.Decode(&genericFeed); err != nil {
			// Error - check for invalid entities and correct as appropriate
			if fixed, fixedContent := fixEntities(content); fixed {
				// At least one replacement was made. Retry
				contentReader = bytes.NewReader(fixedContent)
				decoder = xml.NewDecoder(contentReader)
				decoder.CharsetReader = charset.NewReader

				// Try decoding again
				err = decoder.Decode(&genericFeed)
			}
		}

		if err != nil {
			return
		}

		var xmlFeed FeedMarshaler
		if genericFeed.XMLName.Space == "http://www.w3.org/1999/02/22-rdf-syntax-ns#" && genericFeed.XMLName.Local == "RDF" {
			xmlFeed = &rss1Feed{}
		} else if genericFeed.XMLName.Local == "rss" {
			xmlFeed = &rss2Feed{}
		} else if genericFeed.XMLName.Space == "http://www.w3.org/2005/Atom" && genericFeed.XMLName.Local == "feed" {
			xmlFeed = &atomFeed{}
		} else {
			err = errors.New("Unsupported type of feed (" +
				genericFeed.XMLName.Space + ":" + genericFeed.XMLName.Local + ")")
			return
		}

		contentReader.Seek(0, 0)

		decoder = xml.NewDecoder(contentReader)
		decoder.CharsetReader = charset.NewReader

		if err = decoder.Decode(xmlFeed); err == nil {
			if feed, err = xmlFeed.Marshal(); err == nil {
				feed.URL = url
			}
		}
	}

	return
}
Exemplo n.º 2
1
Arquivo: feed.go Projeto: huluwa/grr
func UnmarshalStream(reader io.Reader) (*Feed, string, error) {
	format := ""

	// Read the stream into memory (we'll need to parse it twice)
	var contentReader *bytes.Reader
	if buffer, err := ioutil.ReadAll(reader); err == nil {
		contentReader = bytes.NewReader(buffer)
	} else {
		return nil, format, err
	}

	genericFeed := GenericFeed{}

	decoder := xml.NewDecoder(contentReader)
	decoder.CharsetReader = charsetReader

	if err := decoder.Decode(&genericFeed); err != nil {
		return nil, format, err
	}

	var xmlFeed FeedMarshaler

	if genericFeed.XMLName.Space == "http://www.w3.org/1999/02/22-rdf-syntax-ns#" && genericFeed.XMLName.Local == "RDF" {
		xmlFeed = &rss1Feed{}
		format = "RSS1"
	} else if genericFeed.XMLName.Local == "rss" {
		xmlFeed = &rss2Feed{}
		format = "RSS2"
	} else if genericFeed.XMLName.Space == "http://www.w3.org/2005/Atom" && genericFeed.XMLName.Local == "feed" {
		xmlFeed = &atomFeed{}
		format = "Atom"
	} else {
		return nil, format, errors.New("Unsupported type of feed (" +
			genericFeed.XMLName.Space + ":" + genericFeed.XMLName.Local + ")")
	}

	contentReader.Seek(0, 0)

	decoder = xml.NewDecoder(contentReader)
	decoder.CharsetReader = charsetReader

	if err := decoder.Decode(xmlFeed); err != nil {
		return nil, format, err
	}

	feed, err := xmlFeed.Marshal()
	if err != nil {
		return nil, format, err
	}

	return &feed, format, nil
}
Exemplo n.º 3
0
// Returns the root node of a tree of valid element names
func names(rdr io.Reader) *name {
	var n = new(name)

	ns := make(map[string]bool)

	decoder := xml.NewDecoder(rdr)
	decoder.Strict = false

	for tok, err := decoder.RawToken(); err != io.EOF; tok, err = decoder.RawToken() {
		// if it breaks, try again!
		if err != nil {
			decoder = xml.NewDecoder(rdr)
			decoder.Strict = false
			continue
		}
		switch el := tok.(type) {
		case xml.StartElement:
			if _, ok := ns[el.Name.Local]; !ok {
				ns[el.Name.Local] = false
			}
		case xml.EndElement:
			if _, ok := ns[el.Name.Local]; ok {
				ns[el.Name.Local] = true
			}
		}
	}
	for k, v := range ns {
		if v {
			n.add([]byte(k))
		}
	}
	// add the XML declaration to the list of valid names (a bit of a hack)
	n.add([]byte("?xml version=\"1.0\"?"))
	return n
}
Exemplo n.º 4
0
func respIsOk(sVueResp *SVUEResponse, expectedElement string) (*xml.Decoder, error) {
	d := xml.NewDecoder(strings.NewReader(sVueResp.Result))

TokenLoop:
	for {
		t, err := d.Token()

		if err == io.EOF {
			return nil, SVUEError{
				OrigError: err,
				Code:      DecodingError,
			}
		}

		if _t, ok := t.(xml.StartElement); ok {
			switch _t.Name.Local {
			case expectedElement:
				break TokenLoop
			case "RT_ERROR":
				return nil, decodeRespError(sVueResp)
			default:
				continue TokenLoop
			}
		} else {
			continue
		}
	}

	return xml.NewDecoder(strings.NewReader(sVueResp.Result)), nil
}
Exemplo n.º 5
0
func loadConfig(dirName string) (*objects.Core, error) {
	dir, err := os.Open(os.ExpandEnv(dirName))
	if err != nil {
		return nil, err
	}
	s, err := dir.Stat()
	if err != nil {
		return nil, err
	}
	cfg := &objects.Core{}
	if !s.IsDir() {
		decoder := xml.NewDecoder(dir)
		err = decoder.Decode(&cfg)
		return cfg, err
	}

	files, err := dir.Readdir(-1)
	if err != nil {
		return nil, err
	}
	sort.Sort(FilesByName(files))
	for _, fInfo := range files {
		file, err := os.Open(dirName + fInfo.Name())
		if err != nil {
			return nil, err
		}
		decoder := xml.NewDecoder(file)
		err = decoder.Decode(&cfg)
		if err != nil {
			return nil, fmt.Errorf("Decoding %s: %s", fInfo.Name(), err.Error())
		}
	}

	return cfg, nil
}
Exemplo n.º 6
0
func (l *LibSuite) TestReadRowsFromSheetWithMergeCells(c *C) {
	var sharedstringsXML = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="3" uniqueCount="3">
  <si>
    <t>Value A</t>
  </si>
  <si>
    <t>Value B</t>
  </si>
  <si>
    <t>Value C</t>
  </si>
</sst>
`)
	var sheetxml = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
  <sheetViews>
    <sheetView workbookViewId="0"/>
  </sheetViews>
  <sheetFormatPr customHeight="1" defaultColWidth="17.29" defaultRowHeight="15.0"/>
  <cols>
    <col customWidth="1" min="1" max="6" width="14.43"/>
  </cols>
  <sheetData>
    <row r="1" ht="15.75" customHeight="1">
      <c r="A1" s="1" t="s">
        <v>0</v>
      </c>
    </row>
    <row r="2" ht="15.75" customHeight="1">
      <c r="A2" s="1" t="s">
        <v>1</v>
      </c>
      <c r="B2" s="1" t="s">
        <v>2</v>
      </c>
    </row>
  </sheetData>
  <mergeCells count="1">
    <mergeCell ref="A1:B1"/>
  </mergeCells>
  <drawing r:id="rId1"/>
</worksheet>`)
	worksheet := new(xlsxWorksheet)
	err := xml.NewDecoder(sheetxml).Decode(worksheet)
	c.Assert(err, IsNil)
	sst := new(xlsxSST)
	err = xml.NewDecoder(sharedstringsXML).Decode(sst)
	c.Assert(err, IsNil)
	file := new(File)
	file.referenceTable = MakeSharedStringRefTable(sst)
	sheet := new(Sheet)
	rows, _, _, _ := readRowsFromSheet(worksheet, file, sheet)
	row := rows[0] //
	cell1 := row.Cells[0]
	c.Assert(cell1.HMerge, Equals, 1)
	c.Assert(cell1.VMerge, Equals, 0)
}
Exemplo n.º 7
0
func processIQ(stanza *xmpp.ClientIQ) interface{} {
	buf := bytes.NewBuffer(stanza.Query)
	parser := xml.NewDecoder(buf)
	token, _ := parser.Token()
	if token == nil {
		return nil
	}
	startElem, ok := token.(xml.StartElement)
	if !ok {
		return nil
	}
	switch startElem.Name.Local {
	case "update_contacts":
		var contacts ConfigContacts
		if err := xml.NewDecoder(bytes.NewBuffer(stanza.Query)).Decode(&contacts); err != nil {
			log.Print("Failed to parse IQ for ConfigContacts")
			return nil
		}
		for _, contact := range contacts.Contacts {
			handleContact(contact)
		}
		return xmpp.EmptyReply{}
	default:
		log.Print("Unknown IQ: ", startElem.Name.Local)
	}

	return nil
}
Exemplo n.º 8
0
func (s *SQSRequest) CreateQueue(queueName string, options map[string]string) (*QueueURLResponse, error) {
	params := map[string]string{
		"Action":    "CreateQueue",
		"QueueName": queueName,
	}

	count := 1
	for name, value := range options {
		params[fmt.Sprintf("Attribute.%d.Name", count)] = name
		params[fmt.Sprintf("Attribute.%d.Value", count)] = value
		count++
	}

	reader, err := s.makeSQSAdminRequest(params)
	if err != nil {
		er := new(ErrorResponse)
		xml.NewDecoder(reader).Decode(er)
		log.Println(er)
		return nil, err
	}

	qur := new(QueueURLResponse)
	if err = xml.NewDecoder(reader).Decode(qur); err != nil {
		return nil, err
	}

	return qur, nil
}
Exemplo n.º 9
0
func (v *Vsphere) call(body string, dst interface{}) error {
	buf := new(bytes.Buffer)
	buf.WriteString(soapPre)
	buf.WriteString(body)
	buf.WriteString(soapPost)
	req, err := http.NewRequest("POST", v.url.String(), buf)
	if err != nil {
		return err
	}
	for k, v := range v.header {
		req.Header[k] = v
	}
	for _, c := range v.cookies.Cookies(v.url) {
		req.AddCookie(c)
	}
	resp, err := v.client.Do(req)
	if err != nil {
		return err
	}
	v.cookies.SetCookies(v.url, resp.Cookies())
	defer resp.Body.Close()
	if resp.StatusCode == 500 {
		var e Error
		if err := xml.NewDecoder(resp.Body).Decode(&e); err != nil {
			return fmt.Errorf("vsphere: bad response, could not decode error: %v", err)
		}
		return e
	} else if resp.StatusCode != 200 {
		return fmt.Errorf("vsphere: bad status code %v", resp.Status)
	}
	if dst != nil {
		return xml.NewDecoder(resp.Body).Decode(dst)
	}
	return nil
}
Exemplo n.º 10
0
// startStream will start a new XML decoder for the connection, signal the start of a stream to the server and verify that the server has
// also started the stream; if o.Debug is true, startStream will tee decoded XML data to stderr.  The features advertised by the server
// will be returned.
func (c *Client) startStream(o *Options, domain string) (*streamFeatures, error) {
	if o.Debug {
		c.p = xml.NewDecoder(tee{c.conn, os.Stderr})
	} else {
		c.p = xml.NewDecoder(c.conn)
	}

	_, err := fmt.Fprintf(c.conn, "<?xml version='1.0'?>\n"+
		"<stream:stream to='%s' xmlns='%s'\n"+
		" xmlns:stream='%s' version='1.0'>\n",
		xmlEscape(domain), nsClient, nsStream)
	if err != nil {
		return nil, err
	}

	// We expect the server to start a <stream>.
	se, err := nextStart(c.p)
	if err != nil {
		return nil, err
	}
	if se.Name.Space != nsStream || se.Name.Local != "stream" {
		return nil, fmt.Errorf("expected <stream> but got <%v> in %v", se.Name.Local, se.Name.Space)
	}

	// Now we're in the stream and can use Unmarshal.
	// Next message should be <features> to tell us authentication options.
	// See section 4.6 in RFC 3920.
	f := new(streamFeatures)
	if err = c.p.DecodeElement(f, nil); err != nil {
		return f, errors.New("unmarshal <features>: " + err.Error())
	}
	return f, nil
}
Exemplo n.º 11
0
func (v *VCloudSession) FindOrganisations(max_page_size, max_pages int) (Orgs []*Organisation, err error) {

	if max_page_size <= 0 {
		max_page_size = 1
	}

	if max_pages <= 0 {
		max_pages = 1
	}

	for i := 1; i <= max_pages; i++ {
		page := &OrgReferences{}
		uri := fmt.Sprintf("/api/query?type=organization&format=references&pageSize=%v&page=%v", max_page_size, i)
		r, err := v.Get(uri)

		if err != nil {
			continue
		}

		defer r.Body.Close()

		if r.StatusCode == 400 {
			break
		}

		_ = xml.NewDecoder(r.Body).Decode(page)

		for _, record := range page.Records {
			u, _ := url.Parse(record.Href)
			r, err := v.Get(u.Path)

			if err != nil {
				continue
			}

			defer r.Body.Close()

			if r.StatusCode != 200 {
				continue
			}

			new_org := &Organisation{}
			_ = xml.NewDecoder(r.Body).Decode(new_org)

			for k, val := range new_org.Links {
				u, _ := url.Parse(val.Href)
				new_org.Links[k].Href = u.Path
			}

			Orgs = append(Orgs, new_org)
		}
	}

	if len(Orgs) <= 0 {
		return Orgs, errors.New("No organisations returned.")
	} else {
		return Orgs, nil
	}
}
Exemplo n.º 12
0
func (l *LibSuite) TestReadRowsFromSheetWithHiddenColumn(c *C) {
	var sharedstringsXML = bytes.NewBufferString(`
		<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
		<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
		    <si><t>This is a test.</t></si>
		    <si><t>This should be invisible.</t></si>
		</sst>`)
	var sheetxml = bytes.NewBufferString(`
		<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
		<worksheet xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main"
		    xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main"
		    xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main">
			<sheetViews><sheetView workbookViewId="0"/>
			</sheetViews>
			<sheetFormatPr customHeight="1" defaultColWidth="14.43" defaultRowHeight="15.75"/>
			<cols>
				<col hidden="1" max="2" min="2"/>
			</cols>
		    <sheetData>
		        <row r="1">
		            <c r="A1" s="1" t="s"><v>0</v></c>
		            <c r="B1" s="1" t="s"><v>1</v></c>
		        </row>
		    </sheetData><drawing r:id="rId1"/></worksheet>`)
	worksheet := new(xlsxWorksheet)
	err := xml.NewDecoder(sheetxml).Decode(worksheet)
	c.Assert(err, IsNil)
	sst := new(xlsxSST)
	err = xml.NewDecoder(sharedstringsXML).Decode(sst)
	c.Assert(err, IsNil)
	file := new(File)
	file.referenceTable = MakeSharedStringRefTable(sst)
	sheet := new(Sheet)
	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
	c.Assert(maxRows, Equals, 1)
	c.Assert(maxCols, Equals, 2)
	row := rows[0]
	c.Assert(row.Sheet, Equals, sheet)
	c.Assert(len(row.Cells), Equals, 2)

	cell1 := row.Cells[0]
	c.Assert(cell1.Type(), Equals, CellTypeString)
	if val, err := cell1.String(); err != nil {
		c.Error(err)
	} else {
		c.Assert(val, Equals, "This is a test.")
	}
	c.Assert(cell1.Hidden, Equals, false)

	cell2 := row.Cells[1]
	c.Assert(cell2.Type(), Equals, CellTypeString)
	if val, err := cell2.String(); err != nil {
		c.Error(err)
	} else {
		c.Assert(val, Equals, "This should be invisible.")
	}
	c.Assert(cell2.Hidden, Equals, true)
}
Exemplo n.º 13
0
func (l *LibSuite) TestReadRowsFromSheetWithTrailingEmptyCells(c *C) {
	var row *Row
	var cell1, cell2, cell3, cell4 *Cell
	var sharedstringsXML = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="4" uniqueCount="4"><si><t>A</t></si><si><t>B</t></si><si><t>C</t></si><si><t>D</t></si></sst>`)
	var sheetxml = bytes.NewBufferString(`
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships"><dimension ref="A1:D8"/><sheetViews><sheetView tabSelected="1" workbookViewId="0"><selection activeCell="A7" sqref="A7"/></sheetView></sheetViews><sheetFormatPr baseColWidth="10" defaultRowHeight="15"/><sheetData><row r="1" spans="1:4"><c r="A1" t="s"><v>0</v></c><c r="B1" t="s"><v>1</v></c><c r="C1" t="s"><v>2</v></c><c r="D1" t="s"><v>3</v></c></row><row r="2" spans="1:4"><c r="A2"><v>1</v></c></row><row r="3" spans="1:4"><c r="B3"><v>1</v></c></row><row r="4" spans="1:4"><c r="C4"><v>1</v></c></row><row r="5" spans="1:4"><c r="D5"><v>1</v></c></row><row r="6" spans="1:4"><c r="C6"><v>1</v></c></row><row r="7" spans="1:4"><c r="B7"><v>1</v></c></row><row r="8" spans="1:4"><c r="A8"><v>1</v></c></row></sheetData><pageMargins left="0.7" right="0.7" top="0.78740157499999996" bottom="0.78740157499999996" header="0.3" footer="0.3"/></worksheet>
`)
	worksheet := new(xlsxWorksheet)
	err := xml.NewDecoder(sheetxml).Decode(worksheet)
	c.Assert(err, IsNil)

	sst := new(xlsxSST)
	err = xml.NewDecoder(sharedstringsXML).Decode(sst)
	c.Assert(err, IsNil)

	file := new(File)
	file.referenceTable = MakeSharedStringRefTable(sst)
	sheet := new(Sheet)
	rows, _, maxCol, maxRow := readRowsFromSheet(worksheet, file, sheet)
	c.Assert(maxCol, Equals, 4)
	c.Assert(maxRow, Equals, 8)

	row = rows[0]
	c.Assert(row.Sheet, Equals, sheet)
	c.Assert(len(row.Cells), Equals, 4)

	cell1 = row.Cells[0]
	c.Assert(cell1.Value, Equals, "A")

	cell2 = row.Cells[1]
	c.Assert(cell2.Value, Equals, "B")

	cell3 = row.Cells[2]
	c.Assert(cell3.Value, Equals, "C")

	cell4 = row.Cells[3]
	c.Assert(cell4.Value, Equals, "D")

	row = rows[1]
	c.Assert(row.Sheet, Equals, sheet)
	c.Assert(len(row.Cells), Equals, 4)

	cell1 = row.Cells[0]
	c.Assert(cell1.Value, Equals, "1")

	cell2 = row.Cells[1]
	c.Assert(cell2.Value, Equals, "")

	cell3 = row.Cells[2]
	c.Assert(cell3.Value, Equals, "")

	cell4 = row.Cells[3]
	c.Assert(cell4.Value, Equals, "")
}
Exemplo n.º 14
0
func (l *LibSuite) TestRowNotOverwrittenWhenFollowedByEmptyRow(c *C) {
	sheetXML := bytes.NewBufferString(`
	<?xml version="1.0" encoding="UTF-8"?>
	<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:mv="urn:schemas-microsoft-com:mac:vml" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:x14="http://schemas.microsoft.com/office/spreadsheetml/2009/9/main" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac" xmlns:xm="http://schemas.microsoft.com/office/excel/2006/main">
		<sheetViews>
			<sheetView workbookViewId="0" />
		</sheetViews>
		<sheetFormatPr customHeight="1" defaultColWidth="14.43" defaultRowHeight="15.75" />
		<sheetData>
			<row r="2">
				<c r="A2" t="str">
					<f t="shared" ref="A2" si="1">RANDBETWEEN(1,100)</f>
					<v>66</v>
				</c>
			</row>
			<row r="3">
				<c r="A3" t="str">
					<f t="shared" ref="A3" si="2">RANDBETWEEN(1,100)</f>
					<v>30</v>
				</c>
			</row>
			<row r="4">
				<c r="A4" t="str">
					<f t="shared" ref="A4" si="3">RANDBETWEEN(1,100)</f>
					<v>75</v>
				</c>
			</row>
			<row r="7">
				<c r="A7" s="1" t="str">
					<f t="shared" ref="A7" si="4">A4/A2</f>
					<v>1.14</v>
				</c>
			</row>
		</sheetData>
		<drawing r:id="rId1" />
	</worksheet>
	`)

	sharedstringsXML := bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?><sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main"/>`)

	worksheet := new(xlsxWorksheet)
	xml.NewDecoder(sheetXML).Decode(worksheet)

	sst := new(xlsxSST)
	xml.NewDecoder(sharedstringsXML).Decode(sst)

	file := new(File)
	file.referenceTable = MakeSharedStringRefTable(sst)

	sheet := new(Sheet)
	rows, _, _, _ := readRowsFromSheet(worksheet, file, sheet)
	cells := rows[3].Cells

	c.Assert(cells, HasLen, 1)
	c.Assert(cells[0].Value, Equals, "75")
}
Exemplo n.º 15
0
func (s *MyAPIDonutCacheSuite) TestBucketMultipartList(c *C) {
	request, err := http.NewRequest("PUT", testAPIDonutCacheServer.URL+"/bucketmultipartlist", bytes.NewBufferString(""))
	c.Assert(err, IsNil)
	setDummyAuthHeader(request)

	client := http.Client{}
	response, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, 200)

	request, err = http.NewRequest("POST", testAPIDonutCacheServer.URL+"/bucketmultipartlist/object?uploads", bytes.NewBufferString(""))
	c.Assert(err, IsNil)
	setDummyAuthHeader(request)

	response, err = client.Do(request)
	c.Assert(response.StatusCode, Equals, http.StatusOK)

	decoder := xml.NewDecoder(response.Body)
	newResponse := &api.InitiateMultipartUploadResult{}

	err = decoder.Decode(newResponse)
	c.Assert(err, IsNil)
	c.Assert(len(newResponse.UploadID) > 0, Equals, true)
	uploadID := newResponse.UploadID

	request, err = http.NewRequest("PUT", testAPIDonutCacheServer.URL+"/bucketmultipartlist/object?uploadId="+uploadID+"&partNumber=1", bytes.NewBufferString("hello world"))
	c.Assert(err, IsNil)
	setDummyAuthHeader(request)

	response1, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response1.StatusCode, Equals, http.StatusOK)

	request, err = http.NewRequest("PUT", testAPIDonutCacheServer.URL+"/bucketmultipartlist/object?uploadId="+uploadID+"&partNumber=2", bytes.NewBufferString("hello world"))
	c.Assert(err, IsNil)
	setDummyAuthHeader(request)

	response2, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response2.StatusCode, Equals, http.StatusOK)

	request, err = http.NewRequest("GET", testAPIDonutCacheServer.URL+"/bucketmultipartlist?uploads", nil)
	c.Assert(err, IsNil)
	setDummyAuthHeader(request)

	response3, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response3.StatusCode, Equals, http.StatusOK)

	decoder = xml.NewDecoder(response3.Body)
	newResponse3 := &api.ListMultipartUploadsResponse{}
	err = decoder.Decode(newResponse3)
	c.Assert(err, IsNil)
	c.Assert(newResponse3.Bucket, Equals, "bucketmultipartlist")
}
Exemplo n.º 16
0
func (l *LibSuite) TestReadRowsFromSheetWithLeadingEmptyRows(c *C) {
	var sharedstringsXML = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<sst xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" count="2" uniqueCount="2"><si><t>ABC</t></si><si><t>DEF</t></si></sst>`)
	var sheetxml = bytes.NewBufferString(`<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<worksheet xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:r="http://schemas.openxmlformats.org/officeDocument/2006/relationships" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="x14ac" xmlns:x14ac="http://schemas.microsoft.com/office/spreadsheetml/2009/9/ac">
  <dimension ref="A4:A5"/>
  <sheetViews>
    <sheetView tabSelected="1" workbookViewId="0">
      <selection activeCell="A2" sqref="A2"/>
    </sheetView>
  </sheetViews>
  <sheetFormatPr baseColWidth="10" defaultRowHeight="15" x14ac:dyDescent="0"/>
  <sheetData>
    <row r="4" spans="1:1">
      <c r="A4" t="s">
        <v>0</v>
      </c>
    </row>
    <row r="5" spans="1:1">
      <c r="A5" t="s">
        <v>1</v>
      </c>
    </row>
  </sheetData>
  <pageMargins left="0.75" right="0.75" top="1" bottom="1" header="0.5" footer="0.5"/>
  <pageSetup paperSize="9" orientation="portrait" horizontalDpi="4294967292" verticalDpi="4294967292"/>
  <extLst>
    <ext uri="{64002731-A6B0-56B0-2670-7721B7C09600}" xmlns:mx="http://schemas.microsoft.com/office/mac/excel/2008/main">
      <mx:PLV Mode="0" OnePage="0" WScale="0"/>
    </ext>
  </extLst>
</worksheet>
`)
	worksheet := new(xlsxWorksheet)
	err := xml.NewDecoder(sheetxml).Decode(worksheet)
	c.Assert(err, IsNil)
	sst := new(xlsxSST)
	err = xml.NewDecoder(sharedstringsXML).Decode(sst)
	c.Assert(err, IsNil)

	file := new(File)
	file.referenceTable = MakeSharedStringRefTable(sst)
	sheet := new(Sheet)
	rows, _, maxCols, maxRows := readRowsFromSheet(worksheet, file, sheet)
	c.Assert(maxRows, Equals, 5)
	c.Assert(maxCols, Equals, 1)

	c.Assert(len(rows[0].Cells), Equals, 0)
	c.Assert(len(rows[1].Cells), Equals, 0)
	c.Assert(len(rows[2].Cells), Equals, 0)
	c.Assert(len(rows[3].Cells), Equals, 1)
	c.Assert(rows[3].Cells[0].String(), Equals, "ABC")
	c.Assert(len(rows[4].Cells), Equals, 1)
	c.Assert(rows[4].Cells[0].String(), Equals, "DEF")
}
Exemplo n.º 17
0
func readDatabase(path string) (*Database, error) {
	db := Database{}

	if strings.HasPrefix(path, "http://") || strings.HasPrefix(path, "https://") {
		resp, err := http.Get(DBFILE)
		if err != nil {
			return nil, fmt.Errorf("cannot fetch database file (%s): %s", DBFILE, err)
		}
		defer resp.Body.Close()
		if err := xml.NewDecoder(resp.Body).Decode(&db); err != nil {
			return nil, err
		}
	} else {
		f, err := os.Open(path)
		if err != nil {
			return nil, err
		}
		defer f.Close()

		if err := xml.NewDecoder(f).Decode(&db); err != nil {
			return nil, err
		}
	}

	if len(db.Signatures) == 0 {
		return nil, fmt.Errorf("no signatures loaded, check file format")
	}

	for i, sig := range db.Signatures {
		r, err := regexp.Compile(sig.Signature)
		if err != nil {
			return nil, fmt.Errorf("failed to compile signature %d regexp %q: %v", sig.Id, sig.Signature, err)
		}
		db.Signatures[i].Regexp = r
	}

	if SKIPSOFT {
		var count int
		for _, sig := range db.Signatures {
			if sig.Type == "c" {
				count++
			}
		}
		critSignatures := make([]Signature, 0, count)
		for _, sig := range db.Signatures {
			if sig.Type == "c" {
				critSignatures = append(critSignatures, sig)
			}
		}
		return &Database{critSignatures}, nil
	}

	return &db, nil
}
Exemplo n.º 18
0
func (s *Session) setConn(conn net.Conn) {
	s.conn = conn
	if s.srv.cfg.DebugEnable {
		s.dec = xml.NewDecoder(readTunnel{s.conn, os.Stdout})
		s.w = writeTunnel{s.conn, os.Stdout}
	} else {
		s.dec = xml.NewDecoder(s.conn)
		s.w = s.conn
	}
	s.enc = xml.NewEncoder(s.conn)
}
Exemplo n.º 19
0
// acceptTypeDecoder provide decoded value in given acceptType
func acceptTypeDecoder(body io.Reader, acceptType string, v interface{}) error {
	var d decoder
	switch {
	case acceptType == "application/xml":
		d = xml.NewDecoder(body)
	case acceptType == "application/json":
		d = json.NewDecoder(body)
	default:
		d = xml.NewDecoder(body)
	}
	return d.Decode(v)
}
Exemplo n.º 20
0
func (s *MyAPIFSCacheSuite) TestBucketMultipartList(c *C) {
	request, err := s.newRequest("PUT", testAPIFSCacheServer.URL+"/bucketmultipartlist", 0, nil)
	c.Assert(err, IsNil)

	client := http.Client{}
	response, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response.StatusCode, Equals, 200)

	request, err = s.newRequest("POST", testAPIFSCacheServer.URL+"/bucketmultipartlist/object?uploads", 0, nil)
	c.Assert(err, IsNil)

	response, err = client.Do(request)
	c.Assert(response.StatusCode, Equals, http.StatusOK)

	decoder := xml.NewDecoder(response.Body)
	newResponse := &InitiateMultipartUploadResponse{}

	err = decoder.Decode(newResponse)
	c.Assert(err, IsNil)
	c.Assert(len(newResponse.UploadID) > 0, Equals, true)
	uploadID := newResponse.UploadID

	buffer1 := bytes.NewReader([]byte("hello world"))
	request, err = s.newRequest("PUT", testAPIFSCacheServer.URL+"/bucketmultipartlist/object?uploadId="+uploadID+"&partNumber=1", int64(buffer1.Len()), buffer1)
	c.Assert(err, IsNil)

	response1, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response1.StatusCode, Equals, http.StatusOK)

	buffer2 := bytes.NewReader([]byte("hello world"))
	request, err = s.newRequest("PUT", testAPIFSCacheServer.URL+"/bucketmultipartlist/object?uploadId="+uploadID+"&partNumber=2", int64(buffer2.Len()), buffer2)
	c.Assert(err, IsNil)

	response2, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response2.StatusCode, Equals, http.StatusOK)

	request, err = s.newRequest("GET", testAPIFSCacheServer.URL+"/bucketmultipartlist?uploads", 0, nil)
	c.Assert(err, IsNil)

	response3, err := client.Do(request)
	c.Assert(err, IsNil)
	c.Assert(response3.StatusCode, Equals, http.StatusOK)

	decoder = xml.NewDecoder(response3.Body)
	newResponse3 := &ListMultipartUploadsResponse{}
	err = decoder.Decode(newResponse3)
	c.Assert(err, IsNil)
	c.Assert(newResponse3.Bucket, Equals, "bucketmultipartlist")
}
Exemplo n.º 21
0
Arquivo: main.go Projeto: brydavis/xql
func X2J(filename string) []byte { //(n xmlNode) {
	var n xmlNode
	b, _ := ioutil.ReadFile(filename)
	reader := strings.NewReader(Elementize(xml.NewDecoder(strings.NewReader(string(b)))))

	xml.NewDecoder(reader).Decode(&n)
	var y []interface{}
	for _, v := range n.Nodes {
		y = append(y, v.walk())
	}

	j, _ := json.Marshal(y)
	return j
}
Exemplo n.º 22
0
func handleSAML(w http.ResponseWriter, r *http.Request) {
	resp := server.NewResponse()
	defer resp.Close()

	defer func() {
		err := osin.OutputJSON(resp, w, r)
		if err != nil {
			log.Println("error finishing authZ request", err)
		}
	}()

	if ar := server.HandleAuthorizeRequest(resp, r); ar != nil {
		defer server.FinishAuthorizeRequest(resp, r, ar)

		err := r.ParseForm()
		if err != nil {
			log.Println(err)
		}

		sr := r.Form.Get("SAMLResponse")
		bs, err := base64.StdEncoding.DecodeString(sr)
		if err != nil {
			log.Println("Error decoding base64", err)
		}

		var res saml.Response
		err = xml.NewDecoder(bytes.NewBuffer(bs)).Decode(&res)
		if err != nil {
			log.Println(err)
			return
		}

		pt, err := res.Decrypt(cert)
		if err != nil {
			log.Println("error decrypting saml:", err)
			return
		}

		var a saml.Assertion
		err = xml.NewDecoder(bytes.NewBuffer(pt)).Decode(&a)

		if err != nil {
			log.Println(err)
			return
		}

		ar.UserData = a
		ar.Authorized = true
	}
}
Exemplo n.º 23
0
func (r *Request) decodeEntity(reader io.Reader, contentType string, entityPointer interface{}) (err error) {
	if strings.Contains(contentType, MIME_XML) {
		return xml.NewDecoder(reader).Decode(entityPointer)
	}
	if strings.Contains(contentType, MIME_JSON) || MIME_JSON == defaultRequestContentType {
		decoder := json.NewDecoder(reader)
		decoder.UseNumber()
		return decoder.Decode(entityPointer)
	}
	if MIME_XML == defaultRequestContentType {
		return xml.NewDecoder(reader).Decode(entityPointer)
	}
	return NewError(400, "Unable to unmarshal content of type:"+contentType)
}
Exemplo n.º 24
0
func makeInOut(conn io.ReadWriter, config *Config) (in *xml.Decoder, out io.Writer) {
	if config != nil && config.InLog != nil {
		in = xml.NewDecoder(io.TeeReader(conn, config.InLog))
	} else {
		in = xml.NewDecoder(conn)
	}

	if config != nil && config.OutLog != nil {
		out = io.MultiWriter(conn, config.OutLog)
	} else {
		out = conn
	}

	return
}
Exemplo n.º 25
0
func (d *Decoder) readData(db *Database) error {
	masterKey, err := db.Credentials.buildMasterKey(db)
	if err != nil {
		return err
	}

	block, err := aes.NewCipher(masterKey)
	if err != nil {
		return err
	}

	in, err := ioutil.ReadAll(d.r)
	if err != nil {
		return err
	}

	mode := cipher.NewCBCDecrypter(block, db.Headers.EncryptionIV)
	decrypted := make([]byte, len(in))
	mode.CryptBlocks(decrypted, in)

	startBytes := db.Headers.StreamStartBytes
	if !reflect.DeepEqual(decrypted[0:len(startBytes)], startBytes) {
		return errors.New("Database integrity check failed")
	}
	decrypted = decrypted[len(startBytes):]

	var xmlDecoder *xml.Decoder
	if db.Headers.CompressionFlags == GzipCompressionFlag { //Unzip if the header compression flag is 1 for gzip
		zippedBody, err := checkHashBlocks(decrypted)
		if err != nil {
			return err
		}

		b := bytes.NewBuffer(zippedBody)
		r, err := gzip.NewReader(b)
		if err != nil {
			return err
		}
		defer r.Close()
		xmlDecoder = xml.NewDecoder(r)
	} else { //Otherwise assume it not compressed
		xmlDecoder = xml.NewDecoder(bytes.NewReader(decrypted))
	}

	db.Content = &DBContent{}
	err = xmlDecoder.Decode(db.Content)
	return err
}
Exemplo n.º 26
0
func Fuzz(data []byte) int {
	score := 0
	for _, ctor := range []func() interface{}{
		func() interface{} { return nil },
		func() interface{} { return new(string) },
		func() interface{} { return new([]string) },
		func() interface{} { return new(X) },
		func() interface{} { return new([]X) },
	} {
		v0 := ctor()
		valid := false
		dec := xml.NewDecoder(bytes.NewReader(data))
		if dec.Decode(v0) == nil {
			valid = true
		}
		v := ctor()
		dec1 := xml.NewDecoder(bytes.NewReader(data))
		dec1.Strict = false
		dec1.AutoClose = xml.HTMLAutoClose
		dec1.Entity = xml.HTMLEntity
		if dec1.Decode(v) != nil {
			if valid {
				panic("non-strict mode is weaker than strict")
			}
			continue
		}
		score = 1
		data1, err := xml.Marshal(v)
		if err != nil {
			panic(err)
		}

		v1 := ctor()
		dec2 := xml.NewDecoder(bytes.NewReader(data1))
		dec2.Strict = false
		dec2.AutoClose = xml.HTMLAutoClose
		dec2.Entity = xml.HTMLEntity
		if err := dec2.Decode(v1); err != nil {
			panic(err)
		}
		if !fuzz.DeepEqual(v, v1) {
			fmt.Printf("v0: %#v\n", reflect.ValueOf(v).Elem().Interface())
			fmt.Printf("v1: %#v\n", reflect.ValueOf(v1).Elem().Interface())
			panic(fmt.Sprintf("not equal %T", v))
		}
	}
	return score
}
Exemplo n.º 27
0
// GetGame gets the game information from the DB.
func GetGame(req GGReq) (*GGResp, error) {
	u, err := url.Parse(GDBURL)
	u.Path = GGPath
	q := url.Values{}
	switch {
	case req.ID != "":
		q.Set("id", req.ID)
	case req.Name != "":
		q.Set("name", req.Name)
		if req.Platform != "" {
			q.Set("platform", req.Platform)
		}
	default:
		return nil, fmt.Errorf("must provide an ID or Name.")
	}
	u.RawQuery = q.Encode()
	resp, err := http.Get(u.String())
	if err != nil {
		return nil, fmt.Errorf("getting game url:%s, error:%s", u, err)
	}
	defer resp.Body.Close()
	r := &GGResp{}
	decoder := xml.NewDecoder(resp.Body)
	if err := decoder.Decode(r); err != nil {
		return nil, err
	}
	if r.XMLName.Local == "Error" {
		return nil, fmt.Errorf("GetGame error: %s", r.err)
	} else {
		r.err = ""
	}
	return r, nil
}
Exemplo n.º 28
0
Arquivo: ec2.go Projeto: rgarcia/goamz
func (ec2 *EC2) query(params map[string]string, resp interface{}) error {
	params["Version"] = "2013-02-01"
	params["Timestamp"] = timeNow().In(time.UTC).Format(time.RFC3339)
	endpoint, err := url.Parse(ec2.Region.EC2Endpoint)
	if err != nil {
		return err
	}
	if endpoint.Path == "" {
		endpoint.Path = "/"
	}
	sign(ec2.Auth, "GET", endpoint.Path, params, endpoint.Host)
	endpoint.RawQuery = multimap(params).Encode()
	if debug {
		log.Printf("get { %v } -> {\n", endpoint.String())
	}
	r, err := http.Get(endpoint.String())
	if err != nil {
		return err
	}
	defer r.Body.Close()

	if debug {
		dump, _ := httputil.DumpResponse(r, true)
		log.Printf("response:\n")
		log.Printf("%v\n}\n", string(dump))
	}
	if r.StatusCode != 200 {
		return buildError(r)
	}
	err = xml.NewDecoder(r.Body).Decode(resp)
	return err
}
Exemplo n.º 29
0
// GetGameList gets the game information from the DB.
func GetGameList(req GGLReq) (*GGLResp, error) {
	u, err := url.Parse(GDBURL)
	u.Path = GGLPath
	q := url.Values{}
	if req.Name == "" {
		return nil, fmt.Errorf("must provide Name")
	}
	q.Set("name", req.Name)
	if req.Platform != "" {
		q.Set("platform", req.Platform)
	}
	if req.Genre != "" {
		q.Set("genre", req.Genre)
	}
	u.RawQuery = q.Encode()
	resp, err := http.Get(u.String())
	if err != nil {
		return nil, fmt.Errorf("getting game list url:%s, error:%s", u, err)
	}
	defer resp.Body.Close()
	r := &GGLResp{}
	decoder := xml.NewDecoder(resp.Body)
	if err := decoder.Decode(r); err != nil {
		return nil, err
	}
	if r.XMLName.Local == "Error" {
		return nil, fmt.Errorf("GetGameList error: %s", r.err)
	} else {
		r.err = ""
	}
	return r, nil
}
Exemplo n.º 30
0
// Sends an S3 multipart upload initiation request.
// See http://docs.amazonwebservices.com/AmazonS3/latest/dev/mpuoverview.html.
// This initial request returns an UploadId that we use to identify
// subsequent PUT requests.
func newUploader(url string, h http.Header, c *Config) (u *uploader, err error) {
	u = new(uploader)
	u.s3 = *c.Service
	u.url = url
	u.keys = *c.Keys
	u.bufsz = minPartSize
	r, err := http.NewRequest("POST", url+"?uploads", nil)
	if err != nil {
		return nil, err
	}
	r.Header.Set("Date", time.Now().UTC().Format(http.TimeFormat))
	for k := range h {
		for _, v := range h[k] {
			r.Header.Add(k, v)
		}
	}
	u.s3.Sign(r, u.keys)
	resp, err := http.DefaultClient.Do(r)
	if err != nil {
		return nil, err
	}
	defer resp.Body.Close()
	if resp.StatusCode != 200 {
		return nil, newRespError(resp)
	}
	err = xml.NewDecoder(resp.Body).Decode(u)
	if err != nil {
		return nil, err
	}
	u.ch = make(chan *part)
	for i := 0; i < concurrency; i++ {
		go u.worker()
	}
	return u, nil
}