URL Validation Help in C


 
Thread Tools Search this Thread
Top Forums Programming URL Validation Help in C
# 1  
Old 05-17-2012
Bug URL Validation Help in C

Hi friends,

I have been writing one c code to validate any url passed by a user.
Here is the code I have so far coded.
I am using one dictionary file to check values like http, https, www, ftp, etc for the URL that a user will pass for validation.

My code so far holds good for every thing except for if blank spaces or a new line character or a tab is mentioned in the URL string passed by user. I m getting a segmentation fault.

Please Help me.



Code:
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define SIZE 50
char *domain1 = "com";
char *domain2 = "co.in";
char *domain3 = "org";
char *domain4 = "net";
char *domain5 = "in";
char *domain6 = "org.net";
static bool f_valid = 0;
bool matchURL(char *url, FILE *fp)
{
        int j = 0;
        int k = 0;
        char buff[SIZE]; 
        char *token1;
        char token2[SIZE] = {0};
        char token3[SIZE] = {0};
        char *delimeter = ".";

        token1 = strtok(url,delimeter);
 
        while ((fscanf(fp,"%s",buff)) != EOF)
        {
                if(0 == strcmp(buff,token1))
                {
                        f_valid = 1;
                }
                else
                {
                        f_valid = 0;
                }
        }
 
        j = 1 + strlen(token1);
 
        if (1 == f_valid) 
        { 
                while(url[j] != '.')
                {
                        token2[k] = url[j];
                        j++;
                        k++;
                }
        }
 
        if ((0 == strlen(token2)) || (0 == strcmp("www",token2)))
        {
                f_valid = 0;
        }
 
        j = 2 + (strlen(token1)+strlen(token2));
 
        k = 0;
 
        while(url[j] != '\0')
        {
                token3[k] = url[j];
                j++;
                k++;
        } 
 
        if(1 == f_valid)
        {
                if((0 == strcmp(domain1,token3)) || (0 == strcmp(domain2,token3)) || (0 == strcmp(domain3,token3)) || (0 == strcmp(domain4,token3)) || (0 == strcmp(domain5,token3)) || (0 == strcmp(domain6,token3)))
                {
                        f_valid = 1;
                }
                else 
                {
                        f_valid = 0;
                } 
        }
 
        return f_valid;
}

int main ()
{ 
        int i = 0;
        char url[SIZE];
        FILE *fp;

        if ((fp=fopen("Dictionary", "r")) == NULL)
        {
                printf("Can't Validate\tDictionary File is missing\n");
                return -1;
        }

        printf("Enter the URL to Validate :\n");
        scanf("%s",url);

        for(i; i < strlen(url); i++)
        {
        if ((url[i] == '@') || (url[i] == '!') || (url[i] == '#') || (url[i] == '$') || (url[i] == '%') || (url[i] == '^') || (url[i] == '&') || (url[i] == '*') || (url[i] == '(') || (url[i] == ')') || (url[i] == '\t') || (url[i] == '\b') || (url[i] == '\n'))
        {
                printf("Invaild URL\nSpecial Character Found!\n");
                return -1;
        }
}

        if ( 1 == matchURL(url,fp))
        {
                printf("Valid URL address\n");
        }
        else
                printf ("Invalid URL address\n");
 
        fclose(fp);
        return 0;
 
}

Moderator's Comments:
Mod Comment Code tags for code, please.

Last edited by Corona688; 05-17-2012 at 12:23 PM..
# 2  
Old 05-17-2012
Your complete and total lack of comments makes it difficult to even begin to figure out where your code is going wrong. You haven't posted what's in your dictionary file either, making it very difficult to guess your intent. breaking the string apart on "." won't get you the http:// at the beginning, either. I've corrected your indenting as best I can.

I think your code needs a rewrite. A lot of things which could've been done with loops or function calls you've done by simple brute force.

Your for-loop while reading the file probably doesn't do what you want. It will loop through every line, meaning, you only get the value you checked for the very last line -- every other value is overwritten by the one after.

Whenever you've decided a URL is invalid, you can just return(0) right then and there, instead of checking f_valid every time thereafter.

You do realize that strtok() modifies its input string, yes?

Also, it'd be better to check for allowable characters than disallowed ones:

Code:
int url_specialchars(const char *url)
{
        // The compiler will stack "multiple" "strings" "end" "to" "end"
        // into "multiplestringsendtoend", so we don't need one giant line.
        static const char *nospecial="0123456789"
                "abcdefghijklmnopqrstuvwxyz"
                "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
                ".";

        while(*url) // Loop until (*url) == 0.  (*url) is about equivalent to url[0].
        {
                // Can we find the character at *url in the string 'nospecial'?
                // If not, it's a special character and we should return 0.
                if(strchr(nospecial, *url) == NULL) return(0);
                url++; // Jump to the next character.  Adding one to a pointer moves it ahead one element.
        }

        return(1); // Return 1 for success.
}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Validation using While and IF

I am learning Shell scripting on own. I am trying to do an assignment to get details from the user like username their individual marks ,DOB and send a report in mail with the Details calculated like total and average. validate_marks() { local Value=$1 if && then return 0 else... (1 Reply)
Discussion started by: JayashreeRobin
1 Replies

2. Shell Programming and Scripting

Reading URL using Mechanize and dump all the contents of the URL to a file

Hello, Am very new to perl , please help me here !! I need help in reading a URL from command line using PERL:: Mechanize and needs all the contents from the URL to get into a file. below is the script which i have written so far , #!/usr/bin/perl use LWP::UserAgent; use... (2 Replies)
Discussion started by: scott_cog
2 Replies

3. UNIX for Dummies Questions & Answers

Awk: print all URL addresses between iframe tags without repeating an already printed URL

Here is what I have so far: find . -name "*php*" -or -name "*htm*" | xargs grep -i iframe | awk -F'"' '/<iframe*/{gsub(/.\*iframe>/,"\"");print $2}' Here is an example content of a PHP or HTM(HTML) file: <iframe src="http://ADDRESS_1/?click=5BBB08\" width=1 height=1... (18 Replies)
Discussion started by: striker4o
18 Replies

4. Web Development

Regex to rewrite URL to another URL based on HTTP_HOST?

I am trying to find a way to test some code, but I need to rewrite a specific URL only from a specific HTTP_HOST The call goes out to http://SUB.DOMAIN.COM/showAssignment/7bde10b45efdd7a97629ef2fe01f7303/jsmodule/Nevow.Athena The ID in the middle is always random due to the cookie. I... (5 Replies)
Discussion started by: EXT3FSCK
5 Replies

5. Shell Programming and Scripting

Name validation

Hi All, I need to write a small piece of code to check the following. name should contain (A-Z), spaces, hyphens & apostrophes I need to generate regular expressions for the same. Please help me out as i am not familiar with regular expressions. (1 Reply)
Discussion started by: lifzgud
1 Replies

6. UNIX for Dummies Questions & Answers

ReDirecting a URL to another URL - Linux

Hello, I need to redirect an existing URL, how can i do that? There's a current web address to a GUI that I have to redirect to another webaddress. Does anyone know how to do this? This is on Unix boxes Linux. example: https://m45.testing.address.net/host.php make it so the... (3 Replies)
Discussion started by: SkySmart
3 Replies

7. UNIX for Dummies Questions & Answers

Validation

I'm kinda new in shell scripting. How do i validate an input from a user to conform to requirement. For example, echo "Enter First Name: " read FName echo "Enter Date of Employment (dd/mm/yyyy): " read DoE If the user enters data that is alphanumeric, it accepts it. I hope i've... (1 Reply)
Discussion started by: Allenzo
1 Replies

8. Shell Programming and Scripting

url calling and parameter passing to url in script

Hi all, I need to write a unix script in which need to call a url. Then need to pass parameters to that url. please help. Regards, gander_ss (1 Reply)
Discussion started by: gander_ss
1 Replies

9. UNIX for Advanced & Expert Users

url calling and parameter passing to url in script

Hi all, I need to write a unix script in which need to call a url. Then need to pass parameters to that url. please help. Regards, gander_ss (1 Reply)
Discussion started by: gander_ss
1 Replies
Login or Register to Ask a Question