Ejemplo n.º 1
0
    def do_POST(self):
	global parser
	clen,pdict = cgi.parse_header(self.headers.getheader('Content-Length'))
	mtype,pdict = cgi.parse_header(self.headers.getheader('Content-Type'))
	data = self.rfile.read(int(clen))
        # print "POST path: %s" % self.path
        response = 200
	if self.path.startswith('/upload'):
		qm = self.path.find('?')
		if qm > 0:
			qd = parse_qs(self.path[qm+1:])
			name = qd['name'][0]
			drive = qd['drive'][0]
                        print "upload drive: %s name: %s" % (drive, name)
			# fileName = tempfile.mktemp(prefix=name.split('/')[-1].split('.')[0], suffix='.'+name.split('.')[-1])
                        fileName = os.path.join(tempfile.gettempdir(), name)
                        print fileName
			with open(fileName, 'wb') as f:
                                comma = data.index(',')
                                f.write(base64.b64decode(data[comma+1:]))
			data = 'dw disk insert %s %s' % (drive, fileName)
                        response = 200
                        msg = "OK: drive:%s name:%s" % (drive, name)
                else:
                        response = 404
                        msg = "%d: Error: Invalid upload specification: %s" % (response, self.path)
                        self._set_headers('text/html', response)
                        self.wfile.write("<html><body><h1>%s</h1></body></html>" % (msg))
                        return
        result = parser.parse(data.lstrip().rstrip()).replace('\r', '')
        self._set_headers('text/plain', response)
        self.wfile.write(result + '\n')
Ejemplo n.º 2
0
	def do_POST(self):
		contentType, ct_dict = cgi.parse_header(
			self.headers.getheader('content-type'))

		length_str,cl_dict = cgi.parse_header(
			self.headers.getheader('content-length'))
        
		length = int(length_str)
		form_data = cgi.parse_qs(self.rfile.read(length))

		new_data = form_data['data'][0]
		user = form_data['user'][0]
		passwd = form_data['pass'][0]

		print form_data
		msg = "Set data: %s" % new_data

		if user in UsenixHttpHandler.data:
			usr_pass,old_data = UsenixHttpHandler.data[user]
			if(usr_pass != passwd):
				msg = "Invalid password!"
			else:
				msg += ", old data was %s" % old_data
				UsenixHttpHandler.data[user] = (usr_pass,new_data)
			if(form_data['debug'][0] != "false"):
				msg = "Debug: %s" % old_data
		else:
			UsenixHttpHandler.users.append(form_data['user'][0])
			UsenixHttpHandler.data[user] = (passwd,new_data)

    
		self.send_response(200)
		self.send_header('content-type', 'text/html')
		self.end_headers()
		self.wfile.write(msg)
Ejemplo n.º 3
0
    def do_POST(self):
        try:
            if self.path.endswith('/restaurant/new'):

                ctype, pdict = cgi.parse_header(self.headers.getheader('Content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    print fields
                    print fields.get('restaurant')[0]

                    new_restaurant = Restaurant(name = fields.get('restaurant')[0])
                    session.add(new_restaurant)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type' , 'text/html')
                    self.send_header('Location' , '/restaurant')
                    self.end_headers()

            if self.path.endswith('/edit'):

                ctype, pdict = cgi.parse_header(self.headers.getheader('Content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                content = fields.get('restaurant')[0]
                IDpath = self.path.split('/')[2]

                query = session.query(Restaurant).filter_by(id = IDpath).one()

                if query != []:
                    query.name = content
                    session.add(query)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type' , 'text/html')
                    self.send_header('Location' , '/restaurant')
                    self.end_headers()

            if self.path.endswith('/delete'):

                ctype, pdict = cgi.parse_header(self.headers.getheader('Content-type'))
                IDpath = self.path.split('/')[2]

                query = session.query(Restaurant).filter_by(id = IDpath).one()

                if query != []:
                    session.delete(query)
                    session.commit()
                    self.send_response(301)
                    self.send_header('Content-type' , 'text/html')
                    self.send_header('Location' , '/restaurant')
                    self.end_headers()

            print ('POST Request')



        except:
            pass
Ejemplo n.º 4
0
def _upload_fields():
    '''Gets a list of files from the request.
    Uses Flask's request.files to get all files, unless the Content-Type is
    `text/csv` or `text/plain`: then returns the request body as a FileStorage
    object, attempting to use Content-Disposition to get a file name.

      :returns: List of tuples of:
                (field_name, `werkzeug.datastructures.FileStorage`)
    '''
    result = []
    content_type = request.headers.get('content-type')
    mime_type, _ = parse_header(content_type)

    # special case for  application/xml; name="Sample 9.16.16.csv"
    is_app_xml_sap = "application/xml" in content_type

    if mime_type in ('text/plain', 'text/csv') or is_app_xml_sap:
        _, params = parse_header(
            request.headers.get('content-disposition', '')
        )
        filename = params.get('filename', 'noname.txt')
        fileo = FileStorage(stream=StringIO(request.data),
                            filename=filename,
                            content_type=request.headers.get('content-type'))
        result.append(('file', fileo))
    else:
        for key, value in request.files.iteritems():
            if not isinstance(value, unicode):
                result.append((key, value))
    return result
    def do_POST(self):
        try:
            if self.path == "/restaurants/new":
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader("content-type")
                )
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    new_restaurant_name = fields.get("new_restaurant")[0]
                    new_restaurant = Restaurant(name=new_restaurant_name)
                    session.add(new_restaurant)
                    session.commit()
                
                self.send_response(301)
                self.send_header("Content-type", "text/html")
                self.send_header("Location", "/restaurants")
                self.end_headers()
                return

            if re.match('^\/restaurants\/[0-9]+\/edit$', self.path):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader("content-type")
                )
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    new_name = fields.get("new_restaurant_name")[0]
                    restaurant_id = int(self.path.split("/")[2])
                    restaurant = session.query(Restaurant) \
                                        .filter_by(id=restaurant_id) \
                                        .one()
                    restaurant.name = new_name
                    session.add(restaurant)
                    session.commit()
                
                self.send_response(301)
                self.send_header("Content-type", "text/html")
                self.send_header("Location", "/restaurants")
                self.end_headers()
                return

            if re.match('^\/restaurants\/[0-9]+\/delete$', self.path):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader("content-type")
                )
                if ctype == "multipart/form-data":
                    restaurant_id = int(self.path.split("/")[2])
                    restaurant = session.query(Restaurant) \
                                        .filter_by(id=restaurant_id) \
                                        .one()
                    session.delete(restaurant)
                    session.commit()
                
                self.send_response(301)
                self.send_header("Content-type", "text/html")
                self.send_header("Location", "/restaurants")
                self.end_headers()
                return

        except:
            pass
Ejemplo n.º 6
0
 def _set_mime_headers(self, headers):
     for name, value in headers:
         name = name.lower()
         if name == 'project-id-version':
             parts = value.split(' ')
             self.project = u' '.join(parts[:-1])
             self.version = parts[-1]
         elif name == 'report-msgid-bugs-to':
             self.msgid_bugs_address = value
         elif name == 'last-translator':
             self.last_translator = value
         elif name == 'language-team':
             self.language_team = value
         elif name == 'content-type':
             mimetype, params = parse_header(value)
             if 'charset' in params:
                 self.charset = params['charset'].lower()
         elif name == 'plural-forms':
             _, params = parse_header(' ;' + value)
             self._num_plurals = int(params.get('nplurals', 2))
             self._plural_expr = params.get('plural', '(n != 1)')
         elif name == 'pot-creation-date':
             self.creation_date = _parse_datetime_header(value)
         elif name == 'po-revision-date':
             # Keep the value if it's not the default one
             if 'YEAR' not in value:
                 self.revision_date = _parse_datetime_header(value)
Ejemplo n.º 7
0
    def parse_response(self, response):
        ctype = response.headers.get('content-type', '')
        ctype, pdict = cgi.parse_header(ctype)
        if ctype in self.contentTypes:
            return xmlrpclib.Transport.parse_response(self, response)
        elif ctype != self.mixedType:
            raise xmlrpclib.ResponseError(
                    "Response has invalid or missing Content-Type")
        decoder = MultipartDecoder(response, pdict['boundary'])

        # Read XMLRPC response
        rpcHeaders, rpcBody = decoder.get()
        if (cgi.parse_header(rpcHeaders.get('content-type'))[0]
                not in self.contentTypes):
            raise xmlrpclib.ResponseError(
                    "Response has invalid or missing Content-Type")
        rpcBody = StringIO.StringIO(rpcBody)
        result = xmlrpclib.Transport.parse_response(self, rpcBody)

        # Replace the URL in the XMLRPC response with a file-like object that
        # reads out the second part of the multipart response
        csResponse = decoder.getStream()
        if csResponse.headers.get('content-type') != (
                'application/x-conary-change-set'):
            raise xmlrpclib.ResponseError(
                    "Response body has wrong Content-Type")
        result[0][1][0] = csResponse
        return result
Ejemplo n.º 8
0
    def do_POST(self):
        try:
            if self.path.endswith("/delete"):
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))

                restaurantIDPath = self.path.split("/")[2]
                myRestaurantQuery = session.query(Restaurant).filter_by(id=
                                    restaurantIDPath).one()
                if myRestaurantQuery != []:
                    session.delete(myRestaurantQuery)
                    session.commit()
                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

            if self.path.endswith("/edit"):
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('newRestaurantName')
                restaurantIDPath = self.path.split("/")[2]

                myRestaurantQuery = session.query(Restaurant).filter_by(id=
                                    restaurantIDPath).one()
                if myRestaurantQuery != []:
                    myRestaurantQuery.name = messagecontent[0]
                    session.add(myRestaurantQuery)
                    session.commit()
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

            if self.path.endswith("/restaurants/new"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('name')
                newResto = Restaurant(name = messagecontent[0])
                session.add(newResto)
                session.commit()
                print "creating output"
                output = ""
                output += "<html><body>"
                output += " <h2>Restaurant added!</h2>"
                restolist = session.query(Restaurant).all()
                for resto in restolist:
                    output += ("</br>%s</br>" % resto.name)
                    output += "<a href=#>Edit</a></br>"
                    output += "<a href=#>Delete</a>"
                output += "</body></html>"
                self.wfile.write(output)
                print output

        except:
            pass
Ejemplo n.º 9
0
 def _set_mime_headers(self, headers):
     for name, value in headers:
         if name.lower() == 'content-type':
             mimetype, params = parse_header(value)
             if 'charset' in params:
                 self.charset = params['charset'].lower()
             break
     for name, value in headers:
         name = name.lower().decode(self.charset)
         value = value.decode(self.charset)
         if name == 'project-id-version':
             parts = value.split(' ')
             self.project = u' '.join(parts[:-1])
             self.version = parts[-1]
         elif name == 'report-msgid-bugs-to':
             self.msgid_bugs_address = value
         elif name == 'last-translator':
             self.last_translator = value
         elif name == 'language-team':
             self.language_team = value
         elif name == 'plural-forms':
             _, params = parse_header(' ;' + value)
             self._num_plurals = int(params.get('nplurals', 2))
             self._plural_expr = params.get('plural', '(n != 1)')
         elif name == 'pot-creation-date':
             # FIXME: this should use dates.parse_datetime as soon as that
             #        is ready
             value, tzoffset, _ = re.split('[+-](\d{4})$', value, 1)
             tt = time.strptime(value, '%Y-%m-%d %H:%M')
             ts = time.mktime(tt)
             tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 +
                                            int(tzoffset[2:]))
             dt = datetime.fromtimestamp(ts)
             self.creation_date = dt.replace(tzinfo=tzoffset)
Ejemplo n.º 10
0
 def _set_mime_headers(self, headers):
     for name, value in headers:
         if name.lower() == "content-type":
             mimetype, params = parse_header(value)
             if "charset" in params:
                 self.charset = params["charset"].lower()
             break
     for name, value in headers:
         name = name.lower().decode(self.charset)
         value = value.decode(self.charset)
         if name == "project-id-version":
             parts = value.split(" ")
             self.project = u" ".join(parts[:-1])
             self.version = parts[-1]
         elif name == "report-msgid-bugs-to":
             self.msgid_bugs_address = value
         elif name == "last-translator":
             self.last_translator = value
         elif name == "language-team":
             self.language_team = value
         elif name == "plural-forms":
             _, params = parse_header(" ;" + value)
             self._num_plurals = int(params.get("nplurals", 2))
             self._plural_expr = params.get("plural", "(n != 1)")
         elif name == "pot-creation-date":
             # FIXME: this should use dates.parse_datetime as soon as that
             #        is ready
             value, tzoffset, _ = re.split("[+-](\d{4})$", value, 1)
             tt = time.strptime(value, "%Y-%m-%d %H:%M")
             ts = time.mktime(tt)
             tzoffset = FixedOffsetTimezone(int(tzoffset[:2]) * 60 + int(tzoffset[2:]))
             dt = datetime.fromtimestamp(ts)
             self.creation_date = dt.replace(tzinfo=tzoffset)
Ejemplo n.º 11
0
    def do_POST(self):
        if self.path.endswith("/restaurants/new"):
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('name')
                utils.create_restaurant(name=messagecontent[0])
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

        if self.path.endswith("/edit"):
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
            if ctype == 'multipart/form-data':
                fields = cgi.parse_multipart(self.rfile, pdict)
                messagecontent = fields.get('name')
                id = self.path.split("/")[2]
                utils.rename_restaurant(id, messagecontent[0])
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()
                
        if self.path.endswith("/delete"):
            id = self.path.split("/")[2]
            utils.delete_restaurant_by_id(id)
            self.send_response(301)
            self.send_header('Content-type', 'text/html')
            self.send_header('Location', '/restaurants')
            self.end_headers()
Ejemplo n.º 12
0
 def do_POST(s):
     try:
         s.path = s.path[1:]
         response = ""
         if s.path not in webscript.scripts:
             raise Exception((403, "Forbidden resource"))
         ctype, pdict = cgi.parse_header(s.headers.getheader("content-type"))
         length = int(cgi.parse_header(s.headers.getheader("content-length"))[0])
         if ctype == "multipart/form-data":
             string = s.rfile.read(length)
             fp = cStringIO.StringIO(string)
             query = cgi.parse_multipart(fp, pdict)
         else:
             string = s.rfile.read(length)
             fp = cStringIO.StringIO(string)
             query = cgi.parse_qs(string, 1)
         type, response = webscript.scripts[s.path](query)
         if _debug:
             print type, len(response), response[:100]
         s.send_response(200)
         s.send_header("Content-Length", response and len(response) or 0)
         if response and type:
             s.send_header("Content-Type", type)
         s.end_headers()
         if response:
             s.wfile.write(response)
     except Exception, e:
         print "Exception:", e
         if e.message and len(e.message) == 2:
             s.send_error(e.message[0], e.message[1])
         else:
             s.send_error(500, str(e.message))
Ejemplo n.º 13
0
    def do_POST(self):

        try:
            if self.path.endswith("restaurants/new"):
                print "made it into the logic"
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type')
                )
                print "ctype = %s" % ctype
                if ctype == 'application/x-www-form-urlencoded':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('newRestaurantname')
                    print restaurant_name[0]
                    print restaurant_name
                    new_restaurant = Restaurant(name=restaurant_name[0])
                    session.add(new_restaurant)
                    session.commit()

            if self.path.endswith("/edit") or self.path.endswith("/delete"):
                self.send_response(301)

                if self.path.endswith("/edit"):
                    ctype, pdict = cgi.parse_header(
                        self.headers.getheader('content-type')
                    )
            self.send_response(301)
            self.send_header('Content-type', 'text/html')
            self.send_header('Location', '/restaurants')
            self.end_headers()
            return

        except Exception:
            print "Had an error"
Ejemplo n.º 14
0
    def do_POST(self):

        try:
            if self.path.endswith("/restaurants/new"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))

                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newRestaurantName')

                    addRestaurant(messagecontent[0])

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()


            if self.path.endswith("/delete"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurantIDPath = self.path.split("/")[2]

                    myRestaurantQuery = session.query(Restaurant).filter_by(
                        id=restaurantIDPath).one()

                    if myRestaurantQuery != []:
                        session.delete(myRestaurantQuery)
                        session.commit()

                        self.send_response(301)
                        self.send_header('Content-type', 'text/html')
                        self.send_header('Location', '/restaurants')
                        self.end_headers()



            if self.path.endswith("/edit"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newRestaurantName')
                    restaurantIDPath = self.path.split("/")[2]

                    myRestaurantQuery = session.query(Restaurant).filter_by(
                        id=restaurantIDPath).one()
                    if myRestaurantQuery != []:
                        myRestaurantQuery.name = messagecontent[0]
                        session.add(myRestaurantQuery)
                        session.commit()
                        self.send_response(301)
                        self.send_header('Content-type', 'text/html')
                        self.send_header('Location', '/restaurants')
                        self.end_headers()
        except:
            pass
Ejemplo n.º 15
0
    def do_POST(self):
	"""
	grab 'command' from the form data and send to server.
	"""
        contentType, pDict = cgi.parse_header(self.headers.getheader('content-type'))
	contentLength, pDict = cgi.parse_header(self.headers.getheader('content-length'))
	print contentLength
	if contentType == 'multipart/form-data':
                print self.headers.type
                env={}
                env['REQUEST_METHOD'] = self.command
                if self.headers.typeheader is None:
                    env['CONTENT_TYPE'] = self.headers.type
                else:
                    env['CONTENT_TYPE'] = self.headers.typeheader

                    # -- Save file (numbered to avoid overwriting, ex: foo-3.png)
                form = DroopyFieldStorage(fp = self.rfile, environ = env);
                
                file_items=form['upfile']
                print file_items.filename
                print file_items.file.getvalue()
                qstring = self.rfile.read(int(contentLength)) 
		query=urlparse.parse_qs(qstring)
		open("a.txt","w").write(str(query))
           	#query=cgi.parse_multipart(self.rfile, parseDict)
	else:
		#TODO The else should be 'xxx-application'
		qstring = self.rfile.read(int(contentLength)) 
		query=urlparse.parse_qs(qstring)
	self.end_headers()
	self.handleMessage(query)
    def do_POST(self):
        try:
            if self.path.endswith("/restaurants/new"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newRestaurant')

                myNewRestaurant = Restaurant(name=messagecontent[0])
                session.add(myNewRestaurant)
                session.commit()

                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

            if self.path.endswith("/edit"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newName')

                # UPDATE
                laPath = self.path.split('/')
                print laPath  # ['', 'restaurants', '4', 'edit']
                laPathId = laPath[2]
                myRestaurant = session.query(Restaurant).filter_by(id=laPathId).one()
                if myRestaurant != []:
                    myRestaurant.name=messagecontent[0]
                    print myRestaurant.name
                    session.add(myRestaurant)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

            if self.path.endswith("/delete"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))

                # UPDATE: regardless of the ctype content, delete
                laPath = self.path.split('/')
                print laPath  # ['', 'restaurants', '4', 'edit']
                laPathId = laPath[2]
                myRestaurant = session.query(Restaurant).filter_by(id=laPathId).one()
                if myRestaurant != []:
                    session.delete(myRestaurant)
                    session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()
        except:
            pass
Ejemplo n.º 17
0
def get_message_from_urllib_addinfourl(http_response, in_response_to):
    """ This function should not be called by libtaxii users directly. """
    info = http_response.info()

    if hasattr(info, 'getheader'):
        taxii_content_type = info.getheader('X-TAXII-Content-Type')
        _, params = cgi.parse_header(info.getheader('Content-Type'))
    else:
        taxii_content_type = info.get('X-TAXII-Content-Type')
        _, params = cgi.parse_header(info.get('Content-Type'))

    encoding = params.get('charset', 'utf-8')
    response_message = http_response.read()

    if taxii_content_type is None:  # Treat it as a Failure Status Message, per the spec

        message = []
        header_dict = six.iteritems(http_response.info().dict)
        for k, v in header_dict:
            message.append(k + ': ' + v + '\r\n')
        message.append('\r\n')
        message.append(response_message)

        m = ''.join(message)

        return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m)

    elif taxii_content_type == VID_TAXII_XML_10:  # It's a TAXII XML 1.0 message
        return tm10.get_message_from_xml(response_message, encoding)
    elif taxii_content_type == VID_TAXII_XML_11:  # It's a TAXII XML 1.1 message
        return tm11.get_message_from_xml(response_message, encoding)
    elif taxii_content_type == VID_CERT_EU_JSON_10:
        return tm10.get_message_from_json(response_message, encoding)
    else:
        raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
Ejemplo n.º 18
0
 def extract(self, response):
     # If the HTTP "Content-Type" header specifies an encoding, try
     # to use it to decode the document.
     params = cgi.parse_header(
         response.headers.getRawHeaders('Content-Type', [''])[0])[1]
     content = yield read_body(response, max_bytes=self.max_download_bytes)
     soup = BeautifulSoup(content, self.parser,
                          from_encoding=params.get('charset'))
     if self.ignore_noscript:
         for noscript in soup('noscript'):
             noscript.clear()
     # Handle any <meta> refreshes.
     for refresh in soup('meta', attrs=META_REFRESH_ATTRS):
         seconds, params = cgi.parse_header(refresh['content'])
         try:
             seconds = int(seconds, 10)
         except ValueError:
             # Not a valid number; just pretend it's zero.
             seconds = 0
         if seconds <= self.max_refresh_delay and 'url' in params:
             returnValue(Redirect(params['url']))
     if soup.title:
         # Join twice: once because soup.title.strings is an iterator
         # and once after splitting to coalesce whitespace.
         returnValue(u' '.join(u''.join(soup.title.strings).split()))
     returnValue(None)
Ejemplo n.º 19
0
def download_file(uri, verify_ssl=True, timeout=5):

    response = requests.get(uri, verify=verify_ssl, timeout=timeout)
    content = response.content

    filename = uri
    encoding = None
    mime_type = None

    # Extract data from headers to help plugin + encoding detection, if
    # available
    headers = response.headers
    if 'content-type' in headers:
        mime_type, options = cgi.parse_header(headers['content-type'])
        encoding = options.get('charset', encoding)
    if 'content-disposition' in headers:
        _, options = cgi.parse_header(headers['content-disposition'])
        filename = options.get('filename', filename)

    # TODO: may send only a sample (chardet can be very slow if content is big)
    source = detect_local_source(filename, content, mime_type, encoding)

    # Save file locally
    extension = extension_by_plugin_name(source.plugin_name)
    tmp = tempfile.NamedTemporaryFile()
    filename = '{}.{}'.format(tmp.name, extension)
    tmp.close()
    with open(filename, 'wb') as fobj:
        fobj.write(content)

    return Source(uri=filename,
                  plugin_name=source.plugin_name,
                  encoding=source.encoding,
                  delete=True)
Ejemplo n.º 20
0
def postscript(env, start_response):
    try:
        path = env['PATH_INFO']; response = ''
        print 'path=', path
        if path not in webscript.scripts: raise Exception((403, 'Forbidden resource'))
        ctype, pdict = cgi.parse_header(env.get('CONTENT_TYPE',''))
        length = int(cgi.parse_header(env.get('CONTENT_LENGTH', '0'))[0])
        if ctype == 'multipart/form-data':
            string = env['wsgi.input'].read(length)
            fp = cStringIO.StringIO(string)
            query = cgi.parse_multipart(fp, pdict)
        else:
            string = env['wsgi.input'].read(length)
            fp = cStringIO.StringIO(string)
            query = cgi.parse_qs(string, 1)
        type, response = webscript.scripts[path](query)
        if _debug: print type, len(response), response[:100]
        headers = [('Content-Length', str(len(response)) if response else '0')]
        if response and type: headers.append(('Content-Type', type))
        start_response('200 OK', headers)
        if response: return [str(response)]
        else: return []
    except Exception, e:
        print 'Exception:', e
        if e.message and len(e.message) == 2: start_response(str(e.message[0]) + ' ' + e.message[1], [])
        else: start_response('500 ' + str(e.message))
        return []
Ejemplo n.º 21
0
    def do_POST(self):
        try:
            if self.path.endswith("/restaurants/new"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newRestaurantName')

                newRestaurant=Restaurant(name=messagecontent[0])
                session.add(newRestaurant)
                session.commit()

                output=""
                output+='''<html><head><meta http-equiv="refresh" content="2;url=/restaurants" ></head><body>'''
                output+="Add "+messagecontent[0]+" into database <b>Success!</b>"
                output+="</body></html>"
                self.wfile.write(output)
                return

            elif self.path.endswith("/edit"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('newRestaurantName')
                    restaurantIDPath = self.path.split("/")[2]

                    myRestaurantQuery = session.query(Restaurant).filter_by(
                        id=restaurantIDPath).one()
                    if myRestaurantQuery != []:
                        myRestaurantQuery.name = messagecontent[0]
                        session.add(myRestaurantQuery)
                        session.commit()
                        self.send_response(301)
                        self.send_header('Content-type', 'text/html')
                        self.send_header('Location', '/restaurants')
                        self.end_headers()

            elif self.path.endswith("/delete"):
                restaurantIDPath = self.path.split("/")[2]
                myRestaurantQuery = session.query(Restaurant).filter_by(id=restaurantIDPath).one()
                if myRestaurantQuery != []:
                    session.delete(myRestaurantQuery)
                    session.commit()
                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()



        except:
            output=""
            output+='''<html><body>500 Internal Server Error</body></html>'''
            self.wfile.write(output)
            return
Ejemplo n.º 22
0
def get_message_from_urllib2_httperror(http_response, in_response_to):
    """ This function should not be called by libtaxii users directly. """
    info = http_response.info()

    if hasattr(info, 'getheader'):
        taxii_content_type = info.getheader('X-TAXII-Content-Type')
        _, params = cgi.parse_header(info.getheader('Content-Type'))
    else:
        taxii_content_type = info.get('X-TAXII-Content-Type')
        _, params = cgi.parse_header(info.get('Content-Type'))

    encoding = params.get('charset', 'utf-8')
    response_message = http_response.read()

    if taxii_content_type is None:
        m = str(http_response) + '\r\n' + str(http_response.info()) + '\r\n' + response_message
        return tm11.StatusMessage(message_id='0', in_response_to=in_response_to, status_type=ST_FAILURE, message=m)
    elif taxii_content_type == VID_TAXII_XML_10:  # It's a TAXII XML 1.0 message
        return tm10.get_message_from_xml(response_message, encoding)
    elif taxii_content_type == VID_TAXII_XML_11:  # It's a TAXII XML 1.1 message
        return tm11.get_message_from_xml(response_message, encoding)
    elif taxii_content_type == VID_CERT_EU_JSON_10:
        return tm10.get_message_from_json(response_message, encoding)
    else:
        raise ValueError('Unsupported X-TAXII-Content-Type: %s' % taxii_content_type)
 def do_POST(self):
     if None != re.search('/startup', self.path):
         ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
         if ctype == 'application/json':
             data = self.get_json_data()
             if db.insert_startup(data):
                 self.send_response(200)
                 self.end_headers()
             else:
                 self.send_response(403)
                 self.end_headers()
     elif None != re.search('/handover', self.path):
         ctype, pdict = cgi.parse_header(self.headers.get('content-type'))
         if ctype == 'application/json':
             data = self.get_json_data()
             if db.insert_handover(data):
                 self.send_response(200)
                 self.end_headers()
             else:
                 self.send_response(403)
                 self.end_headers()
     else:
         self.send_response(403)
         self.send_header('Content-Type', 'application/json')
         self.end_headers()
         # self.wfile.write(bytes(json.dumps({3: 4}), 'UTF-8'))
     return
Ejemplo n.º 24
0
    def do_POST(self):
        try:
            if self.path.endswith("/shelters/create"):

                ctype, pdict = cgi.parse_header(self.headers.getheader("content-type"))
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    shelter_name = fields.get("sheltername")[0]
                    address = fields.get("address")[0]
                    city = fields.get("city")[0]
                    state = fields.get("state")[0]
                    zipcode = fields.get("zipcode")[0]
                    website = fields.get("website")[0]
                    if shelter_name:
                        new_shelter = Shelter(
                            name=shelter_name, address=address, city=city, state=state, zipCode=zipcode, website=website
                        )
                        session.add(new_shelter)
                        session.commit()
                        self.send_response(301)
                        self.send_header("Content-type", "text/html")
                        self.end_headers()
                        output = ""
                        output += "<html><body>"
                        output += " <h2> A new Shelter named has been entered to the database: </h2>"
                        output += "<h1>{0}</h1>".format(shelter_name)
                        output += "<h1>{0}</h1>".format(address)
                        output += "<a href=/shelters>Home</a>"
                        output += "</body></html>"
                        self.wfile.write(output)

            if self.path.endswith("/edit"):
                # print(self.path.split('/'))
                shelter_id = int(self.path.split("/")[2])
                ctype, pdict = cgi.parse_header(self.headers.getheader("content-type"))
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    new_shelter_name = fields.get("newsheltername")[0]
                    shelter = session.query(Shelter).filter_by(id=shelter_id)
                    for se in shelter:
                        se.name = new_shelter_name
                    session.commit()
                    self.send_response(301)
                    self.send_header("Location", "/shelters")
                    self.end_headers()
            if self.path.endswith("/delete"):
                shelter_id = int(self.path.split("/")[2])
                ctype, pdict = cgi.parse_header(self.headers.getheader("content-type"))
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    shelter_to_delete = session.query(Shelter).filter_by(id=shelter_id).one()
                    session.delete(shelter_to_delete)
                    session.commit()
                    self.send_response(301)
                    self.send_header("Location", "/shelters")
                    self.end_headers()
                    sefl.wfile.write("hello world")

        except:
            pass
Ejemplo n.º 25
0
    def do_POST(self):
        try:
            if self.path.endswith("/new"): # NEW POST REQUEST
                ctype, pdict = cgi.parse_header(self.headers.getheader('Content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    namecontent = fields.get('name')
                new_restaurant = Restaurant(name=namecontent[0])
                session.add(new_restaurant)
                session.commit()
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurant')
                self.end_headers()

            if self.path.endswith("/edit"):  # EDIT POST REQUEST
                ctype, pdict = cgi.parse_header(self.headers.getheader('Content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    namecontent = fields.get('name')
                restaurant_id = self.path.split('/')[2]
                restaurant_exists = session.query(Restaurant)\
                    .filter_by(id=restaurant_id).one()
                if restaurant_exists != []:
                    restaurant_exists.name = namecontent[0]
                    session.add(restaurant_exists)
                    session.commit()
                session.query(Restaurant).filter(Restaurant.id == restaurant_id).\
                    update({'name': namecontent[0]})
                session.commit()
                output = '<html><body>'
                output += '<h2> Okay, how about this? </h2>'
                output += '<h1> %s </h1>' %namecontent[0]
                output += '<form method="POST" enctype="multipart/form-data" ' \
                          'action="/restaurant/%s/edit">' \
                          '<h2>Edit restaurant name and hit submit:</h2> ' \
                          '<input name="name" type="text"> ' \
                          '<input type="submit" value="Submit"> </form> ' \
                          '<a href="/restaurant">Restaurant List</a>' \
                          %restaurant_id
                output += '</body></html>'
                self.wfile.write(output)

            if self.path.endswith("/delete"):  # DELETE POST REQUEST
                restaurant_id = self.path.split('/')[2]
                print("here")
                restaurant_exists = session.query(Restaurant)\
                    .filter_by(id=restaurant_id).one()
                if restaurant_exists:
                    print(restaurant_exists.id)
                    session.delete(restaurant_exists)
                    session.commit()
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurant')
                self.end_headers()

        except:
            pass
Ejemplo n.º 26
0
    def do_POST(self):
        try:
            if self.path.endswith("/new"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('message')
                addRestaurant(messagecontent[0])
                message = ""
                message += "<html><body>"
                message += "<h1> %s added!</h1>" % messagecontent[0]
                message += '''<form method='POST' enctype='multipart/form-data' action='/new'><h1>Add new restaurant?</h1><input name="message" type="text" ><input type="submit" value="Add"> </form>'''
                message += "</body></html>"
                self.wfile.write(message)
                self.wfile.write(output)
                print output
            if self.path.endswith("/edit_restaurant_name"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('message')
                addRestaurant(messagecontent[0])
                message = ""
                message += "<html><body>"
                message += '''<form method='POST' enctype='multipart/form-data' action='/edit_restaurant_name'><input name="message" type="text"><input type="submit" value="Edit name"></form>'''
                message += "</body></html>"
                self.wfile.write(message)
                self.wfile.write(output)
                print output
            if self.path.endswith("/delete_restaurant"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('message')
                addRestaurant(messagecontent[0])
                message = ""
                message += "<html><body>"
                message += '''<h1>Added!</h1>'''
                message += '''<form method='POST' enctype='multipart/form-data' action='/new'><h1>Add new restaurant?</h1><input name="message" type="text" ><input type="submit" value="Add"> </form>'''
                message += "</body></html>"
                self.wfile.write(message)
                self.wfile.write(output)
                print output


        except:
            pass
Ejemplo n.º 27
0
    def do_POST(self):
        try:
            # Similar to Edit, Find Delete Page
            if self.path.endswith("/delete"):
                restaurantIDPath = self.path.split("/")[2]
                # Query to find Object with Matching ID
                myRestaurantQuery = session.query(Restaurant).filter_by(id=restaurantIDPath).one()
                if myRestaurantQuery:
                    # Session to Delete and Commit
                    session.delete(myRestaurantQuery)
                    session.commit()
                    # Redirect
                    self.send_response(301)
                    self.send_header("Content-type", "text/html")
                    self.send_header("Location", "/restaurants")
                    self.end_headers()
                    # If Statement to Find Edit Page
            if self.path.endswith("/edit"):
                # Grab Input
                ctype, pdict = cgi.parse_header(self.headers.getheader("content-type"))
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get("newRestaurantName")
                    restaurantIDPath = self.path.split("/")[2]
                    # Perform Query to Find Object with Matching ID
                    myRestaurantQuery = session.query(Restaurant).filter_by(id=restaurantIDPath).one()
                    # Reset Name Field to Entry
                    if myRestaurantQuery != []:
                        myRestaurantQuery.name = messagecontent[0]
                        # Add to Session and Commit
                        session.add(myRestaurantQuery)
                        session.commit()
                        # Redirect Back to Restaurant Page
                        self.send_response(301)
                        self.send_header("Content-type", "text/html")
                        self.send_header("Location", "/restaurants")
                        self.end_headers()

                        # If Statement Looking for New Page
            if self.path.endswith("/restaurants/new"):
                # Extract Information for Form
                ctype, pdict = cgi.parse_header(self.headers.getheader("content-type"))
                if ctype == "multipart/form-data":
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get("newRestaurant")

                    # Create New Restaurant Class
                    newRestaurant = Restaurant(name=messagecontent[0])
                    session.add(newRestaurant)
                    session.commit()

                    self.send_response(301)
                    self.send_header("Content-type", "text/html")
                    # Redirect
                    self.send_header("Location", "/restaurants")
                    self.end_headers()

        except:
            pass
Ejemplo n.º 28
0
    def do_POST(self):
        try:
            if self.path.endswith("/restaurants/new"):
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('restaurantName')[0]

                # Create new Restaurant class
                new_restaurant = Restaurant(name = restaurant_name)
                session.add(new_restaurant)
                session.commit()

                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

                return

            if self.path.endswith("/edit"):
                ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('restaurantName')[0]

                # get id from path    
                restaurant_id = self.path.split("/")[2]

                # Find and rename the Restaurant
                restaurant = session.query(Restaurant).filter_by(id = restaurant_id)[0]
                restaurant.name = restaurant_name
                session.add(restaurant)
                session.commit()

                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()


            if self.path.endswith("/delete"):
                # get id from path    
                restaurant_id = self.path.split("/")[2]

                # Find and delete the Restaurant
                restaurant = session.query(Restaurant).filter_by(id = restaurant_id)[0]
                if restaurant != [] :
                    session.delete(restaurant)
                    session.commit()

                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()


        except:
            pass
    def do_POST(self):
        try:
            if self.path.endswith("/restaurants/new"):
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('restaurant_name')
                # Create a new restaurant object
                new_restaurant = Restaurant(name=restaurant_name[0])
                session.add(new_restaurant)
                session.commit()
                print("New restaurant {} created!".format(restaurant_name[0]))
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

            # Objective 4 - Edit POST
            if self.path.endswith("/edit"):
                input_id = self.path.split("/")[2]
                ctype, pdict = cgi.parse_header(
                    self.headers.getheader('content-type'))
                if ctype == 'multipart/form-data':
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    restaurant_name = fields.get('restaurant_edit_name')
                # Get the restaurant to edit
                restaurant = session.query(Restaurant).filter_by(
                    id=input_id).one()
                if restaurant:
                    old_name = restaurant.name
                    restaurant.name = restaurant_name[0]
                    session.add(restaurant)
                    session.commit()
                    print("Restaurant {0} changer to {1}!".format(
                        old_name, restaurant_name[0]))

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

            # Objective 5 - Delete POST
            if self.path.endswith("/delete"):
                input_id = self.path.split("/")[2]
                # Get the restaurant to delete
                restaurant = session.query(Restaurant).filter_by(
                    id=input_id).one()
                if restaurant:
                    session.delete(restaurant)
                    session.commit()
                    print("Restaurant {0} deleted!".format(restaurant.name))
                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()
        except:
            pass
Ejemplo n.º 30
0
    def do_POST(self):
        p = self.path
        if IS_PY2:
            ctype, pdict = cgi.parse_header(self.headers.getheader('content-type'))
        else: # Python 3
            ctype, pdict = cgi.parse_header(self.headers['content-type'])

        if ctype == 'multipart/form-data':
            postvars = cgi.parse_multipart(self.rfile, pdict)
        elif ctype == 'application/x-www-form-urlencoded':
            try:
                length = int(self.headers.getheader('content-length'))
            except AttributeError:
                length = int(self.headers['content-length'])
            # parse_qs borks if we give it a Unicode string in Python2.
            url_str = self.rfile.read(length).decode('utf-8')
            if IS_PY2: url_str = str(url_str)
            postvars = cgi.parse_qs(url_str, keep_blank_values=1)
        else:
            postvars = {}

        if p == '/set_color/':
            what = postvars.get('what')
            color = postvars.get('color')
            background_color = postvars.get('background_color')
            bold = postvars.get('bold')
            underline = postvars.get('underline')

            if what:
                # Not sure why we get lists here?
                output = self.do_set_color_for_variable(what[0], color[0], background_color[0], parse_bool(bold[0]), parse_bool(underline[0]))
            else:
                output = 'Bad request'
        elif p == '/get_function/':
            what = postvars.get('what')
            output = [self.do_get_function(what[0])]
        elif p == '/delete_history_item/':
            what = postvars.get('what')
            if self.do_delete_history_item(what[0]):
                output = ["OK"]
            else:
                output = ["Unable to delete history item"]
        elif p == '/set_prompt/':
            what = postvars.get('what')
            if self.do_set_prompt_function(what[0]):
                output = ["OK"]
            else:
                output = ["Unable to set prompt"]
        else:
            return SimpleHTTPServer.SimpleHTTPRequestHandler.do_POST(self)

        # Return valid output
        self.send_response(200)
        self.send_header('Content-type','text/html')
        self.write_to_wfile('\n')

        # Output JSON
        self.write_to_wfile(json.dumps(output))
Ejemplo n.º 31
0
def _do_request(method,
                url,
                headers,
                data=None,
                query_params=None,
                timeout=20):
    """
    Performs an HTTP request

    :param method:
        A unicode string of 'POST' or 'PUT'

    :param url;
        A unicode string of the URL to request

    :param headers:
        A dict of unicode strings, where keys are header names and values are
        the header values.

    :param data:
        A dict of unicode strings (to be encoded as
        application/x-www-form-urlencoded), or a byte string of data.

    :param query_params:
        A dict of unicode keys and values to pass as query params

    :param timeout:
        An integer number of seconds to use as the timeout

    :return:
        A 3-element tuple:
         - 0: A unicode string of the response content-type
         - 1: A unicode string of the response encoding, or None
         - 2: A byte string of the response body
    """

    if query_params:
        url += '?' + urlencode(query_params).replace('+', '%20')

    if isinstance(data, dict):
        data_bytes = {}
        for key in data:
            data_bytes[key.encode('utf-8')] = data[key].encode('utf-8')
        data = urlencode(data_bytes)
        headers['Content-Type'] = 'application/x-www-form-urlencoded'
    if isinstance(data, str_cls):
        raise TypeError('data must be a byte string')

    try:
        tempfd, tempf_path = tempfile.mkstemp('-coverage')
        os.write(tempfd, data or b'')
        os.close(tempfd)

        if sys.platform == 'win32':
            powershell_exe = os.path.join(
                'system32\\WindowsPowerShell\\v1.0\\powershell.exe')
            code = "[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12;"
            code += "$wc = New-Object Net.WebClient;"
            for key in headers:
                code += "$wc.Headers.add('%s','%s');" % (key, headers[key])
            code += "$out = $wc.UploadFile('%s', '%s', '%s');" % (url, method,
                                                                  tempf_path)
            code += "[System.Text.Encoding]::GetEncoding('ISO-8859-1').GetString($wc.ResponseHeaders.ToByteArray())"

            # To properly obtain bytes, we use BitConverter to get hex dash
            # encoding (e.g. AE-09-3F) and they decode in python
            code += " + [System.BitConverter]::ToString($out);"
            stdout, stderr = _execute([powershell_exe, '-Command', code],
                                      os.getcwd(),
                                      re.compile(r'Unable to connect to|TLS'))
            if stdout[-2:] == b'\r\n' and b'\r\n\r\n' in stdout:
                # An extra trailing crlf is added at the end by powershell
                stdout = stdout[0:-2]
                parts = stdout.split(b'\r\n\r\n', 1)
                if len(parts) == 2:
                    stdout = parts[0] + b'\r\n\r\n' + codecs.decode(
                        parts[1].replace(b'-', b''), 'hex_codec')

        else:
            args = [
                'curl',
                '--request',
                method,
                '--location',
                '--silent',
                '--show-error',
                '--include',
                # Prevent curl from asking for an HTTP "100 Continue" response
                '--header',
                'Expect:'
            ]
            for key in headers:
                args.append('--header')
                args.append("%s: %s" % (key, headers[key]))
            args.append('--data-binary')
            args.append('@%s' % tempf_path)
            args.append(url)
            stdout, stderr = _execute(
                args, os.getcwd(),
                re.compile(r'Failed to connect to|TLS|SSLRead'))
    finally:
        if tempf_path and os.path.exists(tempf_path):
            os.remove(tempf_path)

    if len(stderr) > 0:
        raise URLError("Error %sing %s:\n%s" % (method, url, stderr))

    parts = stdout.split(b'\r\n\r\n', 1)
    if len(parts) != 2:
        raise URLError("Error %sing %s, response data malformed:\n%s" %
                       (method, url, stdout))
    header_block, body = parts

    content_type_header = None
    content_len_header = None
    for hline in header_block.decode('iso-8859-1').splitlines():
        hline_parts = hline.split(':', 1)
        if len(hline_parts) != 2:
            continue
        name, val = hline_parts
        name = name.strip().lower()
        val = val.strip()
        if name == 'content-type':
            content_type_header = val
        if name == 'content-length':
            content_len_header = val

    if content_type_header is None and content_len_header != '0':
        raise URLError("Error %sing %s, no content-type header:\n%s" %
                       (method, url, stdout))

    if content_type_header is None:
        content_type = 'text/plain'
        encoding = 'utf-8'
    else:
        content_type, params = cgi.parse_header(content_type_header)
        encoding = params.get('charset')

    return (content_type, encoding, body)
Ejemplo n.º 32
0
 def mimetype_params(self) -> Dict[str, str]:
     """Returns the params parsed from the Content-Type header."""
     return parse_header(self.headers.get('Content-Type'))[1]
Ejemplo n.º 33
0
 def __init__(self, header_value: str) -> None:
     self.options: List[AcceptOption] = []
     for accept_option in parse_http_list(header_value):
         option, params = parse_header(accept_option)
         quality = float(params.pop('q', 1.0))
         self.options.append(AcceptOption(option, quality, params))
Ejemplo n.º 34
0
    def do_POST(self):
        try:
            if self.path.endswith("/restaurants/new"):
                ctype, pdict = cgi.parse_header(
                    self.headers.get('content-type'))
                if ctype == 'multipart/form-data':
                    pdict['boundary'] = bytes(pdict['boundary'], 'utf-8')
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    new_restaurant_name = fields.get(
                        'new_restaurant_name')[0].decode('utf-8')
                    add_new_restaurant_name(new_restaurant_name)

                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.send_header('Location', '/restaurants')
                self.end_headers()

                return

            if self.path.endswith("/edit"):
                url = self.path
                url_folders = url.split('/')
                restaurant_id = url_folders[-2]

                session = get_DBSession()
                restaurant = session.query(Restaurant).filter_by(
                    id=restaurant_id).one()

                if restaurant:

                    ctype, pdict = cgi.parse_header(
                        self.headers.get('content-type'))
                    if ctype == 'multipart/form-data':
                        pdict['boundary'] = bytes(pdict['boundary'], 'utf-8')
                        fields = cgi.parse_multipart(self.rfile, pdict)
                        new_restaurant_name = fields.get(
                            'new_restaurant_name')[0].decode('utf-8')

                        restaurant.name = new_restaurant_name
                        session.add(restaurant)
                        session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

                return

            if self.path.endswith("/delete"):
                url = self.path
                url_folders = url.split('/')
                restaurant_id = url_folders[-2]

                session = get_DBSession()
                restaurant = session.query(Restaurant).filter_by(
                    id=restaurant_id).one()

                if restaurant:

                    ctype, pdict = cgi.parse_header(
                        self.headers.get('content-type'))
                    if ctype == 'multipart/form-data':
                        session.delete(restaurant)
                        session.commit()

                    self.send_response(301)
                    self.send_header('Content-type', 'text/html')
                    self.send_header('Location', '/restaurants')
                    self.end_headers()

                return

            elif self.path.endswith("/hello"):
                self.send_response(301)
                self.send_header('Content-type', 'text/html')
                self.end_headers()
                ctype, pdict = cgi.parse_header(
                    self.headers.get('content-type'))
                if ctype == 'multipart/form-data':
                    pdict['boundary'] = bytes(pdict['boundary'], 'utf-8')
                    fields = cgi.parse_multipart(self.rfile, pdict)
                    messagecontent = fields.get('message')[0].decode('utf-8')

                output = ""
                output += "<html><body>"
                output += "<h2> Okay, how about this: </h2>"
                output += f"<h1> {messagecontent} </h1>"
                output += "<form method='POST' enctype='multipart/form-data' action='/hello'><h2>What would you like me to say?</h2><input name='message' type='text'><input type='submit' value='Submit'></form>"
                output += "</body></html>"
                self.wfile.write(output.encode(encoding='utf-8'))
                print(output)

                return

        except:
            pass
Ejemplo n.º 35
0
    def download_data(self,
                      observation_id,
                      *,
                      filename=None,
                      verbose=False,
                      **kwargs):
        """
        Download data from XMM-Newton

        Parameters
        ----------
        observation_id : string
            id of the observation to be downloaded, mandatory
            The identifier of the observation we want to retrieve, 10 digits
            example: 0144090201
        filename : string
            file name to be used to store the file
        verbose : bool
            optional, default 'False'
            flag to display information about the process
        level : string
            level to download, optional, by default everything is downloaded
            values: ODF, PPS
        instname : string
            instrument name, optional, two characters, by default everything
            values: OM, R1, R2, M1, M2, PN
        instmode : string
            instrument mode, optional
            examples: Fast, FlatFieldLow, Image, PrimeFullWindow
        filter : string
            filter, optional
            examples: Closed, Open, Thick, UVM2, UVW1, UVW2, V
        expflag : string
            exposure flag, optional, by default everything
            values: S, U, X(not applicable)
        expno : integer
            exposure number with 3 digits, by default all exposures
            examples: 001, 003
        name : string
            product type, optional, 6 characters, by default all product types
            examples: 3COLIM, ATTTSR, EVENLI, SBSPEC, EXPMAP, SRCARF
        datasubsetno : character
            data subset number, optional, by default all
            examples: 0, 1
        sourceno : hex value
            source number, optional, by default all sources
            example: 00A, 021, 001
        extension : string
            file format, optional, by default all formats
            values: ASC, ASZ, FTZ, HTM, IND, PDF, PNG


        Returns
        -------
        None if not verbose. It downloads the observation indicated
        If verbose returns the filename
        """

        link = self.data_aio_url + "obsno=" + observation_id

        link = link + "".join("&{0}={1}".format(key, val)
                              for key, val in kwargs.items())

        if verbose:
            log.info(link)

        response = self._request('GET', link, save=False, cache=True)

        # Get original extension
        _, params = cgi.parse_header(response.headers['Content-Disposition'])
        r_filename = params["filename"]
        suffixes = Path(r_filename).suffixes

        if filename is None:
            filename = observation_id

        filename += "".join(suffixes)

        log.info("Copying file to {0}...".format(filename))
        with open(filename, 'wb') as f:
            f.write(response.content)

        if verbose:
            log.info("Wrote {0} to {1}".format(link, filename))
Ejemplo n.º 36
0
def _download_http_url(
        link,  # type: Link
        session,  # type: PipSession
        temp_dir,  # type: str
        hashes,  # type: Hashes
        progress_bar  # type: str
):
    # type: (...) -> Tuple[str, str]
    """Download link url into temp_dir using provided session"""
    target_url = link.url.split('#', 1)[0]
    try:
        resp = session.get(
            target_url,
            # We use Accept-Encoding: identity here because requests
            # defaults to accepting compressed responses. This breaks in
            # a variety of ways depending on how the server is configured.
            # - Some servers will notice that the file isn't a compressible
            #   file and will leave the file alone and with an empty
            #   Content-Encoding
            # - Some servers will notice that the file is already
            #   compressed and will leave the file alone and will add a
            #   Content-Encoding: gzip header
            # - Some servers won't notice anything at all and will take
            #   a file that's already been compressed and compress it again
            #   and set the Content-Encoding: gzip header
            # By setting this to request only the identity encoding We're
            # hoping to eliminate the third case. Hopefully there does not
            # exist a server which when given a file will notice it is
            # already compressed and that you're not asking for a
            # compressed file and will then decompress it before sending
            # because if that's the case I don't think it'll ever be
            # possible to make this work.
            headers={"Accept-Encoding": "identity"},
            stream=True,
        )
        resp.raise_for_status()
    except requests.HTTPError as exc:
        logger.critical(
            "HTTP error %s while getting %s",
            exc.response.status_code,
            link,
        )
        raise

    content_type = resp.headers.get('content-type', '')
    filename = link.filename  # fallback
    # Have a look at the Content-Disposition header for a better guess
    content_disposition = resp.headers.get('content-disposition')
    if content_disposition:
        type, params = cgi.parse_header(content_disposition)
        # We use ``or`` here because we don't want to use an "empty" value
        # from the filename param.
        filename = params.get('filename') or filename
    ext = splitext(filename)[1]
    if not ext:
        ext = mimetypes.guess_extension(content_type)
        if ext:
            filename += ext
    if not ext and link.url != resp.url:
        ext = os.path.splitext(resp.url)[1]
        if ext:
            filename += ext
    file_path = os.path.join(temp_dir, filename)
    with open(file_path, 'wb') as content_file:
        _download_url(resp, link, content_file, hashes, progress_bar)
    return file_path, content_type
Ejemplo n.º 37
0
    def __init__(self, scope, stream):
        self.scope = scope
        self._content_length = 0
        self._post_parse_error = False
        self._read_started = False
        self.resolver_match = None
        self.script_name = self.scope.get("root_path", "")
        if self.script_name and scope["path"].startswith(self.script_name):
            # TODO: Better is-prefix checking, slash handling?
            self.path_info = scope["path"][len(self.script_name):]
        else:
            self.path_info = scope["path"]

        # django path is different from asgi scope path args, it should combine with script name
        if self.script_name:
            self.path = "%s/%s" % (
                self.script_name.rstrip("/"),
                self.path_info.replace("/", "", 1),
            )
        else:
            self.path = scope["path"]

        # HTTP basics
        if self.scope.get("type") == "websocket":
            self.method = "GET"
        else:
            self.method = self.scope["method"].upper()

        # fix https://github.com/django/channels/issues/622
        query_string = self.scope.get("query_string", "")
        if isinstance(query_string, bytes):
            query_string = query_string.decode("utf-8")
        self.META = {
            "REQUEST_METHOD": self.method,
            "QUERY_STRING": query_string,
            "SCRIPT_NAME": self.script_name,
            "PATH_INFO": self.path_info,
            # Old code will need these for a while
            "wsgi.multithread": True,
            "wsgi.multiprocess": True,
        }
        if self.scope.get("client", None):
            self.META["REMOTE_ADDR"] = self.scope["client"][0]
            self.META["REMOTE_HOST"] = self.META["REMOTE_ADDR"]
            self.META["REMOTE_PORT"] = self.scope["client"][1]
        if self.scope.get("server", None):
            self.META["SERVER_NAME"] = self.scope["server"][0]
            self.META["SERVER_PORT"] = str(self.scope["server"][1])
        else:
            self.META["SERVER_NAME"] = "unknown"
            self.META["SERVER_PORT"] = "0"
        # Handle old style-headers for a transition period
        if "headers" in self.scope and isinstance(self.scope["headers"], dict):
            self.scope["headers"] = [(x.encode("latin1"), y)
                                     for x, y in self.scope["headers"].items()]
        # Headers go into META
        for name, value in self.scope.get("headers", []):
            name = name.decode("latin1")
            if name == "content-length":
                corrected_name = "CONTENT_LENGTH"
            elif name == "content-type":
                corrected_name = "CONTENT_TYPE"
            else:
                corrected_name = "HTTP_%s" % name.upper().replace("-", "_")
            # HTTPbis say only ASCII chars are allowed in headers, but we latin1 just in case
            value = value.decode("latin1")
            if corrected_name in self.META:
                value = self.META[corrected_name] + "," + value
            self.META[corrected_name] = value
        # Pull out request encoding if we find it
        if "CONTENT_TYPE" in self.META:
            self.content_type, self.content_params = cgi.parse_header(
                self.META["CONTENT_TYPE"])
            if "charset" in self.content_params:
                try:
                    codecs.lookup(self.content_params["charset"])
                except LookupError:
                    pass
                else:
                    self.encoding = self.content_params["charset"]
        else:
            self.content_type, self.content_params = "", {}
        # Pull out content length info
        if self.META.get("CONTENT_LENGTH", None):
            try:
                self._content_length = int(self.META["CONTENT_LENGTH"])
            except (ValueError, TypeError):
                pass
        # Body handling
        self._stream = stream
        # Other bits
        self.resolver_match = None
Ejemplo n.º 38
0
def process_ck_web_request(i):
    """

    Input:  {
              http - Python http object
            }

    Output: { None }
    """

    # http object
    http = i['http']

    # Parse GET variables and path
    xget = {}
    xpath = {
        'host': '',
        'port': '',
        'first': '',
        'rest': '',
        'query': ''
    }  # May be used in the future

    xt = 'json'

    xpath['host'] = i.get('host', '')
    xpath['port'] = i.get('port', '')

    # Check GET variables
    if http.path != '':
        http.send_response(200)

        a = urlparse.urlparse(http.path)
        xp = a.path
        xr = ''

        if xp.startswith('/'): xp = xp[1:]

        u = xp.find('/')
        if u >= 0:
            xr = xp[u + 1:]
            xp = xp[:u]

        xt = xp

        xpath['first'] = xp
        xpath['rest'] = xr
        xpath['query'] = a.query
        b = urlparse.parse_qs(
            a.query,
            keep_blank_values=True,
        )

        xget = {}
        for k in b:
            #           xget[k]=b[k][0]
            xget[k] = urlunquote(b[k][0])
            if sys.version_info[0] < 3:
                xget[k] = xget[k].decode('utf8')

    # Check POST
    xpost = {}
    xpost1 = {}

    try:
        headers = http.headers
        content_type = headers.get('content-type')
        ctype = ''
        if content_type != None:
            ctype, pdict = cgi.parse_header(content_type)
            # Python3 cgi.parse_multipart expects boundary to be bytes, not str.
            if sys.version_info[0] < 3 and 'boundary' in pdict:
                pdict['boundary'] = pdict['boundary'].encode()

        if ctype == 'multipart/form-data':
            if sys.version_info[0] < 3:
                xpost1 = cgi.parse_multipart(http.rfile, pdict)
            else:
                xxpost1 = cgi.FieldStorage(fp=http.rfile,
                                           headers=headers,
                                           environ={'REQUEST_METHOD': 'POST'})
                for k in xxpost1.keys():
                    xpost1[k] = [xxpost1[k].value]
        elif ctype == 'application/x-www-form-urlencoded':
            length = int(http.headers.get('content-length'))
            s = http.rfile.read(length)
            if sys.version_info[0] > 2: s = s.decode('utf8')
            xpost1 = cgi.parse_qs(s, keep_blank_values=1)

    except Exception as e:
        bin = b'internal CK web service error [7101] (' + format(e).encode(
            'utf8') + ')'
        web_err({'http': http, 'type': xt, 'bin': bin})
        ck.out(ck.cfg['error'] + bin.decode('utf8'))
        return

    # Post processing
    for k in xpost1:
        v = xpost1[k]
        if k.endswith('[]'):
            k1 = k[:-2]
            xpost[k1] = []
            for l in v:
                xpost[k1].append(urlunquote(l))
        else:
            if k != 'file_content':
                xpost[k] = urlunquote(v[0])
            else:
                xpost[k] = v[0]

        if k == 'file_content':
            fcrt = xpost1.get('file_content_record_to_tmp', '')
            if (type(fcrt) == list and len(fcrt) > 0
                    and fcrt[0] == 'yes') or fcrt == 'yes':
                fd, fn = tempfile.mkstemp(
                    suffix='.tmp', prefix='ck-'
                )  # suffix is important - CK will delete such file!
                os.close(fd)

                f = open(fn, 'wb')
                f.write(xpost[k])
                f.close()

                xpost[k + '_uploaded'] = fn
                del (xpost[k])
                k += '_uploaded'
            else:
                import base64
                xpost[k + '_base64'] = base64.urlsafe_b64encode(
                    xpost[k]).decode('utf8')
                del (xpost[k])
                k += '_base64'

        if sys.version_info[0] < 3:
            xpost[k] = xpost[k].decode('utf8')

    # Prepare input and check if CK json present
    ii = xget
    ii.update(xpost)

    cj = ii.get('ck_json', '').strip()
    if cj != '':
        r = ck.convert_json_str_to_dict({
            'str': cj,
            'skip_quote_replacement': 'yes'
        })
        if r['return'] > 0:
            bin = b'internal CK web service error [7102] (' + r[
                'error'].encode('utf8') + b')'
            web_err({'http': http, 'type': xt, 'bin': bin})
            ck.out(ck.cfg['error'] + bin.decode('utf8'))
            return

        del (ii['ck_json'])
        ii.update(r['dict'])

    # Misc parameters
    dc = ii.get('detach_console', '')
    act = ii.get('action', '')

    # Check output type
    if ii.get('out', '') != '':
        xt = ii['out']

    if xt == '': xt = 'web'

    if xt != 'json' and xt != 'con' and xt != 'web':
        web_out({
            'http': http,
            'type': 'web',
            'bin': b'Unknown CK request (' + xt.encode('utf8') + b')!'
        })
        return

    # Prepare temporary output file
    fd, fn = tempfile.mkstemp(prefix='ck-')
    os.close(fd)
    os.remove(fn)

    # Check output
    if dc == 'yes':
        if ck.cfg.get('forbid_detached_console', '') == 'yes':
            web_out({
                'http': http,
                'type': 'web',
                'bin': b'Detached console is forbidden!'
            })
            return
    else:
        ii['out_file'] = fn
        ii['web'] = 'yes'
        if xt == 'json' or xt == 'web':
            ii['out'] = 'json_file'
        # else output to console (for remote access for example)

    ii['con_encoding'] = 'utf8'

    ii['host'] = wfe_host
    ii['port'] = wfe_port

    # Execute command *********************************************************
    if act == '':
        if cfg.get('if_web_action_not_defined', '') != '' and cfg.get(
                'if_web_module_not_defined', '') != '':
            ii['module_uoa'] = cfg['if_web_module_not_defined']
            ii['action'] = cfg['if_web_action_not_defined']

    r = call_ck(ii)

    # Process output
    if r['return'] > 0:
        if os.path.isfile(fn): os.remove(fn)

        bout = r['error']

        try:
            bout = bout.encode('utf-8')
        except Exception as e:
            pass

        web_err({'http': http, 'type': xt, 'bin': bout})
        return

    # If output to console or detached console
    if xt == 'con' or dc == 'yes':
        if os.path.isfile(fn): os.remove(fn)

        bout = r.get('std', '').encode('utf8')

        web_out({'http': http, 'type': xt, 'bin': bout})

        return

    # If json or web
    # Try to load output file
    if not os.path.isfile(fn):
        web_err({
            'http':
            http,
            'type':
            xt,
            'bin':
            b'Output json file was not created, see output (' +
            r['std'].encode('utf8') + b')!'
        })
        return

    r = ck.load_text_file({'text_file': fn, 'keep_as_bin': 'yes'})
    if r['return'] > 0:
        bout = r['error']

        try:
            bout = bout.encode('utf-8')
        except Exception as e:
            pass

        web_err({'http': http, 'type': xt, 'bin': bout})

        return

    bin = r['bin']

    if os.path.isfile(fn): os.remove(fn)

    # Process JSON output from file
    fx = ''

    if sys.version_info[0] > 2: bin = bin.decode('utf-8')

    ru = ck.convert_json_str_to_dict({
        'str': bin,
        'skip_quote_replacement': 'yes'
    })
    if ru['return'] > 0:
        bout = ru['error']

        try:
            bout = bout.encode('utf-8')
        except Exception as e:
            pass

        web_err({'http': http, 'type': xt, 'bin': bout})

        return

    rr = ru['dict']
    if rr['return'] > 0:
        bout = rr['error']

        try:
            bout = bout.encode('utf-8')
        except Exception as e:
            pass

        web_err({'http': http, 'type': xt, 'bin': bout})
        return

    # Check if file was returned
    fr = False

    if 'file_content_base64' in rr and rr.get('filename', '') != '':
        fr = True

    # Check if download
    if (xt == 'web' and fr) or (act == 'pull' and xt != 'json'):
        import base64
        x = rr.get('file_content_base64', '')

        fx = rr.get('filename', '')
        if fx == '': fx = ck.cfg['default_archive_name']

        # Fixing Python bug
        if sys.version_info[0] == 3 and sys.version_info[1] < 3:
            x = x.encode('utf-8')
        else:
            x = str(x)
        bin = base64.urlsafe_b64decode(
            x)  # convert from unicode to str since base64 works on strings
        # should be safe in Python 2.x and 3.x

        # Process extension
        fn1, fne = os.path.splitext(fx)
        if fne.startswith('.'): fne = fne[1:]
        if fne != '': xt = fne
        else: xt = 'unknown'
    else:
        # Check and output html
        if rr.get('html', '') != '':
            bin = rr['html'].encode('utf-8')
        else:
            if sys.version_info[0] > 2:  # Unknown output
                bin = bin.encode('utf-8')

    web_out({'http': http, 'type': xt, 'bin': bin, 'filename': fx})

    return {'return': 0}