ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

HTTP Headers and QtmhGetEnv

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HTTP Headers and QtmhGetEnv

    When I call QtmhGetEnv (within a CGI program called via an Apache server) to retrieve an HTTP header, I would expect the QtmhGetEnv procedure to return an error if the header does not exist. However, since the value it is returning is blank, and there is no error in the return parm, I am unable to determine if the value is really blank or if the header does not exist. Is there any way to get QtmhGetEnv to return an error when the header is not found, or, better yet, get all HTTP headers into 1 string so I can parse it myself? Here is some sample code:



    PHP Code:

            dcl
    -pr getHttpHeaders extproc('QtmhGetEnv');
              
    pHeaderData like(headerData);
              
    pHeaderDataLength like(headerDataLength);
              
    pReturnedDataLength like(headerDataLength);
              
    pHeadername char(30) const;
              
    pHeaderNameLength int(10:0) const;
              
    perror like(apiError);
            
    end-pr;

            
    dcl-ds apiError;
              
    errorByteP int(10:0inz(40);
              
    errorByteA int(10:0);
              
    errorMsgID char(7);
              
    errorReserved char(1);
              
    errorData char(40);
            
    end-ds;

            
    dcl-s headerData char(64inz;
            
    dcl-s headerDataLength int(10:0inz(%size(headerData));
            
    dcl-s returnedDataLength int(10:0inz;
            
    dcl-c OriginHeader const('HTTP_ORIGIN');
            
    dcl-c OriginHeaderLen const(%size(OriginHeader));


              
    callp getHttpHeadersheaderData
                                  
    headerDataLength
                                  
    returnedDataLength
                                  
    OriginHeader
                                  
    OriginHeaderLen
                                  
    APIError ); 



  • #2
    FWIW, I use the getenv() API which returns a pointer, which, IIRC, returns a null for a non-existent env var.

    Comment


    • #3
      I would expect the reurnedDataLength to be zero if the variable did not exist - have you checked that?

      That said, I'm with jtaylor - I would use getenv() - faster and easier to use.

      Comment


      • #4
        jtaylor___ and JonBoy

        This is what i was needing, thanks!

        I have a question on top of that. I was highly interested in grabbing the entire header string. I see that if i use the following code, I can get more than just the Origin header.

        PHP Code:
               dcl-s headerPointer pointer;
               
        dcl-s headerValueBased char(256based(headerPointer);
               
        dcl-s testValue char(256inz;   


                  
        headerPointer getEnv('HTTP_ORIGIN');
                  if 
        headerPointer = *null;
                    
        testValue 'notfound';
                  else;
                    
        testValue headerValueBased// this retrieves 256 characters from header starting with the value of HTTP_ORIGIN

                    
        clear testValue;

                    
        testValue = %str(headerPointer);  // this retrieves only the HTTP_ORIGIN value

                  
        endif; 

        Do you know what the first header value is? Is there a trick to get the entire header string?

        Comment


        • #5
          When using C style APIs that return a pointer to a string, the string is null terminated (C basically has no real notion of a fixed length character string). So if you had declared your based variable as 1024 instead of 256 you'd have seen a bunch more rubbish beyond the header.

          When using APIs like this _only_ the data up to the first X'00" can be trusted. So your second example using %Str is the only correct option of the two. Using the first option would be risky.

          Don't know about getting the whole string - GetEnv() doesn't appear to support it.

          Comment

          Working...
          X