示例#1
0
def BuildCGIRequest(base_env_dict, request, dev_appserver):
    """Build a CGI request to Call a method on an SPI backend.

  Args:
    base_env_dict: CGI environment dict
    request: ApiRequest to be converted to a CGI request
    dev_appserver: Handle to dev_appserver to generate CGI request.

  Returns:
    dev_appserver.AppServerRequest internal redirect object
  """
    if request.headers is None:
        request.headers = {}

    request.headers['Content-Type'] = 'application/json'
    url = SPI_ROOT_FORMAT % (request.port, request.path)
    base_env_dict['REQUEST_METHOD'] = 'POST'

    header_outfile = cStringIO.StringIO()
    body_outfile = cStringIO.StringIO()
    WriteHeaders(request.headers, header_outfile, len(request.body))
    body_outfile.write(request.body)
    header_outfile.seek(0)
    body_outfile.seek(0)
    return dev_appserver.AppServerRequest(url, None,
                                          mimetools.Message(header_outfile),
                                          body_outfile)
示例#2
0
        def Dispatch(self, request, outfile, base_env_dict=None):
            """Handle post dispatch.

      This dispatcher will handle all uploaded files in the POST request, store
      the results in the blob-storage, close the upload session and transform
      the original request in to one where the uploaded files have external
      bodies.

      Returns:
        New AppServerRequest indicating request forward to upload success
        handler.
      """
            if base_env_dict['REQUEST_METHOD'] != 'POST':
                outfile.write('Status: 400\n\n')
                return

            upload_key = re.match(UPLOAD_URL_PATTERN,
                                  request.relative_url).group(1)
            try:
                upload_session = datastore.Get(upload_key)
            except datastore_errors.EntityNotFoundError:
                upload_session = None

            if upload_session:
                success_path = upload_session['success_path']

                upload_form = cgi.FieldStorage(fp=request.infile,
                                               headers=request.headers,
                                               environ=base_env_dict)

                try:
                    mime_message_string = self.__cgi_handler.GenerateMIMEMessageString(
                        upload_form)
                    datastore.Delete(upload_session)
                    self.current_session = upload_session

                    header_end = mime_message_string.find('\n\n') + 1
                    content_start = header_end + 1
                    header_text = mime_message_string[:header_end]
                    content_text = mime_message_string[content_start:]

                    complete_headers = ('%s'
                                        'Content-Length: %d\n'
                                        '\n') % (header_text,
                                                 len(content_text))

                    return dev_appserver.AppServerRequest(
                        success_path,
                        None,
                        mimetools.Message(
                            cStringIO.StringIO(complete_headers)),
                        cStringIO.StringIO(content_text),
                        force_admin=True)
                except dev_appserver_upload.InvalidMIMETypeFormatError:
                    outfile.write('Status: 400\n\n')
            else:
                logging.error('Could not find session for %s', upload_key)
                outfile.write('Status: 404\n\n')
示例#3
0
    def Dispatch(self,
                 request,
                 outfile,
                 base_env_dict=None):
      """Handle post dispatch.

      This dispatcher will handle all uploaded files in the POST request, store
      the results in the blob-storage, close the upload session and transform
      the original request in to one where the uploaded files have external
      bodies.

      Returns:
        New AppServerRequest indicating request forward to upload success
        handler.
      """

      if base_env_dict['REQUEST_METHOD'] != 'POST':
        outfile.write('Status: 400\n\n')
        return


      upload_key = re.match(UPLOAD_URL_PATTERN, request.relative_url).group(1)
      try:
        upload_session = datastore.Get(upload_key)
      except datastore_errors.EntityNotFoundError:
        upload_session = None

      if upload_session:
        success_path = upload_session['success_path']
        max_bytes_per_blob = upload_session['max_bytes_per_blob']
        max_bytes_total = upload_session['max_bytes_total']
        bucket_name = upload_session.get('gs_bucket_name', None)

        upload_form = cgi.FieldStorage(fp=request.infile,
                                       headers=request.headers,
                                       environ=base_env_dict)

        try:


          mime_message_string = self.__cgi_handler.GenerateMIMEMessageString(
              upload_form,
              max_bytes_per_blob=max_bytes_per_blob,
              max_bytes_total=max_bytes_total,
              bucket_name=bucket_name)

          datastore.Delete(upload_session)
          self.current_session = upload_session


          header_end = mime_message_string.find('\n\n') + 1
          content_start = header_end + 1
          header_text = mime_message_string[:header_end].replace('\n', '\r\n')
          content_text = mime_message_string[content_start:].replace('\n',
                                                                     '\r\n')


          complete_headers = ('%s'
                              'Content-Length: %d\r\n'
                              '\r\n') % (header_text, len(content_text))

          return dev_appserver.AppServerRequest(
              success_path,
              None,
              mimetools.Message(cStringIO.StringIO(complete_headers)),
              cStringIO.StringIO(content_text),
              force_admin=True)
        except dev_appserver_upload.InvalidMIMETypeFormatError:
          outfile.write('Status: 400\n\n')
        except dev_appserver_upload.UploadEntityTooLargeError:
          outfile.write('Status: 413\n\n')
          response = ERROR_RESPONSE_TEMPLATE % {
              'response_code': 413,
              'response_string': 'Request Entity Too Large',
              'response_text': 'Your client issued a request that was too '
              'large.'}
          outfile.write(response)
        except dev_appserver_upload.FilenameOrContentTypeTooLargeError, ex:
          outfile.write('Status: 400\n\n')
          response = ERROR_RESPONSE_TEMPLATE % {
              'response_code': 400,
              'response_string': 'Bad Request',
              'response_text': str(ex)}
          outfile.write(response)