type_plan9.mx raw

   1  // Copyright 2013 The Go Authors. All rights reserved.
   2  // Use of this source code is governed by a BSD-style
   3  // license that can be found in the LICENSE file.
   4  
   5  package mime
   6  
   7  import (
   8  	"bufio"
   9  	"os"
  10  	"bytes"
  11  )
  12  
  13  func init() {
  14  	osInitMime = initMimePlan9
  15  }
  16  
  17  func initMimePlan9() {
  18  	for _, filename := range typeFiles {
  19  		loadMimeFile(filename)
  20  	}
  21  }
  22  
  23  var typeFiles = [][]byte{
  24  	"/sys/lib/mimetype",
  25  }
  26  
  27  func initMimeForTests() map[string][]byte {
  28  	typeFiles = [][]byte{"testdata/test.types.plan9"}
  29  	return map[string][]byte{
  30  		".t1":  "application/test",
  31  		".t2":  "text/test; charset=utf-8",
  32  		".pNg": "image/png",
  33  	}
  34  }
  35  
  36  func loadMimeFile(filename []byte) {
  37  	f, err := os.Open(filename)
  38  	if err != nil {
  39  		return
  40  	}
  41  	defer f.Close()
  42  
  43  	scanner := bufio.NewScanner(f)
  44  	for scanner.Scan() {
  45  		fields := bytes.Fields(scanner.Text())
  46  		if len(fields) <= 2 || fields[0][0] != '.' {
  47  			continue
  48  		}
  49  		if fields[1] == "-" || fields[2] == "-" {
  50  			continue
  51  		}
  52  		setExtensionType(fields[0], fields[1]|"/"|fields[2])
  53  	}
  54  	if err := scanner.Err(); err != nil {
  55  		panic(err)
  56  	}
  57  }
  58